【发布时间】:2015-10-08 13:23:02
【问题描述】:
我可能已经阅读了所有关于此的帖子。似乎找不到问题。 这是我的收藏
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异常没有帮助
标签: c# wpf image ioexception