Image to byte array and vice-versa

This is an old code snippet I posted on the www.developpez.com faq a few years ago.

I needed it today, and I decided to post it here

Image to byte array :


public static Byte[] ImageToByteArray(Image img)
{
try
{
MemoryStream mstImage = new MemoryStream();
img.Save(mstImage, System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] bytImage = mstImage.GetBuffer();
return bytImage;
}
catch (Exception ex)
{
// do something smart
return null;
}
}

Image to Byte array


public static Image ByteArrayToImage(Byte[] BArray)
{
try
{
MemoryStream mstImage = new MemoryStream(BArray);
Image img = Image.FromStream(mstImage);
return img;
}
catch (Exception ex)
{
// do something smart
return null;
}
}

http://dotnet.developpez.com/faq/csharp/?page=gdiplus#gdip_convert_image_bytes

Incoming search terms:

  • memorystream from controls image
  • convert image to byte[] in wp7
  • mstimage
  • byte array to writeablebitmap
  • byte[] to writablebitmap wp8
  • convert byte array to writeablebitmap net
  • csharp image to byte array
  • writeablebitmap to byte array wp8
  • how to bind image from byte in C# net
  • how to use the system windows media imaging to convert an image to byte[]

About Sam Beauvois

Application Developer, .NET enthusiast since 2004, I'm interested in technology watch, usability, code quality, patterns & practices, UX, ...

5 comments

  1. Worke says:

    Thank you. It helps me.

  2. yudong says:

    I used your method to try to transform System.Windows.Controls.Image to Bytes Array
    Howerer,
    bmp.SaveJpeg(mstImage, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
    —–SaveJpeg do not exists in my wpf progect.
    Are you sure it can work in your progect when testing?

  3. admin says:

    Hi
    This code won’t work with wp7 because wp7 doesn’t have the System.Drawing.Image object.

    To make it work with wp7, use the WriteableBitmap object

    I didn’t tested but it should be something like :

    using System.Windows.Media.Imaging;

    public static Byte[] ImageToByteArray(Image img)// this is the System.Windows.Controls.Image object
    {
    try
    {
    MemoryStream mstImage = new MemoryStream();
    System.Windows.Media.Imaging.WriteableBitmap bmp = new System.Windows.Media.Imaging.WriteableBitmap(img.Source as System.Windows.Media.Imaging.BitmapSource); // not tested
    bmp.SaveJpeg(mstImage, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
    Byte[] bytImage = mstImage.GetBuffer();
    return bytImage;
    }
    catch (Exception ex)
    {
    // do something smart
    return null;
    }
    }

    public static WriteableBitmap ByteArrayToImage(Byte[] BArray)
    {
    try
    {
    MemoryStream mstImage = new MemoryStream(BArray);
    System.Windows.Media.Imaging.WriteableBitmap bmp = new System.Windows.Media.Imaging.WriteableBitmap(1, 1);
    bmp.SetSource(mstImage);
    return bmp;
    }
    catch (Exception ex)
    {
    // do something smart
    return null;
    }
    }

  4. Randy says:

    hi, is this code workable with wp 7?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>