如何释放此文件上的句柄?释放文件上的句柄。来自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