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
Full method:
Context:
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•3y ago
Message Not Public
Sign In & Join Server To View
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•3y ago
Message Not Public
Sign In & Join Server To View
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•3y ago
Message Not Public
Sign In & Join Server To View
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•3y ago
Message Not Public
Sign In & Join Server To View
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
.Weird, because i'm not using .NET 6 in my project!
I'm using .NET 5, and have been for a while.
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?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:Maybe your
System.Drawing.Common
is past .NET5 and does an TFM check somehow? I'm not really sure.? Maybe?
Try changing the project target to
net5.0-windows
and see what happens.Cannot sadly
Though that should only affect build errors and not runtime stuff.
These are my options and it's already on .NET 5
You need to open the csproj
Just double click the project and edit the XML.
<TargetFramework>net5.0-windows7.0</TargetFramework>
I think that's a valid one. 🤔
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
No, windows 10.
That's a min version.
Switching the OS version makes no difference, i've tested that.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
If only!
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Not yet, I'm going to look over it tonight. Steeped neck-deep in work at the moment.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
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.
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
.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•3y ago
Message Not Public
Sign In & Join Server To View
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.
I'd just map the coordinate to fetch from the
WriteableBitmap
then.Awesome! How?
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
That was painful to read.
Now I know why a friend of mine calls M$ "Microshit"
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•3y ago
Message Not Public
Sign In & Join Server To View
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.Oh and the
Bitmap
property here is a WriteableBitmap
.
Well, you said the Source
is/was a WriteableBitmap
.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.
If that's true everywhere where needed, then you can just cast.
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.
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.✅ This post has been marked as answered!