如何釋放此文件上的句柄?釋放文件上的句柄。來自BitmapImage的ImageSource
IMG的類型是System.Windows.Controls.Image的
private void Load()
{
ImageSource imageSrc = new BitmapImage(new Uri(filePath));
img.Source = imageSrc;
//Do Work
imageSrc = null;
img.Source = null;
File.Delete(filePath); // File is being used by another process.
}
解決方案
private void Load()
{
ImageSource imageSrc = BitmapFromUri(new Uri(filePath));
img.Source = imageSrc;
//Do Work
imageSrc = null;
img.Source = null;
File.Delete(filePath); // File deleted.
}
public static ImageSource BitmapFromUri(Uri source)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = source;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
return bitmap;
}
不錯的解決方案。你救了我的一天:) – gisek 2013-01-05 20:43:59
這3行是什麼:img.Source = imageSrc; //做工 imageSrc = null; img.Source = null; – MonsterMMORPG 2013-03-27 11:43:13
@MonsterMMORPG不用擔心它們... bitmap.CacheOption = BitmapCacheOption.OnLoad;是神奇的一部分。 – NitroxDM 2014-04-30 20:15:21