2010-06-01 65 views
0

我有一個高速攝像頭,我在WPF控件中顯示。相機的SDK爲我提供了表示圖像數據的字節數組。我正在尋找最有效的方式來綁定到Canvas控件上的這些圖像。這些圖像並排顯示在20張圖像上。WPF 2D圖像綁定性能

我目前的方法確實按照我想要的方式工作,但我覺得有太多不必要的轉換。

我第一次我的字節數組轉換爲位圖,如下所示:

//note dataAdress is an IntPtr I get from the 3rd party SDK  
//i also have width and height ints 

bmp = new Bitmap(width, weight, PixelFormat.Format32bppRgb);   

BitmapData bData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb); 

int datasize = width * height * 4; 
byte[] rgbValues = new byte[datasize]; 

Marshal.Copy(dataAdress, rgbValues, 0, datasize); 
Marshal.Copy(rgbValues, 0, bData.Scan0, datasize); 
bmp.UnlockBits(bData); 

然後我調用該函數將位圖轉換成一個的BitmapSource:

BitmapSource source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); 

請注意,我跳過一些處理/清理以提高可讀性。我必須使用BitmapSource,以便可以綁定ImageSource來顯示圖像。如果這是XNA,我會使用Texture2Ds,這會飛。我認爲WPF使用DirectX的基礎是一個比使用總體位圖更好的方法。

任何想法?

回答