【发布时间】:2014-06-09 19:06:15
【问题描述】:
我在以 wpf-Image 形式显示图像(uEye-Cam)时确实存在一些问题。显示的 图像是全黑的。以下是我使用的代码:
//Get Cam Bitmap Image
var cam = new uEye.Camera();
cam.Init();
cam.Memory.Allocate();
cam.Acquisition.Capture(uEye.Defines.DeviceParameter.DontWait);
Int32 s32MemId;
cam.Memory.GetActive(out s32MemId);
cam.Memory.Lock(s32MemId);
Bitmap bitmap;
cam.Memory.ToBitmap(s32MemId, out bitmap);
//WPF Image control
System.Windows.Controls.Image img = new System.Windows.Controls.Image();
//convert System.Drawing.Image to WPF image
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(bitmap);
IntPtr hBitmap = bmp.GetHbitmap();
System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
image1.Source = WpfBitmap;
image1.Stretch = System.Windows.Media.Stretch.UniformToFill;
image1.Visibility = Visibility.Visible;
有人知道为什么我只看到黑色图像(摄像头正在工作!)吗? 提前感谢您的帮助
编辑(作为对答案/cmets 的反应):
我在我的应用程序中使用的是 WPF 而不是 WinForms。
你是对的,“img”没有被使用,因此不正确。我相应地更改了代码并实现了 DeleteObject。我的代码现在看起来像这样:
//WPF Image control
var image1 = new System.Windows.Controls.Image();
//convert System.Drawing.Image to WPF image
var bmp = new System.Drawing.Bitmap(bitmap);
IntPtr hBitmap = bmp.GetHbitmap();
System.Windows.Media.ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
image1.Source = wpfBitmap;
image1.Stretch = System.Windows.Media.Stretch.UniformToFill;
image1.Visibility = Visibility.Visible;
DeleteObject(hBitmap);
与之前的不同之处在于我看到的是白色图像而不是黑色图像。我不确定为什么它不给我返回凸轮图像。 image1 属性可能有什么问题或者..?
谢谢
【问题讨论】:
-
在WinForms应用程序中直接使用System.Drawing.Bitmap有图像吗?
-
为什么要创建一个以后不用的
System.Windows.Controls.Image img?为什么要在您已经拥有Bitmap bitmap的地方添加额外的Bitmap bmp?你必须在 GetHbitmap 返回的位图句柄上调用DeleteObject。 -
嗨,我也有类似的问题。你找到解决办法了吗?
-
也遇到过这种情况。即使在关注其他类似帖子之后仍然没有解决方案。