0

I am using a camera which captures images in a live mode. Every new image is called via an event handle. The problem occurs (line: image1.Source = wpfBitmap) when I try to convert the bitmap image to a wpf-image (this conversion is tested and works for a single image). Following code is used for the event and image conversion:

    private void onFrameEvent(object sender, EventArgs e)
    {
        uEye.Camera Camera = sender as uEye.Camera;

        Int32 s32MemID;
        Camera.Memory.GetActive(out s32MemID);


        // Read Camera bitmap
        Bitmap bitmap;
        Camera.Memory.ToBitmap(s32MemID, out bitmap);

        // Convert bitmap to WPF-Image
        var bmp = new Bitmap(bitmap);
        var 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;

        DeleteObject(hBitmap);
    }

The error looks like this:

enter image description here

Does anyone know how I avoid this exception? Thanks in advance for the help...

Norick
  • 271
  • 5
  • 20
  • 1
    This is probably the most asked WPF question here on SO. Did you ever try to google the execption message? You'll get thousands of useful hits. Solution: call `wpfBitmap.Freeze();` before setting `image1.Source`. – Clemens May 15 '14 at 10:00
  • And assign `image1.Source` in a Dispatcher call on the UI thread. – Clemens May 15 '14 at 10:09

0 Answers0