1

I found the following code on web to convert WriteableBitmap to byte array but this code does not work with Silverlight. Can someone please tell me what changes are needed to make it work with Silverlight.

byte[] ConvertBitmapToByteArray(WriteableBitmap bitmap)
{
    WriteableBitmap bmp = bitmap;

    using (Stream stream = bmp.PixelBuffer.AsStream())
    {
        MemoryStream memoryStream = new MemoryStream();
        stream.CopyTo(memoryStream);
        return memoryStream.ToArray();
    }
}

It gives the following error message:

'System.Windows.Media.Imaging.WriteableBitmap' does not contain a definition for 'PixelBuffer' and no extension method 'PixelBuffer' accepting a first argument of type 'System.Windows.Media.Imaging.WriteableBitmap' could be found (are you missing a using directive or an assembly reference?)

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
Saira
  • 107
  • 1
  • 12

2 Answers2

3

I used this method in another project. This snippet belongs to sara silva.

public static byte[] ConvertToByteArray(WriteableBitmap writeableBitmap)
{
    using (var ms = new MemoryStream())
    {
        writeableBitmap.SaveJpeg(ms, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);
        return ms.ToArray();
    }
}

msdn documentation

aloisdg
  • 22,270
  • 6
  • 85
  • 105
0

try this :

public static byte[] ConvertToByteArray(WriteableBitmap writeableBitmap)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                writeableBitmap.SaveJpeg(ms, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);

                return ms.ToArray();
            }
        }
puko
  • 2,819
  • 4
  • 18
  • 27