将WriteableBitmap转换为Byte数组 - Windows Phone 8.1 - Silverlight

时间:2015-03-30 16:34:49

标签: c# silverlight windows-phone-8.1

我在Web上发现以下代码将WriteableBitmap转换为字节数组,但此代码不适用于Silverlight。有人可以告诉我需要哪些更改才能使它与Silverlight一起使用。

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

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

它给出以下错误消息:

  

' System.Windows.Media.Imaging.WriteableBitmap'不包含' PixelBuffer'的定义没有扩展方法' PixelBuffer'接受类型' System.Windows.Media.Imaging.WriteableBitmap'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

2 个答案:

答案 0 :(得分:3)

我在另一个项目中使用了这个方法。此代码段属于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

答案 1 :(得分:0)

试试这个:

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

                return ms.ToArray();
            }
        }