3

根据this,可以使用以下代码将字节数组转换为BitmapImage:

public static async Task<BitmapImage> ByteArrayToBitmapImage(this byte[] byteArray)
{
    if (byteArray != null)
    {
        using (var stream = new InMemoryRandomAccessStream())
        {
            await stream.WriteAsync(byteArray.AsBuffer());
            var image = new BitmapImage();
            stream.Seek(0);
            image.SetSource(stream);
            return image;
        }
    }
    return null;
}

但是,我知道,“ ‘System.Array’不包含‘AsBuffer’的定义,并且找不到接受‘System.Array’类型的第一个参数的扩展方法‘AsBuffer’(您是否缺少 using 指令或装配参考?)

是不是“var stream”分配太模糊(隐式输入),我需要为“stream”var 指定特定的数据类型?System.Array 以外的东西?

也许这,来自“简洁的 Windows 应用商店”是一个线索: 缓冲区/字节数组—System.Runtime.InteropServices.WindowsRuntime。WindowsRuntimeBufferExtensions:此类中的扩展方法提供了在 .NET 字节数组和 WinRT 缓冲区内容之间移动的方法,公开为 IBuffer 实现。

...但如果是,那还不足以让我知道如何处理它。而不是“TMI”,而是“NEI”(信息不足)。

4

1 回答 1

8

问题是编译器没有找到扩展方法 AsBuffer()。确保您有对命名空间的引用System.Runtime.InteropServices.WindowsRuntime,即

using System.Runtime.InteropServices.WindowsRuntime;

您还需要添加对适当 DLL 的引用,如果您还没有:

命名空间:System.Runtime.InteropServices.WindowsRuntime

程序集:System.Runtime.WindowsRuntime(在 System.Runtime.WindowsRuntime.dll 中)

于 2014-10-24T00:07:28.960 回答