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

 
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 
Even if you call all your friends to vote for you 5 points, the code quality remains 0 (zero).

The function how it should've look:
public byte[] ImageToByteArray( System.Drawing.Image p_ImageIn )
{
    byte[] aRet = null;

    using ( System.IO.MemoryStream oMS = new System.IO.MemoryStream() )
    {
        p_ImageIn.Save( oMS, System.Drawing.Imaging.ImageFormat.Gif );
        aRet = oMS.ToArray();
    }

    // Possibly dispose image too here, if no extra manipulation is needed
    //      Remember, Image is sent by ref, so if you dispose it inside method it will not be available outside either.
    // p_ImageIn.Dispose();

    return aRet;
}

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 
GeneralHelp Required in using StreamCoder's RTP.Net Pin
SairaHamid9-May-10 22:25
SairaHamid9-May-10 22:25 
GeneralConvert Byte to bitmap Pin
Tomic20-Feb-10 1:20
Tomic20-Feb-10 1:20 
Generalthanks Pin
zhangqz27-Jan-10 16:30
zhangqz27-Jan-10 16:30 
AnswerGood work. Pin
dhami_naresh23-Dec-09 19:32
dhami_naresh23-Dec-09 19:32 
GeneralNice One Pin
Anurag Gandhi25-Nov-09 22:11
professionalAnurag Gandhi25-Nov-09 22:11 
GeneralPlease help me! Pin
mohammadftm.er6425-Nov-09 20:39
mohammadftm.er6425-Nov-09 20:39 
GeneralThank you Pin
tehCoosh11-Jun-09 12:46
tehCoosh11-Jun-09 12:46 

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.