First, I load a BitmapImage
into the Image
control, which is located on the Window
.
Then I work with the Image
control and then close the Window
.
I do it 2-3 times in a minute and my memory fills up very quickly because the images do not unload from the memory for some reason when the window is closed.
So how do I unload BitmapImage
from Image.Source
control manually to free the RAM?
I believe the solution you are looking for is at http://www.ridgesolutions.ie/index.php/2012/02/03/net-wpf-bitmapimage-file-locking/. In my case, I was trying to find a way to delete the file after it was created, but it appears to be a solution to both issues.
Doesn't free up memory:
var bitmap = new BitmapImage(new Uri(imageFilePath));
Frees up memory, and allows file to be deleted:
var bitmap = new BitmapImage();
var stream = File.OpenRead(imageFilePath);
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
stream.Close();
stream.Dispose();
Optionally, also freeze the BitmapImage:
bitmap.Freeze();