Click here to Skip to main content
15,867,704 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.3K   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

 
QuestionSome comments Pin
Member 1389978921-Oct-19 5:35
Member 1389978921-Oct-19 5:35 
QuestionError in byte to image method Pin
Member 1407152429-Nov-18 7:27
Member 1407152429-Nov-18 7:27 
PraiseDrawing Pin
Member 1367172310-Feb-18 8:09
Member 1367172310-Feb-18 8:09 
SuggestionRefactored Code Pin
Ranjan.D31-Jan-16 7:13
professionalRanjan.D31-Jan-16 7:13 
GeneralMy vote of 1 Pin
Yury Shcherbakov19-Dec-15 20:56
Yury Shcherbakov19-Dec-15 20:56 
QuestionGood article Pin
afilho3-Dec-15 6:11
professionalafilho3-Dec-15 6:11 
QuestionByte Array to Image--Error Pin
RevathySanthanam2-Dec-15 23:09
RevathySanthanam2-Dec-15 23:09 
QuestionA little change... Pin
TeknoRapture19-Mar-15 8:08
TeknoRapture19-Mar-15 8:08 
Questionhow to use it ? Pin
thejagat20-Nov-14 0:20
thejagat20-Nov-14 0:20 
AnswerRe: how to use it ? Pin
Aditya Prakash Roy20-Nov-14 0:29
professionalAditya Prakash Roy20-Nov-14 0:29 
GeneralRe: how to use it ? Pin
thejagat4-Dec-14 20:45
thejagat4-Dec-14 20:45 
GeneralRe: how to use it ? Pin
Aditya Prakash Roy5-Dec-14 22:13
professionalAditya Prakash Roy5-Dec-14 22:13 
AnswerRe: how to use it ? Pin
richardalgor11-Feb-15 19:14
richardalgor11-Feb-15 19:14 
QuestionImage to Byte Array converter Pin
Aditya Prakash Roy19-Nov-14 23:31
professionalAditya Prakash Roy19-Nov-14 23:31 
QuestionIn Windows CE Device from vs2008 Image.FromStream is not available, than what to use here ? Pin
kool15th25-Oct-14 6:23
kool15th25-Oct-14 6:23 
GeneralMy vote of 5 Pin
Dave Paras24-Mar-14 2:32
professionalDave Paras24-Mar-14 2:32 
Question[My vote of 1] not great Pin
BillW334-Dec-13 7:37
professionalBillW334-Dec-13 7:37 
AnswerRe: [My vote of 1] not great Pin
adriancs3-Oct-14 20:04
mvaadriancs3-Oct-14 20:04 
GeneralRe: [My vote of 1] not great Pin
BillW337-Oct-14 5:51
professionalBillW337-Oct-14 5:51 
QuestionImage Name and its Extenssion Pin
Nilesh Moradiya29-Oct-13 23:52
Nilesh Moradiya29-Oct-13 23:52 
GeneralGracias Pin
Sergio99923-Oct-13 11:18
Sergio99923-Oct-13 11:18 
Questionthanks Pin
mqpabalan21-Oct-13 17:32
mqpabalan21-Oct-13 17:32 
thanks for this easy to used....
QuestionImage Retrieve from access DB using VB 2010 Pin
Member 999520324-May-13 1:29
Member 999520324-May-13 1:29 
GeneralMy vote of 5 Pin
BeamingJo7-May-13 22:27
BeamingJo7-May-13 22:27 
Questionvote 5 Pin
Mahdi_kishislan13-Mar-13 3:29
Mahdi_kishislan13-Mar-13 3:29 

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.