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