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

 
GeneralMy vote of 5 Pin
adriancs6-Sep-12 20:03
mvaadriancs6-Sep-12 20:03 
GeneralMy vote of 5 Pin
Davide Montorsi15-Apr-12 5:10
professionalDavide Montorsi15-Apr-12 5:10 
GeneralThanks, Pin
Tim Xu chihn11-Apr-12 15:30
Tim Xu chihn11-Apr-12 15:30 
GeneralMy vote of 1 Pin
Sparkling_ouc15-Mar-12 19:53
Sparkling_ouc15-Mar-12 19:53 
GeneralRe: My vote of 1 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:36
rajantawate1(http//www.tawateventures.com16-Mar-12 3:36 
GeneralMy vote of 2 Pin
AndrewB-UK9-Mar-12 4:23
AndrewB-UK9-Mar-12 4:23 
GeneralMy vote of 2 Pin
Gabriel GM26-Jan-12 4:33
Gabriel GM26-Jan-12 4:33 
Questionimage upload from WEb service Pin
spydeehunk10-Jan-12 21:39
spydeehunk10-Jan-12 21:39 
i have andorid client which uploads image to server via WCF. as this client call my method having signature
public string InsertImage(string byteStream). now i am converting this string in array of byte and using yr imageconvertor class to render image by following code
public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms.ToArray());
Response.Write(returnImage);
return returnImage;
}
but m getting exception. can you solve this issue or help me out right approach .
AnswerRe: image upload from WEb service Pin
rajantawate1(http//www.tawateventures.com30-May-12 3:05
rajantawate1(http//www.tawateventures.com30-May-12 3:05 
GeneralRe: image upload from WEb service Pin
tctvijiram30-Aug-12 0:36
tctvijiram30-Aug-12 0:36 
GeneralRe: image upload from WEb service Pin
tctvijiram30-Aug-12 0:41
tctvijiram30-Aug-12 0:41 
GeneralMy vote of 5 Pin
Addon Solutions3-Jan-12 17:49
professionalAddon Solutions3-Jan-12 17:49 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37 
GeneralMy vote of 5 Pin
bennee28-Dec-11 11:19
bennee28-Dec-11 11:19 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37 
GeneralMy vote of 5 Pin
hoolly25-Dec-11 22:33
hoolly25-Dec-11 22:33 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37 
GeneralMy vote of 5 Pin
r_pippi21-Dec-11 3:55
r_pippi21-Dec-11 3:55 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:38
rajantawate1(http//www.tawateventures.com16-Mar-12 3:38 
QuestionQuestion about the quality of image Pin
topazojun11-Dec-11 13:31
topazojun11-Dec-11 13:31 
QuestionColour format Pin
Member 79558496-Jul-11 2:23
Member 79558496-Jul-11 2:23 
GeneralCalling four Function using an If statement Pin
ndeza7-Mar-11 21:05
ndeza7-Mar-11 21:05 
GeneralMy vote of 4 Pin
Xiaosheng Wang1-Mar-11 23:53
Xiaosheng Wang1-Mar-11 23:53 
General5 For my vote Pin
babakzawari1-Mar-11 5:49
babakzawari1-Mar-11 5:49 
GeneralRe: 5 For my vote Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:38
rajantawate1(http//www.tawateventures.com16-Mar-12 3:38 

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.