Click here to Skip to main content
15,886,362 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
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 
GeneralMy vote of 4 Pin
mehulsant13-Feb-11 19:54
mehulsant13-Feb-11 19:54 
GeneralThanks Pin
Krzysztow29-Oct-10 23:37
Krzysztow29-Oct-10 23:37 
GeneralVoting 1 PinPopular
BeeGone28-Sep-10 0:54
BeeGone28-Sep-10 0:54 
GeneralRe: Voting 1 Pin
Sajan S Jabbar18-Jul-11 1:41
Sajan S Jabbar18-Jul-11 1:41 
GeneralMy vote of 1 Pin
BeeGone28-Sep-10 0:32
BeeGone28-Sep-10 0:32 
GeneralMy vote of 1 PinPopular
Member 450393723-Sep-10 14:31
Member 450393723-Sep-10 14:31 
GeneralRe: My vote of 1 Pin
Tarun.K.S28-Jun-11 23:34
Tarun.K.S28-Jun-11 23:34 
Generalhex string to sql and to website Pin
meenu s9-Aug-10 12:35
meenu s9-Aug-10 12:35 
GeneralRe: hex string to sql and to website Pin
rajantawate1(http//www.tawateventures.com11-Aug-10 3:30
rajantawate1(http//www.tawateventures.com11-Aug-10 3:30 
GeneralRe: hex string to sql and to website Pin
meenu s17-Aug-10 1:20
meenu s17-Aug-10 1:20 
QuestionHey, how about closing your streams? Pin
Yat9-Aug-10 5:17
Yat9-Aug-10 5:17 
GeneralMy vote of 1 Pin
jmzl66621-Jun-10 12:55
jmzl66621-Jun-10 12:55 
GeneralRe: My vote of 1 Pin
Johnson.Sebastian/353771913-Feb-11 0:56
Johnson.Sebastian/353771913-Feb-11 0:56 

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.