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?)

1
  • I'm not sure, but maybe this way will work. Commented Mar 30, 2015 at 16:45

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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();
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.