0

i use for a project a function that i found on StackOverflow : https://stackoverflow.com/a/6484754/9535211
The goal of this function is to convert a System.Windows.Media.Imaging.BitmapImage to a System.Drawing.Bitmap.

public Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{

            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bitmapImage));
                enc.Save(outStream);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
                return (new Bitmap(bitmap));
            }
}

It works pretty well (even if its realy heavy), but it throw an exception everytime its called : Exception thrown: 'System.NotSupportedException' in PresentationCore.dll
It seems that it comes from the line :

enc.Save(outStream);

Is there a way to make it disappear ?
Thanks for your help !

Axel.A
  • 94
  • 1
  • 9
  • By the way, the "Hello" at the begining of my question seems to have disappear and i can't edit it... – Axel.A Oct 24 '18 at 13:32
  • 1
    Your code should work. Make sure the BitmapImage is valid. However, it seems odd to have `return (new Bitmap(bitmap));` instead of just `return bitmap;` – Clemens Oct 24 '18 at 13:40
  • After taking a closer look, what you mean is that it writes an error message to the Debug Output, but it does not actually throw an exception? – Clemens Oct 24 '18 at 14:01
  • Well, you're right, it don't realy throw an exception even if its not in a try/catch block (Its in a "using" block). I just wanted to know if its "dangerous" to let it in the program like that (since it work) – Axel.A Oct 24 '18 at 14:12
  • Not sure why, but even with a correctly loaded BitmapImage, `BitmapFrame.Create(bitmapImage)` outputs this message. It just writes the message to debug out, nothing to worry about. Just an exception internally thrown and handled by the framework. – Clemens Oct 24 '18 at 14:13
  • Oh ok, thanks for your help ! – Axel.A Oct 24 '18 at 14:17
  • You may avoid this message if you wouldn't use a BitmapImage at all. You could as well directly create a BitmapFrame from an Uri or a Stream, and pass that directly to enc.Frames. Since it also is a BitmapSource, a BitmapFrame can usually be used everywhere where a BitmapImage is used, e.g. for the Source property of an Image element. – Clemens Oct 24 '18 at 14:21

1 Answers1

-1

You should check to ensure that the BitmapImage you're passing in is valid. According to the documentation for BitmapEncoder.Save, the error you're getting is a result of the "Frames" count being less than or equal to zero.

Ensure that the frame is being added properly and that your bitmapImage is being passed in with the correct values.

Heisenberg
  • 37
  • 2
  • 12
  • "Ensure that the frame is being added properly": `enc.Frames.Add(BitmapFrame.Create(bitmapImage));` – Clemens Oct 24 '18 at 13:41
  • Ensure it's being added properly. As in, run it in the debugger and check to make sure. It's obvious that this is where things are failing. Because it's not being added properly. – Heisenberg Oct 24 '18 at 13:42
  • Which part of `enc.Frames.Add(BitmapFrame.Create(...))` should be checked by OP? The remark is pointless. "*It's obvious that this is where things are failing*" - not at all. If you try the code from the question with a valid BitmapImage, it just works. – Clemens Oct 24 '18 at 13:43
  • As opposed to what? "Your code should work."? How is that any more helpful? I'm just trying to point OP to where they might look because they seem to be stuck on the exception with no proof of doing any research. Just trying to help here but I forgot this place isn't for that anymore. – Heisenberg Oct 24 '18 at 13:47
  • I'm not writing an answer, based on a guess. You might write this as a comment when you have got enough reputation. Right now, it doesn't answer anything. – Clemens Oct 24 '18 at 13:49
  • Okay, that's fair. I tried to comment but don't have enough internet points to do that. Otherwise that's what I would have done. – Heisenberg Oct 24 '18 at 13:50