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
System.Windows.Controls.Image imgwhich you don't use later? Why the additionalBitmap bmp, where you already haveBitmap bitmap? And you have to call DeleteObject on the bitmap handle returned from GetHbitmap.