I'm working with image files over 1 GB, creating a Bitmap
from a large BitmapSource
and attempting to dispose of the original BitmapSource
. The BitmapSource
stays in memory. Normally this is an inconvenience as it is eventually collected, but with these large files, clearing the memory immediately is a necessity:
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
I've come across a work-around here, but the BitmapSource
still persists until much later in the pipeline.