5,693,062 members and growing! (17,045 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » General     Intermediate 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# 2.0, C#, Windows, .NET, .NET 2.0VS.NET2003, VS2005, Visual Studio, Dev

Posted: 3 Sep 2006
Updated: 22 Oct 2007
Views: 84,794
Bookmarked: 40 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 3.32 Rating: 4.75 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 20.0%
3
0 votes, 0.0%
4
4 votes, 80.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Recently I was looking for a class which could convert an System.Drawing.Image to byte[] array and vice versa. After a lot of search 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 memory stream 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 is attached below.

Rajan Tawate

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)



Location: United States United States

Other popular Algorithms & Recipes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralIn .net 3.5memberketansnadar5:00 27 Nov '08  
GeneralThanksmemberbasslk15:24 20 Nov '08  
GeneralDoesn't this class come standard with the .NET framework ?memberRadiantThunder3:00 4 Sep '08  
QuestionI am getting an error Argumnet exception on doing this.membersherry chauhan21:05 4 May '08  
GeneralDon't think this can be used to convert arbitrary byte array to image.memberjroughgarden8:29 10 Mar '08  
GeneralRe: Don't think this can be used to convert arbitrary byte array to image.memberEdgarVerona8:39 28 Apr '08  
GeneralError! [modified]memberHemal K17:48 20 Nov '07  
GeneralRe: Error!memberjroughgarden8:04 10 Mar '08  
GeneralRe: Error!memberLance May6:53 4 Aug '08  
GeneralThanks!memberThomas Radioyes0:38 2 Oct '07  
GeneralRe: Thanks!memberrajantawate1(http//www.jhatak.com)5:41 5 Oct '07  
Generalto hexmemberzmrcic2:30 6 Aug '07  
GeneralRe: to hexmemberzmrcic4:09 7 Aug '07  
GeneralRe: to hexmemberrajantawate1(http//www.jhatak.com)17:46 14 Aug '07  
GeneralVery Good - Resize and Render Image Code HerememberPRRNET16:10 12 Jun '07  
GeneralThanks!memberCardinal418:13 29 May '07  
GeneralThanx!memberNice Life5:59 24 May '07  
GeneralBetter and Saver MethodsmemberPrimadi6:55 12 Jan '07  
QuestionRe: Better and Saver Methodsmemberjroughgarden8:00 10 Mar '08  
GeneralAnd the point of this 'article' is...memberrax_s5:45 31 Dec '06  
GeneralPixel DatamemberEnnis Ray Lynch, Jr.18:11 30 Dec '06  
GeneralWhy not any object?memberStuart Konen8:21 10 Dec '06  
GeneralUnmanaged Codememberrrferreira15:44 8 Dec '06  
GeneralImage Formatmemberdannyres11:30 17 Oct '06  
GeneralRe: Image FormatmemberJBurkey11:34 17 Oct '06  

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

PermaLink | Privacy | Terms of Use
Last Updated: 22 Oct 2007
Editor: Sean Ewington
Copyright 2006 by rajantawate1(http//www.jhatak.com)
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project