Click here to Skip to main content
15,398,432 members
Articles / Web Development / HTML
Posted 3 Sep 2006


193 bookmarked

C# Image to Byte Array and Byte Array to Image Converter Class

Rate me:
Please Sign up or sign in to vote.
4.83/5 (116 votes)
28 May 2009CPOL1 min read
C# Helper class to convert image to byte array and byte array to image


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:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
 MemoryStream ms = new MemoryStream();
 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:

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



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

Comments and Discussions

QuestionSome comments Pin
Member 1389978921-Oct-19 5:35
MemberMember 1389978921-Oct-19 5:35 
QuestionError in byte to image method Pin
Member 1407152429-Nov-18 7:27
MemberMember 1407152429-Nov-18 7:27 
PraiseDrawing Pin
Member 1367172310-Feb-18 8:09
MemberMember 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
MemberYury 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
MemberRevathySanthanam2-Dec-15 23:09 
QuestionA little change... Pin
TeknoRapture19-Mar-15 8:08
MemberTeknoRapture19-Mar-15 8:08 
Questionhow to use it ? Pin
thejagat20-Nov-14 0:20
Memberthejagat20-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
Memberthejagat4-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
Memberrichardalgor11-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
Memberkool15th25-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
professionaladriancs3-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
MemberNilesh Moradiya29-Oct-13 23:52 
GeneralGracias Pin
Sergio99923-Oct-13 11:18
MemberSergio99923-Oct-13 11:18 
Questionthanks Pin
mqpabalan21-Oct-13 17:32
Membermqpabalan21-Oct-13 17:32 
QuestionImage Retrieve from access DB using VB 2010 Pin
Member 999520324-May-13 1:29
MemberMember 999520324-May-13 1:29 
GeneralMy vote of 5 Pin
BeamingJo7-May-13 22:27
MemberBeamingJo7-May-13 22:27 
Questionvote 5 Pin
Mahdi_kishislan13-Mar-13 3:29
MemberMahdi_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.