WPF位图到图像的转换仅显示黑色图像 [英] WPF bitmap to image conversion shows a black image only

查看:139
本文介绍了WPF位图到图像的转换仅显示黑色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以wpf-Image格式显示图像(uEye-Cam)确实有一些问题.显示的 图像是完全黑色的.下面是我使用的代码:

I do have some problems to display an image (uEye-Cam) in a wpf-Image form. The displayed image is completely black. Below is my used code:

//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;

有人知道为什么我只能看到黑色图像(凸轮在工作!)吗? 预先感谢您的帮助

Does anybody know why I see only a black image (The cam is working!)? Thanks in advance for the help

编辑(作为对答案/评论的回应):

Edit (as a reaction to answers/comments):

我在应用程序中使用的是WPF而不是WinForms.

I am using WPF not WinForms in my application.

您是正确的,没有使用"img",因此不正确.我相应地更改了代码并实现了DeleteObject.我的代码现在看起来像这样:

You are right the "img" was not used and therefore not correct. I changed the code accordingly and implemented the DeleteObject. My code looks like this now:

//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);

与之前的区别在于,我看到的是白色图像,而不是黑色图像.我不确定为什么它不能给我回去的凸轮图像.

The difference to before is that I see a WHITE image instead of a black image. I am not sure why it does not gives me back the cam-image. Is there anything wrong with the image1 properties maybe or..?

谢谢

推荐答案

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

I know it is a long time but your problem is probably because in your capture method (see the line below) you are passing in DontWait, which means the thread will not wait for the camera to capture the image but you are immediately accessing the image from the camera memory. The camera is not likely to have acquired the image by the time you copy the image.

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

您可以传入uEye.Defines.DeviceParameter.Wait来阻塞线程,直到完全捕获图像为止;或者,如果您使用DontWait(我个人比较喜欢这样做),请订阅相机的OnFrameReceived事件,然后将图像复制到相机在那里.

You can either pass in uEye.Defines.DeviceParameter.Wait to block the thread until image is fully captured or if you use DontWait (I prefer this personally), subscribe to the camera OnFrameReceived event and then copy the image out of the camera there.

这篇关于WPF位图到图像的转换仅显示黑色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆