Detailed solution for memory release Based on. NET BitmapImage

Source: Internet
Author: User

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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.