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
Rate this:
Please Sign up or sign in to vote.
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:

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

No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalDave Paras24-Mar-14 2:32 
Question[My vote of 1] not great PinprofessionalBillW334-Dec-13 7:37 
QuestionImage Name and its Extenssion PingroupNilesh Moradiya29-Oct-13 23:52 
GeneralGracias [modified] PinmemberSergio99923-Oct-13 11:18 
Questionthanks Pinmembermqpabalan21-Oct-13 17:32 
QuestionImage Retrieve from access DB using VB 2010 PinmemberMember 999520324-May-13 1:29 
GeneralMy vote of 5 PinmemberBeamingJo7-May-13 22:27 
Questionvote 5 PinmemberMahdi_kishislan13-Mar-13 3:29 
many tnx dude
GeneralMy vote of 5 PinmemberMember 950756223-Jan-13 2:47 
GeneralRe: My vote of 5 Pinmemberrajantawate1(http//www.tawateventures.com30-Jan-13 19:34 
Generalvery useful Pinmembervantyc13-Jan-13 18:12 
GeneralRe: very useful Pinmemberrajantawate1(http//www.tawateventures.com21-Jan-13 23:25 
GeneralMy vote of 4 Pinmembercsharpbd7-Jan-13 22:48 
GeneralMy vote of 1 Pinmemberlilen31-Dec-12 5:03 
GeneralRe: My vote of 1 Pinmemberrajantawate1(http//www.tawateventures.com21-Jan-13 23:27 
GeneralMy vote of 5 Pinmemberjalalbabaei1-Oct-12 5:54 
GeneralRe: My vote of 5 Pinmemberrajantawate1(http//www.tawateventures.com21-Jan-13 23:25 
Questionthe byte array in PinmemberMember 944380027-Sep-12 22:59 
Questionthe array PinmemberMember 944380025-Sep-12 3:48 
GeneralMy vote of 5 PinmemberSaesee25-Sep-12 1:03 
GeneralMy vote of 5 Pinmemberadriancs6-Sep-12 20:03 
GeneralMy vote of 5 PinmemberDavide Montorsi15-Apr-12 5:10 
GeneralThanks, PinmemberTim Xu chihn11-Apr-12 15:30 
GeneralMy vote of 1 PingroupSparkling_ouc15-Mar-12 19:53 
GeneralRe: My vote of 1 Pinmemberrajantawate1(http//www.tawateventures.com16-Mar-12 3:36 

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 | Mobile
Web03 | 2.8.140721.1 | Last Updated 28 May 2009
Article Copyright 2006 by rajantawate1(http//www.tawateventures.com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid