Click here to Skip to main content
15,881,812 members
Articles / Web Development / HTML

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

Rate me:
Please Sign up or sign in to vote.
4.84/5 (119 votes)
28 May 2009CPOL1 min read 1.6M   25.4K   192  
C# Helper class to convert image to byte array and byte array to image
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>New Page...</title><!--C1 -->
<meta http-equiv="Content-Type" content="text/html; CHARSET=Windows-1252">
<meta name="Generator" content="Ezypage 9.58 : Corporate - ImageConverter.pge              UN-REGISTERED VERSION">


</head>

<body link="#0000FF" alink="#FF0000" vlink="#800080" Scroll=yes bgcolor="#FFFFFF" >

<Div align="Left" style="Font-Family: Arial; Font-Size:12px; Left:1px; Width:697px; POSITION: absolute; Top:0px;">
<font color="#000000"><br><b>Introduction<br></b>Image to byte array class:<br><br>Recently I was looking for a class which could convert an System.Drawing.Image to byte[] array and vice versa.&nbsp;&nbsp;After<br>a lot of search on google I realised that it would be faster for me to write this class and also share<br>it with the community.<br><br><br>The class which I wrote is called ImageConverter.cs<br><br>The class has two methods<br><br>First Method : Convert Image to byte[] array<br><br>public byte[] imageToByteArray(System.Drawing.Image imageIn)<br>		{<br>			MemoryStream ms = new MemoryStream();<br>			imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);<br>			return&nbsp;&nbsp;ms.ToArray();<br>		}<br><br><br>This method uses the System.Drawing.Image.Save method to save the image to a memorystream.<br>The memory stream can then be used to return a byte array using the .ToArray() method in the<br>MemoryStream class.<br><br>Second Method : Convert byte[] array to Image<br><br>public Image byteArrayToImage(byte[] byteArrayIn)<br>		{<br>			MemoryStream ms = new MemoryStream(byteArrayIn);<br>			Image returnImage = Image.FromStream(ms);<br>			return returnImage;<br>	<br>		}<br><br>This method uses the Image.FromStream method in the Image class to create a method from<br>a&nbsp;&nbsp;memorystream which has been created using a byte array.&nbsp;&nbsp;The image thus created is returned<br>in this method.<br><br>The way I happen to use this method was to transport an image to a web service, by converting it<br>to a byte array and vice-versa.<br><br>Hope this class is useful to the community as well.&nbsp;&nbsp;The code of ImageConverter.cs is attached below.<br><br>Rajan Tawate</font>
</Div>


<a name="Page1">
<Div align="Left" style="Font-Family: Arial; Font-Size:12px; Left:1px; Width:697px; POSITION: absolute; Top:0px;">
<font color="#000000"><br><b>Introduction<br></b>Image to byte array class:<br><br>Recently I was looking for a class which could convert an System.Drawing.Image to byte[] array and vice versa.&nbsp;&nbsp;After<br>a lot of search on google I realised that it would be faster for me to write this class and also share<br>it with the community.<br><br><br>The class which I wrote is called ImageConverter.cs<br><br>The class has two methods<br><br>First Method : Convert Image to byte[] array<br><br>public byte[] imageToByteArray(System.Drawing.Image imageIn)<br>		{<br>			MemoryStream ms = new MemoryStream();<br>			imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);<br>			return&nbsp;&nbsp;ms.ToArray();<br>		}<br><br><br>This method uses the System.Drawing.Image.Save method to save the image to a memorystream.<br>The memory stream can then be used to return a byte array using the .ToArray() method in the<br>MemoryStream class.<br><br>Second Method : Convert byte[] array to Image<br><br>public Image byteArrayToImage(byte[] byteArrayIn)<br>		{<br>			MemoryStream ms = new MemoryStream(byteArrayIn);<br>			Image returnImage = Image.FromStream(ms);<br>			return returnImage;<br>	<br>		}<br><br>This method uses the Image.FromStream method in the Image class to create a method from<br>a&nbsp;&nbsp;memorystream which has been created using a byte array.&nbsp;&nbsp;The image thus created is returned<br>in this method.<br><br>The way I happen to use this method was to transport an image to a web service, by converting it<br>to a byte array and vice-versa.<br><br>Hope this class is useful to the community as well.&nbsp;&nbsp;The code of ImageConverter.cs is attached below.<br><br>Rajan Tawate</font>
</Div>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions