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

By , 28 May 2009
 

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:

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:

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
Seegloo Web Meeting

License

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

About the Author

rajantawate1(http//www.tawateventures.com
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionImage Retrieve from access DB using VB 2010memberMember 999520324 May '13 - 1:29 
Try
                Dim c1 As New OleDb.OleDbDataAdapter(" SELECT VoterID_Image FROM Voter_ID_Card where Record_No='" & TxtBox_IDInfo_RNo.Text & "'", cnn)
                c1.Fill(dt)
 

 

                If dt.Rows.Count > 0 Then
                    'Me.txtName.Text = dt.Rows(0).Item("name") & ""
                    If Not IsDBNull(dt.Rows(0).Item("VoterID_Image")) Then
                        arrImage = dt.Rows(0).Item("VoterID_Image")
                        'Dim Bp As Bitmap = Bitmap(arrImage)
                        For Each ar As Byte In arrImage
                            myMS.WriteByte(ar)
                        Next
                        Me.imgRetrieve.Image = System.Drawing.Image.FromStream(myMS)
                    End If
                End If
 
i'm trying to convert the image bt it is n't working... plz help me....with proper vb code
GeneralMy vote of 5memberBeamingJo7 May '13 - 22:27 
Well done!
 
Thanks a lot for sharing!
Questionvote 5memberMahdi_kishislan13 Mar '13 - 3:29 
many tnx dude
GeneralMy vote of 5memberMember 950756223 Jan '13 - 2:47 
Just what I was looking for. Thanks
GeneralRe: My vote of 5memberrajantawate1(http//www.tawateventures.com30 Jan '13 - 19:34 
cool...thanks for the feedback
Generalvery usefulmembervantyc13 Jan '13 - 18:12 
you save my life, I just needed to save an image from picturebox to an access database and your code works perfect!
GeneralRe: very usefulmemberrajantawate1(http//www.tawateventures.com21 Jan '13 - 23:25 
thanks for your feedback
GeneralMy vote of 4membercsharpbd7 Jan '13 - 22:48 
Nice!!!
GeneralMy vote of 1memberlilen31 Dec '12 - 5:03 
does not work give argument exception
GeneralRe: My vote of 1memberrajantawate1(http//www.tawateventures.com21 Jan '13 - 23:27 
learn to debug your code..see what parameter was being passed
GeneralMy vote of 5memberjalalbabaei1 Oct '12 - 5:54 
it is best for
image to stream
GeneralRe: My vote of 5memberrajantawate1(http//www.tawateventures.com21 Jan '13 - 23:25 
thanks for your feedback
Questionthe byte array inmemberMember 944380027 Sep '12 - 22:59 
is your bytearray oen dimensional???what about two dimensional
 
please help me
best wishes!!!
Questionthe arraymemberMember 944380025 Sep '12 - 3:48 
in your article ,the byte is one dimension.my question is if the byte array has two dimensions,what the code will be ?
GeneralMy vote of 5memberSaesee25 Sep '12 - 1:03 
Helpful
GeneralMy vote of 5memberadriancs6 Sep '12 - 20:03 
Thanks for sharing, this save my breath.
GeneralMy vote of 5memberDavide Montorsi15 Apr '12 - 5:10 
excellent try&use
GeneralThanks,memberTim Xu chihn11 Apr '12 - 15:30 
Did it too easy?
GeneralMy vote of 1groupSparkling_ouc15 Mar '12 - 19:53 
very poor
GeneralRe: My vote of 1memberrajantawate1(http//www.tawateventures.com16 Mar '12 - 3:36 
pity
GeneralMy vote of 2memberAndrewB-UK9 Mar '12 - 4:23 
Many people have pointed this out: you must dispose of your streams. The failure to update your article accordingly, and the fact that there are better solutions, explains my vote.
GeneralMy vote of 2memberGabriel GM26 Jan '12 - 4:33 
It's a good start to convert, but you should always dispose your stream. Why always use Gif instead of native format?
Questionimage upload from WEb servicememberspydeehunk10 Jan '12 - 21:39 
i have andorid client which uploads image to server via WCF. as this client call my method having signature
public string InsertImage(string byteStream). now i am converting this string in array of byte and using yr imageconvertor class to render image by following code
public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms.ToArray());
Response.Write(returnImage);
return returnImage;
}
but m getting exception. can you solve this issue or help me out right approach .
AnswerRe: image upload from WEb servicememberrajantawate1(http//www.tawateventures.com30 May '12 - 3:05 
in send use : image->memorystream->bytearray->base64string
 
in get use : base64string->bytearray->memorystream->image
GeneralRe: image upload from WEb servicemembertctvijiram30 Aug '12 - 0:36 
MemoryStream ms = new MemoryStream(bytedata, 0, len);
System.Drawing.Image photo = System.Drawing.Image.FromStream(ms.ToArray());
 

this coding shows me an error
System.Drawing.Image.FromStream(System.IO.MemoryStream);has some invalid values
 
pls help me
Viji

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 28 May 2009
Article Copyright 2006 by rajantawate1(http//www.tawateventures.com
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid