Click here to Skip to main content
15,884,007 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

 
GeneralRe: to hex Pin
zmrcic7-Aug-07 3:09
zmrcic7-Aug-07 3:09 
GeneralVery Good - Resize and Render Image Code Here Pin
PRRNET12-Jun-07 15:10
PRRNET12-Jun-07 15:10 
GeneralThanks! Pin
Cardinal429-May-07 17:13
Cardinal429-May-07 17:13 
GeneralThanx! Pin
Joe Sonderegger24-May-07 4:59
Joe Sonderegger24-May-07 4:59 
GeneralBetter and Saver Methods Pin
Primadi12-Jan-07 5:55
Primadi12-Jan-07 5:55 
QuestionRe: Better and Saver Methods Pin
jroughgarden10-Mar-08 7:00
jroughgarden10-Mar-08 7:00 
AnswerRe: Better and Saver Methods Pin
downhillchris4-May-09 2:44
downhillchris4-May-09 2:44 
GeneralAnd the point of this 'article' is... Pin
rax_s31-Dec-06 4:45
rax_s31-Dec-06 4:45 
GeneralRe: And the point of this 'article' is... Pin
downhillchris4-May-09 2:26
downhillchris4-May-09 2:26 
GeneralPixel Data Pin
Ennis Ray Lynch, Jr.30-Dec-06 17:11
Ennis Ray Lynch, Jr.30-Dec-06 17:11 
QuestionWhy not any object? Pin
Stuart Konen10-Dec-06 7:21
Stuart Konen10-Dec-06 7:21 
GeneralUnmanaged Code Pin
rrferreira8-Dec-06 14:44
rrferreira8-Dec-06 14:44 
GeneralImage Format Pin
kaptaintens17-Oct-06 10:30
kaptaintens17-Oct-06 10:30 
GeneralRe: Image Format PinPopular
JBurkey17-Oct-06 10:34
JBurkey17-Oct-06 10:34 
GeneralRe: Image Format Pin
CastorTiu2-Nov-06 19:41
CastorTiu2-Nov-06 19:41 
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 
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.