我正在做一个项目,正在从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?

提前致谢 :)

最佳答案

尝试这个:

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

关于c# - 清除WPF中的内存,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20780557/

10-12 16:07