创建BitmapImage WPF

我有一个包含我需要在屏幕上显示的图像数据的ushort [],当我创建一个Windows.System.Drawing.Bitmap,并将其转换为BitmapImage时,这感觉就像一个缓慢无助的方式来做到这一点。

有没有人创建ushort []的BitmapImage的最快方法是什么?

或者从数据中另外创建一个ImageSource对象?

谢谢,

埃蒙·

我之前将Bitmap转换为BitmapImage的方法是:

MemoryStream ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Png); ms.Position = 0; BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); 

我能够加快使用速度

 Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 

编辑:任何人使用这个应该知道bitmap.GetHbitmap创建一个非托管对象,因为这是非托管的,它不会被.net垃圾收集器拾取,必须删除以避免内存泄漏,使用以下代码来解决这个问题:

  [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject); IntPtr hBitmap = bitmap.GetHbitmap(); try { imageSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); } catch (Exception e) { } finally { DeleteObject(hBitmap); } 

(它不是很整齐,必须导入像这样的DLL,但这是从msdn,并似乎是围绕这个问题的唯一方法 – http://msdn.microsoft.com/en-us/library/1dz311e4.aspx