我讀過可能所有關於這個帖子。似乎無法找到問題。 這是我收集WPF無法從代碼中刪除圖像。 IOException
private ObservableCollection<BitmapImage> _imageList = new ObservableCollection<BitmapImage>();
這也是我如何載入我的圖片
foreach (string filepath in temp) //populate image collection
{
BitmapImage tempImg = new BitmapImage();
tempImg.BeginInit();
tempImg.CacheOption = BitmapCacheOption.OnLoad;
tempImg.UriSource = new Uri(filepath);
tempImg.EndInit();
_imageList.Add(tempImg);
}
唯一的例外發生在線路當我嘗試刪除該文件正在使用中。
if (File.Exists(_imageList[SelectedItem].UriSource.AbsolutePath.ToString()))
{
int temp = SelectedItem;
//_imageList.RemoveAt(temp);
Console.WriteLine(_imageList[temp].UriSource.AbsolutePath.ToString());
File.Delete(_imageList[temp].UriSource.AbsolutePath.ToString());
}
除外:
類型「System.IO.IOException」的一個例外發生在mscorlib.dll但在用戶代碼中沒有處理。
附加信息:進程無法訪問文件'e:\ pix \ img.jpg',因爲它正在被另一個進程使用。
有人可以重新打開嗎?我想出了問題,這不是這個代碼,它是在視圖的部分,我想發佈一個答案。
它可能不會解決你的問題,但你應該添加tempImg.Freeze(); tempImg.EndInit();以避免內存泄漏。 –
謝謝你,我已經這樣做了,沒有幫助我更新代碼的io異常 – Akos