从 System.Drawing.Bitmap 加载 WPF BitmapImage
问题描述:
我有一个 System.Drawing.Bitmap
的实例,并希望以 System.Windows.Media.Imaging.BitmapImage.
I have an instance of a System.Drawing.Bitmap
and would like to make it available to my WPF app in the form of a System.Windows.Media.Imaging.BitmapImage
.
最好的方法是什么?
答
感谢 Hallgrim,这是我最终得到的代码:
Thanks to Hallgrim, here is the code I ended up with:
ScreenCapture = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(),
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(width, height));
我也最终绑定到 BitmapSource 而不是 BitmapImage,就像我原来的问题一样
I also ended up binding to a BitmapSource instead of a BitmapImage as in my original question