我正在尝试使用 RenderTargetBitmap 渲染图像
每次我从 RenderTargetBitmap 创建一个实例来渲染图像时,内存都会增加,完成后内存永远不会释放
这是代码:
RenderTargetBitmap rtb = new RenderTargetBitmap((int)(renderWidth * dpiX / 96.0),
(int)(renderHeight * dpiY / 96.0),
dpiX,
dpiY,
PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(target);
ctx.DrawRectangle(vb, null, new System.Windows.Rect(new Point(0, 0), new Point(bounds.Width, bounds.Height)));
}
rtb.Render(dv);
拜托,我需要帮助
我怎样才能释放内存
感谢大家。
如果你监控RenderTargetBitmap
类使用资源监控器,您可以看到每次调用该类时,您都会丢失 500KB 的内存。我对你问题的回答是:不要使用RenderTargetBitmap
上课很多次
您无法释放 RenderTargetBitmap 的已用内存。
如果你确实需要使用RenderTargetBitmap
类,只需在代码末尾添加这些行即可。
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
这可能会解决你的问题:
RenderTargetBitmap rtb = new RenderTargetBitmap((int)(renderWidth * dpiX / 96.0),
(int)(renderHeight * dpiY / 96.0),
dpiX,
dpiY,
PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(target);
ctx.DrawRectangle(vb, null, new System.Windows.Rect(new Point(0, 0), new Point(bounds.Width, bounds.Height)));
}
rtb.Render(dv);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)