我以前的转换位图以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 CRE茨非托管对象躺在附近,因为这是不受管理它不会由.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)