Click here to Skip to main content
13,247,689 members (21,526 online)
Click here to Skip to main content
Add your own
alternative version


187 bookmarked
Posted 3 Sep 2006

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

QuestionImage Name and its Extenssion Pin
Nilesh Moradiya30-Oct-13 0:52
groupNilesh Moradiya30-Oct-13 0:52 
GeneralGracias Pin
Sergio99923-Oct-13 12:18
memberSergio99923-Oct-13 12:18 
Questionthanks Pin
mqpabalan21-Oct-13 18:32
membermqpabalan21-Oct-13 18:32 
QuestionImage Retrieve from access DB using VB 2010 Pin
Member 999520324-May-13 2:29
memberMember 999520324-May-13 2:29 
                Dim c1 As New OleDb.OleDbDataAdapter(" SELECT VoterID_Image FROM Voter_ID_Card where Record_No='" & TxtBox_IDInfo_RNo.Text & "'", cnn)


                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
                        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 5 Pin
BeamingJo7-May-13 23:27
memberBeamingJo7-May-13 23:27 
Questionvote 5 Pin
Mahdi_kishislan13-Mar-13 4:29
memberMahdi_kishislan13-Mar-13 4:29 
GeneralMy vote of 5 Pin
Member 950756223-Jan-13 3:47
memberMember 950756223-Jan-13 3:47 
GeneralRe: My vote of 5 Pin
rajantawate1(http//www.tawateventures.com30-Jan-13 20:34
memberrajantawate1(http//www.tawateventures.com30-Jan-13 20:34 
Generalvery useful Pin
vantyc13-Jan-13 19:12
membervantyc13-Jan-13 19:12 
GeneralRe: very useful Pin
rajantawate1(http//www.tawateventures.com22-Jan-13 0:25
memberrajantawate1(http//www.tawateventures.com22-Jan-13 0:25 

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 | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 28 May 2009
Article Copyright 2006 by rajantawate1(http//
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid