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