Làm cách nào để có thể giải phóng xử lý trên tệp này?Xử lý phát hành trên hồ sơ. ImageSource từ BitmapImage
img là loại 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.
}
Giải pháp
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;
}
Giải pháp tốt. Bạn đã lưu ngày của tôi :) – gisek
3 dòng này là gì: img.Source = imageSrc; // Làm việc imageSrc = null; img.Source = null; – MonsterMMORPG
@MonsterMMORPG đừng lo lắng về chúng ... bitmap.CacheOption = BitmapCacheOption.OnLoad; là phần ma thuật. – NitroxDM