Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I convert an image into an array of bytes. I need to do this in Silverlight.

Please help me!
Posted
Updated 20-Jul-12 1:36am
v2

1 solution

You can do it in various ways. Here are some:

1st way:(By using MemoryStream)
C#
public byte[] imageToByteArray(System.Drawing.Image img)
{
   byte[] byteArray = new byte[0];
   using (MemoryStream stream = new MemoryStream())
   {
      img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
      stream.Close();
      byteArray = stream.ToArray();
   }
   return byteArray;
}


2nd way:(By using System.Drawing.ImageConvertor)
C#
public static byte[] ImageToByte(Image img)
{    
  ImageConverter converter = new ImageConverter();    
  return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

Refer:
C# Image to Byte Array and Byte Array to Image Converter Class[^]
Store or save images in SQL Server database using C#[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900