Coder Perfect

Make a byte array out of a bitmap.


Is there a better way to convert a Windows Bitmap to a byte[] in C# than writing the output to a temporary file and reading it using a FileStream?

Asked by Jeremy McGee

Solution #1

There are a few options.


public static byte[] ImageToByte(Image img)
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));

This one is handy because it doesn’t necessitate a lot of coding.

Memory Stream

public static byte[] ImageToByte2(Image img)
    using (var stream = new MemoryStream())
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        return stream.ToArray();

This one is the same as the last one, except the file is stored to memory rather than to disk. Although there is more code, ImageFormat is an option that can be simply updated to save to memory or disk.


Answered by prestomanifesto

Solution #2

This is when a MemoryStream comes in handy. You might include it in a method extension:

public static class ImageExtensions
    public static byte[] ToByteArray(this Image image, ImageFormat format)
        using(MemoryStream ms = new MemoryStream())
            image.Save(ms, format);
            return ms.ToArray();

It might simply be written as:

var image = new Bitmap(10, 10);
// Draw your image
byte[] arr = image.ToByteArray(ImageFormat.Bmp);

In regards to the ImageConverter, I partially disagree with prestomanifto’s response. ImageConverter should not be used. There’s nothing technically wrong with it, but the fact that it uses object boxing/unboxing tells me it’s code from the.NET framework’s dark ages, and it’s not ideal for image processing (at the very least, it’s overkill for converting to a byte[]), especially when you consider the following.

I looked at the.Net framework’s ImageConverter function, and it utilizes code that is nearly identical to the one I posted above. It starts a new MemoryStream, saves the Bitmap in the format you provided it with, and returns the array. By using MemoryStream, you may avoid the added overhead of building an ImageConverter class.

Answered by Christopher Currens

Solution #3

You can just just use the word Marshal. Make a copy of the bitmap data. There is no need for an intermediary memorystream, etc., and the memory copy is quick. Both 24-bit and 32-bit bitmaps should work with this.

public static byte[] BitmapToByteArray(Bitmap bitmap)

    BitmapData bmpdata = null;

        bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
        int numbytes = bmpdata.Stride * bitmap.Height;
        byte[] bytedata = new byte[numbytes];
        IntPtr ptr = bmpdata.Scan0;

        Marshal.Copy(ptr, bytedata, 0, numbytes);

        return bytedata;
        if (bmpdata != null)



Answered by deegee

Solution #4

Grab the byte array after saving the image to a MemoryStream.

  Byte[] data;

  using (var memoryStream = new MemoryStream())
    image.Save(memoryStream, ImageFormat.Bmp);

    data = memoryStream.ToArray();

Answered by Chris Baxter

Solution #5

Instead of a FileStream, use a MemoryStream like this:

MemoryStream ms = new MemoryStream();
bmp.Save (ms, ImageFormat.Jpeg);
byte[] bmpBytes = ms.ToArray();

Answered by Jeff Reddy

Post is based on