I created an image with this function:
private BitmapImage LoadImage(byte[] imageData)
{
if (imageData == null || imageData.Length == 0) return null;
var image = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
return image;
}
When I attempt to dispose it:
myImage.StreamSource.Close();
myImage.StreamSource.Dispose();
// Throws an exception since its frozen to read only
//myImage.StreamSource = null;
GC.Collect();
It isn't collected by the garbage collector. Possibly since I can't set it to null
.
How can I dispose this BitmapImage
so it doesn't live longer in memory?