c# - 清除wpf中的内存

标签 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 时,我得到了这部分的错误“内存不足”。 无论如何要在每个过程后清除内存?或者有没有其他方法可以在 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/

相关文章:

c# - System.Data.Sqlite 是否支持 Entity Framework 6?

c# - WPF 如何更改 ContentControl 内容

c++ - C++中的内部函数内存限制

c# - 内存和代码平衡

c - 分配连续内存以包含具有灵活数组成员的多个结构

c# - 为什么 WinDbg 显示 C# 类和结构的大小相同?

c# - 更新后将程序集安装到 GAC 中?

c# - 如何避免 "Response.Redirect cannot be called in a Page callback"

wpf - 无法在 powershell 中加载 XAML

wpf - 适用于 Win7 和 Win8 主题的 WPF 拆分按钮