BitmapSource MemoryStream泄漏大图像

时间:2012-05-10 02:49:48

标签: wpf .net-4.0 memorystream bitmapimage bitmapsource

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

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

我遇到了here的解决方法,但BitmapSource仍然存在,直到很晚才开始。

0 个答案:

没有答案
相关问题