Most of the code found on the internet is writtenMemoryStreamTo achieve:
Copy codeThe Code is as follows:
New Thread (new ThreadStart () => {
Var bitmap = new BitmapImage ();
Bitmap. BeginInit ();
Using (var stream = new MemoryStream (File. ReadAllBytes (...))){
Bitmap. StreamSource = stream;
Bitmap. CacheOption = BitmapCacheOption. OnLoad;
Bitmap. EndInit ();
Bitmap. Freeze ();
}
This. Dispatcher. Invoke (Action) delegate {
Image1.Source = bitmap;
});
}). Start ();
Today, the problem arises. When I set DecodeWidth to 100 and load 1000 images, the memory should maintain 100x100 images, but in fact, the memory of the original image is not released until the BitmapImage is recycled, which makes me very embarrassed. In other words, using (MemoryStream) does not actually release the Buffer in MemoryStream as expected, how can we release it?
In fact, the simplest thing is to directly discard the MemoryStream for conversion to FileStream, as shown below:
Copy codeThe Code is as follows:
Using (var stream = new FileStream (path, FileMode. Open )){
Image. BeginInit ();
Image. StreamSource = stream;
Image. DecodePixelWidth = 100;
Image. CacheOption = BitmapCacheOption. OnLoad;
Image. EndInit ();
Image. Freeze ();
}