据我所知,从 BitmapSource 转换为 Bitmap 的唯一方法是通过不安全的代码......就像这样(来自Lesters WPF 博客):
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 在位图上工作。