至于我可以告诉从的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