至於我可以告訴從的BitmapSource轉換爲位圖的唯一途徑是通過不安全的代碼......像這樣(從Lesters WPF blog):有沒有一種很好的方式來轉換BitmapSource和位圖?
myBitmapSource.CopyPixels(bits, stride, 0);
unsafe
{
fixed (byte* pBits = bits)
{
IntPtr ptr = new IntPtr(pBits);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(
width,
height,
stride,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb,ptr);
return bitmap;
}
}
要反過來做:
System.Windows.Media.Imaging.BitmapSource bitmapSource =
System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
在框架中有更簡單的方法嗎?它不在那裏的原因是什麼(如果不是)?我認爲這是相當有用的。
我需要它的原因是因爲我使用AForge在WPF應用程序中執行某些圖像操作。 WPF希望顯示BitmapSource/ImageSource,但是AForge在位圖上工作。
要做相反的事情,你*必須**刪除你用'GetHbitmap'得到的位圖句柄。這個錯誤遍佈互聯網。這是不可修復的。世界正在緩慢泄漏GDI手柄;我們很快就會在他們身上游泳! – 2011-12-31 04:25:10
Thx,指出:) – JohannesH 2012-01-03 10:22:28
romkyns指的是這個:http://stackoverflow.com/questions/1546091/wpf-createbitmapsourcefromhbitmap-memory-leak – 2012-04-26 21:24:44