Image to Byte Array

In numerous situations, converting an image to a byte array becomes a necessity. This conversion proves highly useful in a variety of scenarios due to the versatility of byte arrays. They offer advantages such as easy comparison, compression, storage, and effortless conversion to other data types. There exist several methods to perform this conversion, but in this context, we present the two fastest and most memory-efficient approaches to achieve this task.

Convert Image to Byte Array and Byte Array to Image

Converting images to byte arrays, developers can access and manipulate the image data directly in a compact and convenient format. This allows for streamlined processing and transmission of image data, making it suitable for various applications, such as image compression, data storage, and transmission over networks. Implementing the fastest and memory-efficient conversion methods ensures optimal performance and resource utilization, ultimately leading to enhanced overall efficiency in handling image data.

Using MemoryStream

The Image object in the .NET Framework provides a convenient save function that enables developers to preserve an image to a file in various supported image formats. In this context, the save function is applied to the MemoryStream object, with the option to specify an image format. Since the MemoryStream object resides in memory, it facilitates easy conversion into a byte array using the ToArray function, which is a method available to the MemoryStream object.

This approach proves advantageous when there's a need to process or transmit image data without physically writing it to a file. By utilizing the MemoryStream object, developers can effectively manipulate and store image data in memory, without the overhead of disk operations. The subsequent conversion to a byte array using the ToArray function further simplifies the handling of image data, allowing for effortless comparison, compression, or transmission to other data types or storage mechanisms.

C#
using (MemoryStream mStream = new MemoryStream()) { img.Save(mStream, img.RawFormat); return mStream.ToArray(); }
VB.Net
Using mStream As New MemoryStream() img.Save(mStream, img.RawFormat) Return mStream.ToArray() End Using
C# Convert Image to Byte Array

It is essential to handle resource cleanup properly to avoid potential memory leaks when working with MemoryStream and Image objects. Both these objects implement the IDisposable interface, and therefore, it is crucial to dispose of them correctly.

To ensure proper disposal and prevent memory leaks, it is recommended to use the "using" statement, which automatically calls the Dispose() method on objects implementing IDisposable after the block is executed, even if an exception occurs.

Additionally, we can make use of the RawFormat property of the Image object, which provides information about the file format of the image. This property returns an ImageFormat object that can be used to specify the image format when saving the image to a file.

Using ImageConverter

ImageConverter class can be used to convert Image objects from one data type to another.


C#
ImageConverter imgCon = new ImageConverter(); return (byte[])imgCon.ConvertTo(inImg, typeof(byte[]));

VB.Net
Dim imgCon As New ImageConverter() Return DirectCast(imgCon.ConvertTo(inImg, GetType(Byte())), Byte())

Convert ByteArray to Image


C#
using (MemoryStream mStream = new MemoryStream(byteArrayIn)) { return Image.FromStream(mStream); }

VB.Net
Using mStream As New MemoryStream(byteArrayIn) Return Image.FromStream(mStream) End Using
VB.Net Convert Image to Byte Array

The following program first convert an Image to ByteArray and then convert that byteArray to Image and loads in a picture box.


Source Code | C#
using System; using System.Drawing; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //image to byteArray Image img = Image.FromFile("d:\\bank-copy.png"); byte[] bArr = imgToByteArray(img); //byte[] bArr = imgToByteConverter(img); //Again convert byteArray to image and displayed in a picturebox Image img1 = byteArrayToImage(bArr); pictureBox1.Image = img1; } //convert image to bytearray public byte[] imgToByteArray(Image img) { using (MemoryStream mStream = new MemoryStream()) { img.Save(mStream, img.RawFormat); return mStream.ToArray(); } } //convert bytearray to image public Image byteArrayToImage(byte[] byteArrayIn) { using (MemoryStream mStream = new MemoryStream(byteArrayIn)) { return Image.FromStream(mStream); } } //another easy way to convert image to bytearray public static byte[] imgToByteConverter(Image inImg) { ImageConverter imgCon = new ImageConverter(); return (byte[])imgCon.ConvertTo(inImg, typeof(byte[])); } } }

Source Code | Vb.Net
Imports System.IO Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'image to byteArray Dim img As Image = Image.FromFile("d:\bank-copy.png") Dim bArr As Byte() = imgToByteArray(img) 'Dim bArr As Byte() = imgToByteConverter(img) 'Again convert byteArray to image and displayed in a picturebox Dim img1 As Image = byteArrayToImage(bArr) PictureBox1.Image = img1 End Sub 'convert image to bytearray Public Function imgToByteArray(ByVal img As Image) As Byte() Using mStream As New MemoryStream() img.Save(mStream, img.RawFormat) Return mStream.ToArray() End Using End Function 'convert bytearray to image Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image Using mStream As New MemoryStream(byteArrayIn) Return Image.FromStream(mStream) End Using End Function 'another easy way to convert image to bytearray Public Shared Function imgToByteConverter(ByVal inImg As Image) As Byte() Dim imgCon As New ImageConverter() Return DirectCast(imgCon.ConvertTo(inImg, GetType(Byte())), Byte()) End Function End Class

Conclusion

Converting an image to a byte array in C# involves utilizing the Image object's Save function on a MemoryStream and then using the ToArray method from the MemoryStream object to obtain the byte array, allowing for efficient handling and manipulation of image data.