Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :)
Posted

 
Share this answer
 
Hi Niloufar
What a coincidence. Nice to see your same question again on codeproject. I've answered your question on StackOverflow :). so I just post it here again. But this time, I'll separate my code block:

// declare in your class
            [System.Runtime.InteropServices.DllImport("gdi32.dll")]
            public static extern bool DeleteObject(IntPtr hObject);
            IntPtr bmp;

// your frame loop
{
                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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900