2

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):

I am using WPF not WinForms in my application.

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..?

Thanks

SQB
  • 3,926
  • 2
  • 28
  • 49
Norick
  • 271
  • 5
  • 20
  • Is there an image when you directly use the System.Drawing.Bitmap in a WinForms application? – Clemens Apr 24 '14 at 13:28
  • Why do you create a `System.Windows.Controls.Image img` which you don't use later? Why the additional `Bitmap bmp`, where you already have `Bitmap bitmap`? And you have to call [DeleteObject](http://stackoverflow.com/a/1546121/1136211) on the bitmap handle returned from GetHbitmap. – Clemens Apr 24 '14 at 13:32
  • Hi, I have a similar issue. Have you managed to find a solution? – Stefan Jul 29 '15 at 09:37
  • Experiencing this also. Still no solution even after following other similar posts. –  Mar 19 '16 at 06:35

1 Answers1

0

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

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.

falopsy
  • 636
  • 7
  • 16