我需要逐像素绘制一幅图像,并在WPF中显示它。 我尝试通过使用System.Drawing.Bitmap
,然后使用CreateBitmapSourceFromHBitmap()
为WPF Image控件创建BitmapSource
来实现这一目标。 由于反复调用CreateBitmapSourceFromBitmap()
会导致内存泄漏,因此我在某个地方存在内存泄漏,直到程序结束时其内存使用量才会下降。 如果我不调用CreateBitmapSourceFromBitmap()
,则内存使用量没有明显变化。
for (int i = 0; i < 100; i++)
{
var bmp = new System.Drawing.Bitmap(1000, 1000);
var source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
source = null;
bmp.Dispose();
bmp = null;
}
如何释放BitmapSource
的内存?