Click here to Skip to main content
14,176,439 members
Click here to Skip to main content
Add your own
alternative version


191 bookmarked
Posted 3 Sep 2006
Licenced CPOL

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

Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...

Comments and Discussions

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
groupNilesh 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 
GeneralMy vote of 5 Pin
Member 950756223-Jan-13 2:47
memberMember 950756223-Jan-13 2:47 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190525.1 | Last Updated 28 May 2009
Article Copyright 2006 by rajantawate1(http//
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid