2011-03-21 37 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