42

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?

casperOne
  • 73,706
  • 19
  • 184
  • 253
SoManyGoblins
  • 5,605
  • 8
  • 45
  • 65

5 Answers5

71

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.

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • 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'. – Tommy Nov 01 '21 at 23:22
  • 1
    @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? – Christoph Fink Nov 02 '21 at 06:46
  • @Christopher Thanks so much. I've posted [here](https://stackoverflow.com/questions/69812386/saving-a-bitmapimage-to-file-works-when-the-image-has-been-imported-from-file-b) – Tommy Nov 02 '21 at 14:52
6

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.

casperOne
  • 73,706
  • 19
  • 184
  • 253
-1

write it to a MemoryStream, then you can access the bytes from there. something kinda like this:

public Byte[] ImageToByte(BitmapImage imageSource)
{
    Stream stream = imageSource.StreamSource;
    Byte[] buffer = null;
    if (stream != null && stream.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            buffer = br.ReadBytes((Int32)stream.Length);
        }
    }

    return buffer;
}
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
-2

You can give a formate of bitmap:

Image bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics.FromImage(bmp).CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.PrimaryScreen.Bounds.Size);

MemoryStream m = new MemoryStream();
bmp.Save(m, System.Drawing.Imaging.ImageFormat.Png);
byte[] imageBytes = m.ToArray();
string base64String = Convert.ToBase64String(imageBytes);

Gavin
  • 67
  • 2
-7

Just use a MemoryStream.


byte[] data = null;

using(MemoryStream ms = new MemoryStream())
{
    bitmapImage.Save(ms);
    data = ms.ToArray();
}


bdowden
  • 993
  • 7
  • 11