1

是否可以从 ushort 数组创建 BitmapImage?如果是这样怎么办?

在我创建位图的那一刻,将其转换为位图数组并显示它,但这太慢了,我需要不断更新图像(实时视频源),而每一帧都在创建 UI studders,这视频运行时使我的应用程序非常慢。所以我需要尽快将我的 ushort[] 放入 BitmapImage

谢谢,埃蒙

4

2 回答 2

2

在这里,您有一个如何通过 MemoryStream 获取 BitmapImage 的示例,这可能会对您有所帮助

您可以使用BitConverter将 ushorts 转换为字节以输入到 MemoryStream

于 2011-03-21T18:28:50.190 回答
1

假设您正在使用介于之间的值0255您可以将其转换为字节数组,然后将其加载到MemoryStream

// Please note that with values higher than 255 the program will throw an exception
checked
{
    ushort[] values = { 200, 100, 30/*, 256*/ };
    var bytes = (from value in values
                 select (byte)value).ToArray();

    // Taken from: http://stackoverflow.com/questions/5346727/wpf-convert-memory-stream-to-bitmapimage
    using (var stream = new MemoryStream(data))
    {
        var bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.StreamSource = stream;
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.EndInit();
        bitmap.Freeze();
    }
}
于 2011-03-21T18:49:51.363 回答