Click here to Skip to main content
15,868,340 members
Articles / Web Development / HTML

C# Image to Byte Array and Byte Array to Image Converter Class

Rate me:
Please Sign up or sign in to vote.
4.84/5 (119 votes)
28 May 2009CPOL1 min read 1.6M   25.4K   192   124
C# Helper class to convert image to byte array and byte array to image

Introduction

Recently I was looking for a class which could convert a System.Drawing.Image to byte[] array and vice versa. After a lot of searching on Google, I realised that it would be faster for me to write this class and also share it with the community.

The class which I wrote is called ImageConverter.cs. The class has two methods.

First method: Convert Image to byte[] array:

C#
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}

This method uses the System.Drawing.Image.Save method to save the image to a memorystream. The memorystream can then be used to return a byte array using the ToArray() method in the MemoryStream class.

Second method: Convert byte[] array to Image:

C#
public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

This method uses the Image.FromStream method in the Image class to create a method from a memorystream which has been created using a byte array. The image thus created is returned in this method.

The way I happen to use this method was to transport an image to a web service, by converting it to a byte array and vice-versa.

Hope this class is useful to the community as well. The code of ImageConverter.cs can be downloaded from the link at the top of this article.

Rajan Tawate

Founder

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCreativity Pin
Ri Qen-Sin13-Sep-06 9:02
Ri Qen-Sin13-Sep-06 9:02 
GeneralArray to image Pin
Pugwash200411-Sep-06 3:41
Pugwash200411-Sep-06 3:41 
GeneralRe: Array to image Pin
rajantawate1(http//www.tawateventures.com11-Sep-06 10:51
rajantawate1(http//www.tawateventures.com11-Sep-06 10:51 
GeneralRe: Array to image Pin
SetaSensei28-Sep-06 6:03
SetaSensei28-Sep-06 6:03 
GeneralOne More Step Pin
Polymorpher9-Sep-06 10:03
Polymorpher9-Sep-06 10:03 
GeneralRe: One More Step Pin
Grimolfr13-Sep-06 1:10
Grimolfr13-Sep-06 1:10 
GeneralArticle formatting Pin
Colin Angus Mackay4-Sep-06 4:48
Colin Angus Mackay4-Sep-06 4:48 
GeneralRe: Article formatting Pin
rajantawate1(http//www.tawateventures.com11-Oct-06 5:24
rajantawate1(http//www.tawateventures.com11-Oct-06 5:24 
Colin,

I have implemented some of your suggestions.

Thanks,

www.jhatak.com
GeneralRe: Article formatting Pin
Colin Angus Mackay11-Oct-06 7:09
Colin Angus Mackay11-Oct-06 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.