更新时间:2023-02-02 21:36:15
我相信您正在寻找的解决方案是 http://www.ridgesolutions.ie/index.php/2012/02/03/net-wpf-bitmapimage-file锁/ 。就我而言,我试图在创建文件后找到一种删除文件的方法,但它似乎是两个问题的解决方案。
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.
不免费up memory:
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();
也可以冻结BitmapImage:
Optionally, also freeze the BitmapImage:
bitmap.Freeze();