Image to Byte Array C# , VB.Net
In many situations you may forced to convert image to byte array. It is useful in many scenarios because byte arrays can be easily compared, compressed, stored, or converted to other data types. You can make this conversion in many ways, but here you can see the fastest and memory efficient conversion in two ways.
Using MemoryStream
The Image object has a save function which allows developers to save an image to a file in any image format supported by the .NET Framework. Here this save function applied 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.
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
Both MemoryStream and Image have a dispose method, make sure you are Disposing of them as this can cause MemoryLeaks. Here we use the using blocks which ensures that it calls Dispose() method after the using-block is over, even if the code throws an exception. Also here use a RawFormat property of Image parameter which returns the file format of the image.
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
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
NEXT.....Numeric Only TextBox Control