Click here to Skip to main content
14,640,678 members
Rate this:
Please Sign up or sign in to 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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

1st way:(By using MemoryStream)
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)
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#[^]
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100