C# Programming > Data

C# Image to Bytes

Image Bytes

Developers can convert an image to a byte array in C# since bitmaps and other images are made up of bytes.

Converting an image to bytes is useful in many scenarios. A byte arrays can be easily compared, compressed, stored, or converted to other data types. For example, programmers can convert an image into a byte array and then convert the array to a string, thus converting an image to a string.

Converting a Bitmap

Converting a bitmap into a byte array is simple with built-in .NET Framework functions. However there is more than one way to do it. We are going to cover two ways to transform a .NET image into a bunch of bytes.

It is important to know how each method works to use it properly.

ImageConverter

The first and easiest way to convert an image to bytes is to use the ImageConverter class under the System.Drawing namespace. The ConvertTo function is not static, so you'll need to create an instance of the class. The function returns an object so the result needs to be converted to the proper type.

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

The thing to remember about ImageConverter, is that the image will be directly converted into bytes. Thus an image in bmp format and the same image in png format will NOT have the same byte array. So if you want to the compare two images for example, you would have to make sure they are first in the same format before comparing their byte arrays.

Memory Stream

The second method takes a little more C# code, but it is potentially more reliable. As you may already know, any .NET Image or Bitmap object has a Save function. The Save function is important because it allows programmers to save an image to a file in any image format supported by the .NET Framework. Even better is that an overload of the Save function allows developers to write to a stream instead of a file.

The trick is to create a MemoryStream from the System.IO namespace. We then call the Save function on the MemoryStream object, while specifying an image format. Since the object is in memory, it can easily be converted into a byte array with the ToArray function from the MemoryStream object:

public static byte[] ImageToByte2(Image img)
{
    byte[] byteArray = new byte[0];
    using (MemoryStream stream = new MemoryStream())
    {
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Close();

        byteArray = stream.ToArray();
    }
    return byteArray;
}

This method is advantageous since we can specify the image format before converting the image to a byte array. That way developers can ensure which format the byte array will be in, making comparisons for example more reliable.

Conclusion

Once an image has been converted to a byte array, as mentioned before, programmers can do many things with it. A byte array is easy to save, convert to other types, and compare with other byte arrays.

Back to C# Article List