Click here to Skip to main content
Licence CPOL
First Posted 3 Sep 2006
Views 480,406
Downloads 6,021
Bookmarked 136 times

C# Image to Byte Array and Byte Array to Image Converter Class

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

rajantawate1(http//www.tawateventures.com



United States United States

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberDavide Montorsi5:10 15 Apr '12  
GeneralThanks, PinmemberTim Xu chihn15:30 11 Apr '12  
GeneralMy vote of 1 PingroupSparkling_ouc19:53 15 Mar '12  
GeneralRe: My vote of 1 Pinmemberrajantawate1(http//www.tawateventures.com3:36 16 Mar '12  
GeneralMy vote of 2 PinmemberAndrewB-UK4:23 9 Mar '12  
GeneralMy vote of 2 PinmemberGabriel GM4:33 26 Jan '12  
Questionimage upload from WEb service Pinmemberspydeehunk21:39 10 Jan '12  
GeneralMy vote of 5 PinmemberMember 772320517:49 3 Jan '12  
GeneralRe: My vote of 5 Pinmemberrajantawate1(http//www.tawateventures.com3:37 16 Mar '12  
GeneralMy vote of 5 Pinmemberbennee11:19 28 Dec '11  
GeneralRe: My vote of 5 Pinmemberrajantawate1(http//www.tawateventures.com3:37 16 Mar '12  
GeneralMy vote of 5 Pinmemberhoolly22:33 25 Dec '11  
GeneralRe: My vote of 5 Pinmemberrajantawate1(http//www.tawateventures.com3:37 16 Mar '12  
GeneralMy vote of 5 Pinmemberr_pippi3:55 21 Dec '11  
GeneralRe: My vote of 5 Pinmemberrajantawate1(http//www.tawateventures.com3:38 16 Mar '12  
QuestionQuestion about the quality of image Pinmembertopazojun13:31 11 Dec '11  
QuestionColour format PinmemberMember 79558492:23 6 Jul '11  
GeneralCalling four Function using an If statement Pinmemberndeza21:05 7 Mar '11  
GeneralMy vote of 4 PinmemberXiaosheng Wang23:53 1 Mar '11  
General5 For my vote Pinmemberbabakzawari5:49 1 Mar '11  
GeneralRe: 5 For my vote Pinmemberrajantawate1(http//www.tawateventures.com3:38 16 Mar '12  
GeneralMy vote of 4 Pinmembermehulsant19:54 13 Feb '11  
GeneralThanks PinmemberKrzysztow23:37 29 Oct '10  
GeneralVoting 1 PinPopularmemberBeeGone0:54 28 Sep '10  
GeneralRe: Voting 1 PinmemberSajan S Jabbar1:41 18 Jul '11  

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.

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