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

 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com21-Jan-13 23:25
rajantawate1(http//www.tawateventures.com21-Jan-13 23:25 
Questionthe byte array in Pin
Joce.Cui27-Sep-12 22:59
Joce.Cui27-Sep-12 22:59 
Questionthe array Pin
Joce.Cui25-Sep-12 3:48
Joce.Cui25-Sep-12 3:48 
GeneralMy vote of 5 Pin
Saesee25-Sep-12 1:03
Saesee25-Sep-12 1:03 
GeneralMy vote of 5 Pin
adriancs6-Sep-12 20:03
mvaadriancs6-Sep-12 20:03 
GeneralMy vote of 5 Pin
Davide Montorsi15-Apr-12 5:10
professionalDavide Montorsi15-Apr-12 5:10 
GeneralThanks, Pin
Tim Xu chihn11-Apr-12 15:30
Tim Xu chihn11-Apr-12 15:30 
GeneralMy vote of 1 Pin
Sparkling_ouc15-Mar-12 19:53
Sparkling_ouc15-Mar-12 19:53 
very poor
GeneralRe: My vote of 1 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:36
rajantawate1(http//www.tawateventures.com16-Mar-12 3:36 
GeneralMy vote of 2 Pin
AndrewB-UK9-Mar-12 4:23
AndrewB-UK9-Mar-12 4:23 
GeneralMy vote of 2 Pin
Gabriel GM26-Jan-12 4:33
Gabriel GM26-Jan-12 4:33 
Questionimage upload from WEb service Pin
spydeehunk10-Jan-12 21:39
spydeehunk10-Jan-12 21:39 
AnswerRe: image upload from WEb service Pin
rajantawate1(http//www.tawateventures.com30-May-12 3:05
rajantawate1(http//www.tawateventures.com30-May-12 3:05 
GeneralRe: image upload from WEb service Pin
tctvijiram30-Aug-12 0:36
tctvijiram30-Aug-12 0:36 
GeneralRe: image upload from WEb service Pin
tctvijiram30-Aug-12 0:41
tctvijiram30-Aug-12 0:41 
GeneralMy vote of 5 Pin
Addon Solutions3-Jan-12 17:49
professionalAddon Solutions3-Jan-12 17:49 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37 
GeneralMy vote of 5 Pin
bennee28-Dec-11 11:19
bennee28-Dec-11 11:19 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37 
GeneralMy vote of 5 Pin
hoolly25-Dec-11 22:33
hoolly25-Dec-11 22:33 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37
rajantawate1(http//www.tawateventures.com16-Mar-12 3:37 
GeneralMy vote of 5 Pin
r_pippi21-Dec-11 3:55
r_pippi21-Dec-11 3:55 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com16-Mar-12 3:38
rajantawate1(http//www.tawateventures.com16-Mar-12 3:38 
QuestionQuestion about the quality of image Pin
topazojun11-Dec-11 13:31
topazojun11-Dec-11 13:31 
QuestionColour format Pin
Member 79558496-Jul-11 2:23
Member 79558496-Jul-11 2:23 

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.