3

我正在处理超过 1 GB 的图像文件,Bitmap从一个大文件创建一个BitmapSource并尝试处理原始BitmapSource. BitmapSource留在记忆中。通常这是一个不便,因为它最终会被收集,但是对于这些大文件,立即清除内存是必要的:

System.Drawing.Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
    BitmapEncoder enc = new BmpBitmapEncoder();
    enc.Frames.Add(BitmapFrame.Create(bitmapsource)); //large image
    enc.Save(outStream);
    bitmapsource = null;

    Bitmap temp = new System.Drawing.Bitmap(outStream);
    bitmap = temp.Clone(new Rectangle(0, 0, bm.Width, bm.Height), bm.PixelFormat);
    temp.Dispose();
}

GC.Collect();
GC.Collect();

//both bitmap and bitmapsource remain in memory

我在这里遇到了一种解决方法,但它BitmapSource仍然持续到管道的后期。

4

0 回答 0