Click here to Skip to main content
6,595,444 members and growing! (20,455 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » General License: The Code Project Open License (CPOL)

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

By rajantawate1(http//www.jhatak.com)

C# Helper class to convert image to byte array and byte array to image
C# (C# 2.0)
Version:4 (See All)
Posted:3 Sep 2006
Updated:28 May 2009
Views:164,786
Bookmarked:82 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 6.49 Rating: 4.76 out of 5

1

2
2 votes, 8.7%
3
3 votes, 13.0%
4
18 votes, 78.3%
5

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.jhatak.com)


Member

Location: United States United States

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 45 (Total in Forum: 45) (Refresh)FirstPrevNext
GeneralThank you PinmembertehCoosh13:46 11 Jun '09  
GeneralRead bmp image and extract data Pinmembersandeeprattu2:01 3 Jun '09  
GeneralRe: Read bmp image and extract data PingroupAmarnath S2:55 29 Jun '09  
GeneralRe: Read bmp image and extract data Pinmemberdan2010here4:25 4 Sep '09  
QuestionJPEG2000 causing Invalid Param Exceptions from MemoryStream PinmemberAJarrett11:29 3 Apr '09  
GeneralCreate an image from an byte array Pinmembervikaskhan64:59 1 Mar '09  
GeneralStream remains open for some time Pinmemberculeadragos4:36 20 Jan '09  
GeneralThanks Pinmemberrodo20246:18 30 Dec '08  
GeneralIn .net 3.5 Pinmemberketansnadar5:00 27 Nov '08  
GeneralThanks Pinmemberbasslk15:24 20 Nov '08  
GeneralDoesn't this class come standard with the .NET framework ? PinmemberRadiantThunder3:00 4 Sep '08  
QuestionI am getting an error Argumnet exception on doing this. Pinmembersherry chauhan21:05 4 May '08  
GeneralDon't think this can be used to convert arbitrary byte array to image. Pinmemberjroughgarden8:29 10 Mar '08  
GeneralRe: Don't think this can be used to convert arbitrary byte array to image. PinmemberEdgarVerona8:39 28 Apr '08  
GeneralError! [modified] PinmemberHemal K17:48 20 Nov '07  
GeneralRe: Error! Pinmemberjroughgarden8:04 10 Mar '08  
GeneralRe: Error! PinmemberLance May6:53 4 Aug '08  
GeneralThanks! PinmemberThomas Radioyes0:38 2 Oct '07  
GeneralRe: Thanks! Pinmemberrajantawate1(http//www.jhatak.com)5:41 5 Oct '07  
Generalto hex Pinmemberzmrcic2:30 6 Aug '07  
GeneralRe: to hex Pinmemberzmrcic4:09 7 Aug '07  
GeneralRe: to hex Pinmemberrajantawate1(http//www.jhatak.com)17:46 14 Aug '07  
GeneralVery Good - Resize and Render Image Code Here PinmemberPRRNET16:10 12 Jun '07  
GeneralThanks! PinmemberCardinal418:13 29 May '07  
GeneralThanx! PinmemberNice Life5:59 24 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 May 2009
Editor: Deeksha Shenoy
Copyright 2006 by rajantawate1(http//www.jhatak.com)
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project