Click here to Skip to main content
Click here to Skip to main content

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

, 28 May 2009 CPOL
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

Seegloo Web Meeting


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

Questionhow to use it ? Pinmemberthejagat20-Nov-14 1:20 
AnswerRe: how to use it ? PinmemberAditya Prakash Roy20-Nov-14 1:29 
GeneralRe: how to use it ? Pinmemberthejagat4-Dec-14 21:45 
GeneralRe: how to use it ? PinmemberAditya Prakash Roy5-Dec-14 23:13 
QuestionImage to Byte Array converter PinmemberAditya Prakash Roy20-Nov-14 0:31 
QuestionIn Windows CE Device from vs2008 Image.FromStream is not available, than what to use here ? Pinmemberkool15th25-Oct-14 7:23 
GeneralMy vote of 5 PinprofessionalDave Paras24-Mar-14 3:32 
Question[My vote of 1] not great PinprofessionalBillW334-Dec-13 8:37 
AnswerRe: [My vote of 1] not great Pinprofessionaladriancs3-Oct-14 21:04 
GeneralRe: [My vote of 1] not great PinprofessionalBillW337-Oct-14 6:51 
QuestionImage Name and its Extenssion PingroupNilesh Moradiya30-Oct-13 0:52 
GeneralGracias [modified] PinmemberSergio99923-Oct-13 12:18 
Questionthanks Pinmembermqpabalan21-Oct-13 18:32 
QuestionImage Retrieve from access DB using VB 2010 PinmemberMember 999520324-May-13 2:29 
GeneralMy vote of 5 PinmemberBeamingJo7-May-13 23:27 
Questionvote 5 PinmemberMahdi_kishislan13-Mar-13 4:29 
GeneralMy vote of 5 PinmemberMember 950756223-Jan-13 3:47 
GeneralRe: My vote of 5 Pinmemberrajantawate1(http//www.tawateventures.com30-Jan-13 20:34 
cool...thanks for the feedback
Generalvery useful Pinmembervantyc13-Jan-13 19:12 
GeneralRe: very useful Pinmemberrajantawate1(http//www.tawateventures.com22-Jan-13 0:25 
GeneralMy vote of 4 Pinmembercsharpbd7-Jan-13 23:48 
GeneralMy vote of 1 Pinmemberlilen31-Dec-12 6:03 
GeneralRe: My vote of 1 Pinmemberrajantawate1(http//www.tawateventures.com22-Jan-13 0:27 
GeneralMy vote of 5 Pinmemberjalalbabaei1-Oct-12 6:54 
GeneralRe: My vote of 5 Pinmemberrajantawate1(http//www.tawateventures.com22-Jan-13 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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