2

I have a problem. I want to convert BitmapImage into byte[] array and back.

I wrote these methods:

public static byte[] ToByteArray(this BitmapImage bitmapImage) { byte[] bytes; using (MemoryStream ms = new MemoryStream()) { bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource.CopyTo(ms); bitmapImage.EndInit(); bytes = ms.ToArray(); } return bytes; } public static BitmapImage ToBitmapImage(this byte[] bytes, int width, int height) { BitmapImage bitmapImage = new BitmapImage(); using (MemoryStream ms = new MemoryStream(bytes)) { ms.Position = 0; bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.StreamSource = ms; bitmapImage.EndInit(); // HERE'S AN EXCEPTION!!! } return bitmapImage; } 

First one works fine, but when I try to convert from byte[] into BitmapImage I got a NotSupportedException... Why? How to correct the code of the 2nd method?

4
  • I just tested the code you posted and it worked fine for me. Are you sure your bytes are a valid image? Commented Jan 13, 2013 at 2:16
  • BitmapImage should be valid, because I display it in my form:/ Don't know where the problem is... Commented Jan 13, 2013 at 2:27
  • I am having issues getting the ToByteArray method working, I keep getting errors stating that 'Cannot set the initializing state more than once.' on the BeginInit line. I am wondering if something in that method is returning invalid bytes. Commented Jan 13, 2013 at 2:32
  • Refer to this link - social.msdn.microsoft.com/Forums/en-US/winforms/thread/… Commented Jan 13, 2013 at 7:55

1 Answer 1

5

There are two problems with your ToByteArray method.

First it calls BeginInit and EndInit on an already initialized BitmapImage instance. This is not allowed, see the Exceptions list in BeginInit.

Second, the method could not be called on a BitmapImage that was created from an Uri instead of a Stream. Then the StreamSource property would be null.

I suggest to implement the method like shown below. This implementation would work for any BitmapSource, not only BitmapImages. And you are able to control the image format by selecting an appropriate BitmapEncoder, e.g. JpegBitmapEncoder instead of PngBitmapEncoder.

public static byte[] ToByteArray(this BitmapSource bitmap) { var encoder = new PngBitmapEncoder(); // or any other encoder encoder.Frames.Add(BitmapFrame.Create(bitmap)); using (var ms = new MemoryStream()) { encoder.Save(ms); return ms.ToArray(); } } 

An image buffer returned by this ToByteArray method can always be converted back to a BitmapImage by your ToBitmapImage method.

And please note that the width and height arguments of your ToBitmapImage method are currently unused.


UPDATE

An alternative implementation of the decoding method could look like shown below, although it does not return a BitmapImage but only an instance of the base class BitmapSource. You may however change the return type to BitmapFrame.

public static BitmapSource ToBitmapImage(this byte[] bytes) { using (var stream = new MemoryStream(bytes)) { var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); return decoder.Frames[0]; } } 
Sign up to request clarification or add additional context in comments.

6 Comments

I tried your method ToByteArray, but still getting same error in my ToBitmapImage method. Do you know how do convert byte[] array into BitmapImage in the another way?
Your ToByteArray method looks ok. You may perhaps remove the ms.Position = 0; statement, since it is redundant. I have edited my answer to provide an alternative solution for decoding the image. Note however that this method will not return a BitmapImage but a BitmapFrame instead.
I guess, I can simply cast BitmapSource to BitmapImage?
No you can't. It's a BitmapFrame, not a BitmapImage. Both have BitmapSource as common base class but you can't cast one to the other. Why exactly do you need BitmapImage? BitmapSource usually provides all necessary information about the bitmap.
No I don't need BitmapImage. I just need 3 methods. The first one that renders my System.Windows.Controls.Image into Bitmap/BitmapImage/etc., the second one that converts it into byte[] array (I need to serialize the image into binary file), and the third one that converts byte[] array back into my image (because I want to display it using binding for System.Windows.Controls.Image.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.