RenderTargetBitmap导致内存不足

时间:2016-11-14 20:44:40

标签: c# wpf memory rendertargetbitmap

我之前看到线程出现问题,但我似乎无法找到问题的解决方案。我有一个应用程序,允许用户创建自己的画布。让我们说在这个例子中,用户创建了150幅画布,分别为745px x 1045px。现在有一个功能允许用户将这些全部导出到单独的PNG文件中。这是我的方法:

public static void ExportToPng(Uri path, Canvas surface)
{
    if (path == null) return;

    Size size = new Size(surface.Width, surface.Height);
    surface.Measure(size);
    surface.Arrange(new Rect(size));
    surface.UpdateLayout();

    RenderTargetBitmap renderBitmap =
      new RenderTargetBitmap(
        (int)size.Width,
        (int)size.Height,
        96d,
        96d,
        PixelFormats.Pbgra32);
    renderBitmap.Render(surface);

    using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
    {
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
        encoder.Save(outStream);
    }
}

大约45张左右的图像后,应用程序在行中断开: renderBitmap.Render(surface); 说: 内存不足,无法继续执行程序。

现在我明白这个过程可能有点内存耗费,但我觉得在渲染图像并导出为PNG后,我应该能够以某种方式处理它,然后再转到下一个图像。我不知道该怎么做。任何和所有建议表示赞赏!

0 个答案:

没有答案
相关问题