Delete an open picture in WPF

Source: Internet
Author: User

Original: Delete an open picture in WPF

In WPF, when we delete an open picture, we often encounter "... Cannot delete the exception that the file is being used by another process. This is not possible even if the current file is closed after it has been opened.

The reason for this problem is simple because the WPF caching policy causes the picture to be locked, not the default ImageSource constructor, instead of using the following method:

???? imagesourcegetImage(string file)
???? {
???????? var bmp = newBitmapImage();
???????? Bmp. BeginInit();
???????? Bmp. cacheoption = bitmapcacheoption. OnLoad ;???????? // This is a very important line .
???????? Bmp. UriSource = newUri(file);
???????? Bmp. EndInit();

???????? return bmp;
????}

This is not a problem in itself, but a simpler one is found today, as long as a single line of code:

???? bitmapframe. Create(newUri(file), bitmapcreateoptions. None , bitmapcacheoption. OnLoad );

Here is a record, the main in the future in this way.

Delete an open picture in WPF

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.