BitmapImage to byte[]

60,945

Solution 1

To convert to a byte[] you can use a MemoryStream:

byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
using(MemoryStream ms = new MemoryStream())
{
    encoder.Save(ms);
    data = ms.ToArray();
}

Instead of the JpegBitmapEncoder you can use whatever BitmapEncoder you like as casperOne said.

If you are using MS SQL you could also use a image-Column as MS SQL supports that datatype, but you still would need to convert the BitmapImage somehow.

Solution 2

You will have to use an instance of a class that derives from BitmapEncoder (such as BmpBitmapEncoder) and call the Save method to save the BitmapSource to a Stream.

You would choose the specific encoder depending on the format you want to save the image in.

Share:
60,945
SoManyGoblins
Author by

SoManyGoblins

Updated on June 24, 2021

Comments

  • SoManyGoblins
    SoManyGoblins almost 3 years

    I have a BitmapImage that I'm using in a WPF application, I later want to save it to a database as a byte array (I guess it's the best way), how can I perform this conversion?

    Or, alternatively, is there a better way to save a BitmapImage (or any of its base classes, BitmapSource or ImageSource) to a data repository?

  • Christoph Fink
    Christoph Fink almost 13 years
    I think that doesn't work if you are using UriSource to load the image, but I'm not shure...
  • Muad'Dib
    Muad'Dib almost 13 years
    @chrfin mmmm, dunno havn't tried it. should work fine for his WPF app, tho
  • SoManyGoblins
    SoManyGoblins almost 13 years
    My StreamSource property on the BitmapImage is null for some reason, any idea why?
  • Sam
    Sam almost 13 years
    @So: because you didn't load it from a stream. This isn't going to work for you.
  • SoManyGoblins
    SoManyGoblins almost 13 years
    Can you provide a code sample? Let's say for simplicity I want PNG (that's the format loaded into the BitmapImage).
  • memory of a dream
    memory of a dream about 10 years
    You forgot a few steps there... you need to use an encoder.
  • Venugopal M
    Venugopal M about 9 years
    @ChrFin: Yes. You are correct. if we use UriSource, StreamSource would be null.
  • Tommy
    Tommy over 2 years
    This works if I've loaded my image in using the UriSource, but if I load it in from a memory stream, at encoder.Save(ms), I get Exception thrown: 'System.InvalidOperationException' in PresentationCore.dll An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationCore.dll Operation is not valid due to the current state of the object. A few of the properties of ms are showing 'System.InvalidOperationException'.
  • Christoph Fink
    Christoph Fink over 2 years
    @Tommy This sounds like some other problem you are facing which maybe just pops up during conversion, because it should work fine... Maybe you can create a new question including you code to reproduce the problem and comment a link here?
  • Tommy
    Tommy over 2 years
    @Christopher Thanks so much. I've posted here