C
C#3y ago
LukeZurg22

Bitmap From BitmapSource new Error [Answered]

I've been working on a project of mine for quite some time, and I haven't encountered this error before. Data fed into the method converting an image source to a bitmap has not changed, nor the method itself. I cannot explain why I am getting this error, and google yielded no results.
50 Replies
LukeZurg22
LukeZurg22OP3y ago
System.PlatformNotSupportedException: 'System.Drawing.Common is not supported on this platform.'
System.PlatformNotSupportedException: 'System.Drawing.Common is not supported on this platform.'
Full method:
private Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();

enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new Bitmap(outStream);
}
return bitmap;
}
private Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();

enc.Frames.Add(BitmapFrame.Create(bitmapsource));
enc.Save(outStream);
bitmap = new Bitmap(outStream);
}
return bitmap;
}
Context:
var source = (BitmapSource)mapMode.Source;
var image = (Image)sender;
var mousePos = e.GetPosition(image);
var pixelX = (int)((mousePos.X / image.ActualWidth * source.PixelWidth) - 0.1);
var pixelY = (int)((mousePos.Y / image.ActualHeight * source.PixelHeight) - 0.1);
var bitmap = BitmapFromSource(source);
var pixelColor = bitmap.GetPixel(pixelX, pixelY);
var source = (BitmapSource)mapMode.Source;
var image = (Image)sender;
var mousePos = e.GetPosition(image);
var pixelX = (int)((mousePos.X / image.ActualWidth * source.PixelWidth) - 0.1);
var pixelY = (int)((mousePos.Y / image.ActualHeight * source.PixelHeight) - 0.1);
var bitmap = BitmapFromSource(source);
var pixelColor = bitmap.GetPixel(pixelX, pixelY);
mapMode is from the Image control in WPF. : System.Windows.Controls.Image The concept is that I am converting the data of an image source into a bitmap for manipulation.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
I’m using .NET 5 I upgraded from an older .NET Framework, but not to .NET 6 as far as I’m aware. My goal was to simply get the pixel data of an image that has been clicked on. The image changes and moves, so how I handled it was to convert the image source into a bitmap, which any given pixel was read directly.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
Wacky, since it worked fine for the while until now. Regardless yeah it’s in windows; would you know any alternative to getting a pixel from a click upon an image control.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
Hmmm.. when I hop on my pc I’ll take a look. It just might have what I’m going for. Why had Windows discontinued System.Drawing anyway? It was the crutch of a lot of my image manipulation in my first project, and seemed rather important.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
distilldowo
Klarth
Klarth3y ago
It's not "discontinued". But System.Drawing.Common isn't available in .NET 6 (net6.0) because it was never cross-platform compatible. However, you can add it via NuGet and use it if you use net6.0-windows.
LukeZurg22
LukeZurg22OP3y ago
Weird, because i'm not using .NET 6 in my project! I'm using .NET 5, and have been for a while.
Klarth
Klarth3y ago
Yeah, kinda weird in the context of .NET 5 WPF. https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only is the article for the change. What's the exact line that throws PlatformNotSupportException? Or does it happen at startup?
LukeZurg22
LukeZurg22OP3y ago
Exactly? I'll get it for you, 1 sec bitmap = new Bitmap(outStream); : Line 236 on NavigationHandler.cs Error: System.PlatformNotSupportedException: 'System.Drawing.Common is not supported on this platform.' Details:
Klarth
Klarth3y ago
Maybe your System.Drawing.Common is past .NET5 and does an TFM check somehow? I'm not really sure.
LukeZurg22
LukeZurg22OP3y ago
? Maybe?
Klarth
Klarth3y ago
Try changing the project target to net5.0-windows and see what happens.
LukeZurg22
LukeZurg22OP3y ago
Cannot sadly
LukeZurg22
LukeZurg22OP3y ago
Klarth
Klarth3y ago
Though that should only affect build errors and not runtime stuff.
LukeZurg22
LukeZurg22OP3y ago
These are my options and it's already on .NET 5
Klarth
Klarth3y ago
You need to open the csproj Just double click the project and edit the XML.
LukeZurg22
LukeZurg22OP3y ago
<TargetFramework>net5.0-windows7.0</TargetFramework>
Klarth
Klarth3y ago
I think that's a valid one. 🤔
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
No, windows 10.
Klarth
Klarth3y ago
That's a min version.
LukeZurg22
LukeZurg22OP3y ago
Switching the OS version makes no difference, i've tested that.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
If only!
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
Not yet, I'm going to look over it tonight. Steeped neck-deep in work at the moment.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
Well, I say that, but I did do a quick test of the sample, and it looks fantastic Since it clearly works with an uploaded image file**, I'd wager it's got exactly what I need.
Klarth
Klarth3y ago
Personally, I've moved away from Bitmap into other image types like WriteableBitmap (most GUI frameworks have their own version, WPF included), ImageSharp's Image, or SkiaSharp's SKImage / SKBitmap.
LukeZurg22
LukeZurg22OP3y ago
I am using WriteableBitmap elsewhere, surely. In fact the image itself is stored as a WriteableBitmap which is sourced from the Image.Source -> BitmapSource
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
In my code I had written it apparently in a way where clicking the image is meant to yield the pixel data you clicked on, but only in the bounds of that control; lest you get funky window pixel data.
Klarth
Klarth3y ago
I'd just map the coordinate to fetch from the WriteableBitmap then.
LukeZurg22
LukeZurg22OP3y ago
Awesome! How? HmmNoted
Klarth
Klarth3y ago
Because it's a lot of work to create a managed version of those and the ImageSharp guy is a bit of an ass, even if he created a great library https://github.com/dotnet/Microsoft.Maui.Graphics/issues/47
LukeZurg22
LukeZurg22OP3y ago
That was painful to read. Now I know why a friend of mine calls M$ "Microshit"
Klarth
Klarth3y ago
You can either lock the WriteableBitmap and access the BackBuffer by pointer or use CopyPixels and work from that buffer. You can look at https://github.com/stevemonaco/ImageMagitek/blob/main/TileShop.WPF/ViewExtenders/Imaging/DirectBitmapAdapter.cs#L85 for an example of the first, although it's writing from one image format into the WriteableBitmap instead of reading. Reverse would be similar.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
LukeZurg22
LukeZurg22OP3y ago
Yeah, we are are getting into territory that is starting to become entirely foreign to me. At the moment I have BitmapSource, and Image.Source as things to work with. Image.Source is the one most reliable.
Klarth
Klarth3y ago
Oh and the Bitmap property here is a WriteableBitmap. Well, you said the Source is/was a WriteableBitmap.
LukeZurg22
LukeZurg22OP3y ago
Yes and no, and I should correct myself here. I think I actually know a good and neat way to grab it and it was under my nose in my old code.
Klarth
Klarth3y ago
If that's true everywhere where needed, then you can just cast.
LukeZurg22
LukeZurg22OP3y ago
var provinceMapSource = BitmapFactory.ConvertToPbgra32Format((BitmapSource)MainWindow.mapProvinces.Source);
var provinceMapSource = BitmapFactory.ConvertToPbgra32Format((BitmapSource)MainWindow.mapProvinces.Source);
This converts the image source to a WriteableBitmap with no issue. I only recently made it this way as the code i'm working with is old, and very poorly written, so reworking it to new methods is a bit harder when you have a supposedly functional one right in front of you.
_ = SpareMaps.TryGetValue(MainWindow.mapModeButtons.GetMapMode(), out Image mapMode);
var mapSource = BitmapFactory.ConvertToPbgra32Format((BitmapSource)mapMode.Source);
var image = (Image)sender;
var mousePos = e.GetPosition(image);
var pixelX = (int)(mousePos.X / image.ActualWidth * mapSource.PixelWidth - 0.1);
var pixelY = (int)(mousePos.Y / image.ActualHeight * mapSource.PixelHeight - 0.1);
var pixelColor = mapSource.GetPixel(pixelX, pixelY);
_ = SpareMaps.TryGetValue(MainWindow.mapModeButtons.GetMapMode(), out Image mapMode);
var mapSource = BitmapFactory.ConvertToPbgra32Format((BitmapSource)mapMode.Source);
var image = (Image)sender;
var mousePos = e.GetPosition(image);
var pixelX = (int)(mousePos.X / image.ActualWidth * mapSource.PixelWidth - 0.1);
var pixelY = (int)(mousePos.Y / image.ActualHeight * mapSource.PixelHeight - 0.1);
var pixelColor = mapSource.GetPixel(pixelX, pixelY);
For the sake of sharing, I think this might actually work It has a bit of my old code in there, but shouldn't have any issues. Oh yeah, hella inaccurate. Closing this thread. The original problem has already been solved through the usage of that suggested link.
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server