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

 
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 
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 
GeneralMy vote of 5 Pin
Member 950756223-Jan-13 2:47
Member 950756223-Jan-13 2:47 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com30-Jan-13 19:34
rajantawate1(http//www.tawateventures.com30-Jan-13 19:34 
cool...thanks for the feedback
Generalvery useful Pin
vantyc13-Jan-13 18:12
vantyc13-Jan-13 18:12 
GeneralRe: very useful Pin
rajantawate1(http//www.tawateventures.com21-Jan-13 23:25
rajantawate1(http//www.tawateventures.com21-Jan-13 23:25 
GeneralMy vote of 4 Pin
csharpbd7-Jan-13 22:48
professionalcsharpbd7-Jan-13 22:48 
GeneralMy vote of 1 Pin
lilen31-Dec-12 5:03
lilen31-Dec-12 5:03 
GeneralRe: My vote of 1 Pin
rajantawate1(http//www.tawateventures.com21-Jan-13 23:27
rajantawate1(http//www.tawateventures.com21-Jan-13 23:27 
GeneralMy vote of 5 Pin
jalalbabaei1-Oct-12 5:54
jalalbabaei1-Oct-12 5:54 
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 
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 

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.