2011-03-21 43 views
1

是否可以从一个ushort数组创建一个BitmapImage?如果是的话如何?从ushort创建一个BitmapImage []

当我创建一个位图,将其转换为位图数组并显示它,但这太慢,我需要不断更新图像(实时视频源),而每个帧正在创建用户界面试玩者,这使得我的应用在视频运行时非常缓慢。所以,我需要让我的USHORT []成的BitmapImage尽可能快

感谢, 埃蒙·

+4

什么在阵列中? – SLaks 2011-03-21 18:25:06

+0

图像数据,从0到65000的数字代表灰度图像 – 2011-03-22 10:17:56

+0

男人,这是您提问时应该出现在您的问题中的信息!记住这个为你的下一个问题 – 2011-03-22 10:48:08

回答

1

假设你有0之间的值工作,255你可以将它转换成字节数组,然后加载到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(); 
    } 
} 
+0

数据是0 - 65000,但我会尝试缩小它,看看这种方法是否比我目前的方法更快 – 2011-03-22 10:22:24

2

here你有如何通过一个MemoryStream得到BitmapImage的一个例子,这可以帮助你

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