【问题标题】:WPF bitmap to image conversion shows a black image onlyWPF 位图到图像的转换仅显示黑色图像
【发布时间】: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
  • 嗨,我也有类似的问题。你找到解决办法了吗?
  • 也遇到过这种情况。即使在关注其他类似帖子之后仍然没有解决方案。

标签: c# wpf image bitmap


【解决方案1】:

我知道这很长,但您的问题可能是因为在您的捕获方法中(请参见下面的行)您正在传递 DontWait,这意味着线程不会等待相机捕获图像,但您会立即从相机内存中访问图像。在您复制图像时,相机不太可能已获取图像。

cam.Acquisition.Capture(uEye.Defines.DeviceParameter.DontWait);

你可以传入 uEye.Defines.DeviceParameter.Wait 来阻塞线程直到图像被完全捕获,或者如果你使用 DontWait(我个人更喜欢这个),订阅相机 OnFrameReceived 事件,然后将图像复制出相机在那里。

【讨论】:

    猜你喜欢
    • 2014-06-13
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多