1

I do have some problems to display an image (uEye-Cam) in a wpf-Image form. The displayed image is completely white. 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
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);

Is there anything wrong with the image1 properties maybe or..?

Thanks

WPF bitmap to image conversion shows a black image only

Community
  • 1
  • 1
Norick
  • 271
  • 5
  • 20
  • possible duplicate of [WPF bitmap to image conversion shows a black image only](http://stackoverflow.com/questions/23269876/wpf-bitmap-to-image-conversion-shows-a-black-image-only) – Clemens Apr 28 '14 at 07:17

1 Answers1

2

I've had good results with converting every new frame to a bitmap as in the question:

Bitmap bmp;

cam.Memory.ToBitmap(s32MemId, out bmp);

And then converting this to a BitmapImage using:

BitmapImage bitmapImage = null;

using (System.IO.MemoryStream memory = new System.IO.MemoryStream())
{
    bitmapImage = new BitmapImage();
    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
    memory.Position = 0;
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = memory;
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.EndInit();
}

And then locking and freezing (as in How do you pass a BitmapImage from a background thread to the UI thread in WPF?) the BitmapImage to set it as the source of the Image control using:

Object tempLock = new Object();
BitmapImage lastBitmapImage = null;

lock (tempLock)
{ 
    lastBitmapImage = bitmapImage.Clone();
}
lastBitmapImage.Freeze();

Finally, the BitmapImage is set as the souce of the Image control with:

this.Dispatcher.Invoke(new Action(() =>
{
    imageDisplay.Source = lastBitmapImage;
}));
Community
  • 1
  • 1
Wibble
  • 796
  • 1
  • 8
  • 23