在wpf中清除内存

时间:2013-12-26 06:57:21

标签: c# wpf memory

我正在做一个项目,我正在从kinect捕获帧并对它们进行一些实时处理,我需要显示位图,所以我将它们转换为bmapsource并传递给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;

但是当我在2分钟后处理15FPS时,我得到了这部分的“Out of memory”错误。 无论如何在每个过程之后清除内存?或者是否有任何其他方式在wpf中显示bmap?

提前致谢:)

1 个答案:

答案 0 :(得分:0)

试试这个:

            [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;
}