0

Like in the title. How to convert BitmapeImage to byte in metro style app. Ther is no System.drawing lib. I need it to use in LuminanaceSource() which need source in byte[]. Have some example, but i dont know how to use it.

BitmapImage bmpImage = new BitmapImage(new Uri(file.Path));
   var encoderId = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId;

Thanks

Faron
  • 123
  • 14
  • possible duplicate of [Convert ImageSource to WriteableBitmap in Metro Windows 8](http://stackoverflow.com/questions/11797915/convert-imagesource-to-writeablebitmap-in-metro-windows-8) – Mark Aug 22 '14 at 09:31

1 Answers1

0

these two methods should convert Bitmap image to byte[] and viceversa.

public BitmapImage ImageFromBuffer(Byte[] bytes)
{
    MemoryStream stream = new MemoryStream(bytes);
    BitmapImage image = new BitmapImage();
    image.BeginInit();
    image.StreamSource = stream;
    image.EndInit();
    return image;
}

public Byte[] BufferFromImage(BitmapImage imageSource)
{
    Stream stream = imageSource.StreamSource;
    Byte[] buffer = null;
    if (stream != null && stream.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            buffer = br.ReadBytes((Int32)stream.Length);
        }
    }

    return buffer;
}
Marco Cadei
  • 145
  • 2
  • 4
  • 14
  • In Steram stream = imageSource.StreamSource i cant use StreamSource, don't know why. Any ideas? – Faron Aug 22 '14 at 09:41