3

I'm doing a project and I'm capturing frames from kinect and do some real-time process on them, I need to display bitmaps so I'm converting them to bmapsource and pass to image.source:

Bitmap bmap = new Bitmap(640, 480, System.Drawing.Imaging.PixelFormat
                                                     .Format24bppRgb);

BitmapSource bmapSource= System.Windows.Interop.Imaging.
  CreateBitmapSourceFromHBitmap(bmap.GetHbitmap(),IntPtr.Zero, Int32Rect.Empty, 
                                BitmapSizeOptions.FromEmptyOptions());
image.source = bmapSource;

But as I'm processing 15FPS after 2 minute I get the error "Out of memory" for this part. Is there anyway to clear the memory after each process? or isthere any other way to display the bmap in wpf?

Thanks in advance :)

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
Niloufar
  • 512
  • 1
  • 5
  • 24
  • possible duplicate of [C# WPF BitmapSource Memory Leak?](http://stackoverflow.com/questions/7218721/c-sharp-wpf-bitmapsource-memory-leak) – Rohit Vats Dec 26 '13 at 07:46

1 Answers1

0

Try this:

            [System.Runtime.InteropServices.DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            IntPtr bmp;
{
                if (bmp != null)
                {
                    DeleteObject(bmp);
                }
                Bitmap bmap = new Bitmap(640, 480, System.Drawing.Imaging.PixelFormat
                                                         .Format24bppRgb);
                bmp = bmap.GetHbitmap();
                BitmapSource bmapSource = System.Windows.Interop.Imaging.
                  CreateBitmapSourceFromHBitmap(bmp, IntPtr.Zero, Int32Rect.Empty,
                                                BitmapSizeOptions.FromEmptyOptions());
                bmap.Dispose();
                bmap = null;

                image.Source = bmapSource;
}
yushulx
  • 11,695
  • 8
  • 37
  • 64
  • Dear Yushulx, thanks for your help, but I'm a little confused with this code, would you please give me some comments please. where are you filling the frame you are getting, I mean you image or frame captured from kinect? I have a result image which is a bitmap and I want to display it in image.source – Niloufar Dec 26 '13 at 16:07
  • Sorry, I didn't make it clear. I should have separate the code block. the code out of the bracket should be declared globally in your class. and the code inside of the bracket is for your implementation. I didn't do anything about kinect capture, just I tried it in my own event. so what I've done is to release two copies of data. one is bitmap, you can use dispose. the other is the copy from bitmap.gethbitmap. you should use DeleteObject to release it. so have a try. Hopefully it will work :) – yushulx Dec 27 '13 at 01:19
  • the IntPtr bmp is used to save the bitmap handle, and release it before you are trying to draw a new frame captured from Kinect. make sure there is only one copy for your image source, and release other uesless memory. the bitmap is not mangaged. you have to manually release it. – yushulx Dec 27 '13 at 01:22
  • Dear Yushulx, thanks for your help, but still, I have the out of memory problem and now it's on a part of your code, bmp = bmap.GetHbitmap(); – Niloufar Dec 28 '13 at 07:42
  • You have to release the memory by DeleteObject(bmp) when you receive a new frame or stop receiving frame from Kinect. – yushulx Dec 28 '13 at 08:26
  • Yes I'm doing that, it is in your solution. but the problem is still there. – Niloufar Dec 28 '13 at 12:53