在WPF Bitmap类 [英] Bitmap class in WPF
问题描述
我与Emgu简历工作的WinForms使用Kinect的做人脸识别。现在,我想移动到WPF。然而,EmguCv库只支持位图类。
I'm working with Emgu Cv in Winforms to do face recognition using Kinect. Now, i want to move to WPF. However, the EmguCv library support only Bitmap class.
我可以使用WPF中的位图类(在使用的WinForms)?如果没有,是否有WPF中使用Emgu简历连Kinect的另一种方法?
Can i use the Bitmap class (used in Winforms) in WPF ? if not, is there an other method to use Emgu cv with kinect in WPF?
感谢。
推荐答案
<一个href=\"http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx\"><$c$c>System.Drawing.Bitmap$c$c>不能直接作为WPF图像源,所以你必须将它转换为<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.aspx\"><$c$c>System.Windows.Media.Imaging.BitmapSource$c$c>.
要做到这一点,最好的办法是使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.interop.imaging.createbitmapsourcefromhbitmap.aspx\"><$c$c>Imaging.CreateBitmapSourceFromHBitmap$c$c>.
The best way to do it is by using Imaging.CreateBitmapSourceFromHBitmap
.
您可以使用扩展方法:
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
IntPtr ip = source.GetHbitmap();
try
{
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip,
IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(ip);
}
}
请注意,您必须调用 DeleteObject的
,因为 Bitmap.GetHbitmap()
泄漏一个GDI句柄(见< A HREF =http://stackoverflow.com/a/1118557/63011>这个接听)。
Please note that you must invoke DeleteObject
, because Bitmap.GetHbitmap()
leaks a GDI handle (see this answer).
一旦你有一个的BitmapSource
,您可以使用它显示的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.controls.image.aspx\"><$c$c>Image$c$c>控制和通过设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.controls.image.source.aspx\"><$c$c>Source$c$c>属性。
Once you have a BitmapSource
, you can display it using an Image
control and by setting the Source
property.
您可以在这篇文章中阅读更多关于WPF成像:成像概述
You can read more about WPF imaging in this article: Imaging Overview
这篇关于在WPF Bitmap类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!