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

 
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 
GeneralRead bmp image and extract data Pin
sandeeprattu3-Jun-09 1:01
sandeeprattu3-Jun-09 1:01 
GeneralRe: Read bmp image and extract data Pin
Amarnath S29-Jun-09 1:55
professionalAmarnath S29-Jun-09 1:55 
GeneralRe: Read bmp image and extract data Pin
dan2010here4-Sep-09 3:25
dan2010here4-Sep-09 3:25 
QuestionJPEG2000 causing Invalid Param Exceptions from MemoryStream Pin
AJarrett3-Apr-09 10:29
AJarrett3-Apr-09 10:29 
GeneralCreate an image from an byte array Pin
vikaskhan61-Mar-09 3:59
vikaskhan61-Mar-09 3:59 
GeneralStream remains open for some time Pin
culeadragos20-Jan-09 3:36
culeadragos20-Jan-09 3:36 
GeneralThanks Pin
rodo202430-Dec-08 5:18
rodo202430-Dec-08 5:18 
GeneralIn .net 3.5 Pin
ketansnadar27-Nov-08 4:00
ketansnadar27-Nov-08 4:00 
GeneralThanks Pin
basslk20-Nov-08 14:24
basslk20-Nov-08 14:24 
QuestionDoesn't this class come standard with the .NET framework ? Pin
RadiantThunder4-Sep-08 2:00
RadiantThunder4-Sep-08 2:00 
QuestionI am getting an error Argumnet exception on doing this. Pin
sherry chauhan4-May-08 20:05
sherry chauhan4-May-08 20:05 
GeneralDon't think this can be used to convert arbitrary byte array to image. Pin
jroughgarden10-Mar-08 7:29
jroughgarden10-Mar-08 7:29 
GeneralRe: Don't think this can be used to convert arbitrary byte array to image. Pin
EdgarVerona28-Apr-08 7:39
EdgarVerona28-Apr-08 7:39 
GeneralError! [modified] PinPopular
Hemal K20-Nov-07 16:48
Hemal K20-Nov-07 16:48 
GeneralRe: Error! Pin
jroughgarden10-Mar-08 7:04
jroughgarden10-Mar-08 7:04 
GeneralRe: Error! Pin
Lance May4-Aug-08 5:53
Lance May4-Aug-08 5:53 
GeneralThanks! Pin
Thomas Radioyes1-Oct-07 23:38
Thomas Radioyes1-Oct-07 23:38 
Generalto hex Pin
zmrcic6-Aug-07 1:30
zmrcic6-Aug-07 1:30 

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.