【发布时间】:2017-08-14 06:11:54
【问题描述】:
我有一个 WPF 应用程序,它连续显示来自 ObservableCollection 的图像(包括带有图像路径的字符串)
<Image Source="{Binding ListOfUnsortedImages[0], UpdateSourceTrigger=PropertyChanged}" />
这是包含图片路径的集合:
private ObservableCollection<String> _listOfUnsortedImages = new ObservableCollection<String>();
应用程序显示图像,单击按钮后,应用程序复制图像,将其从集合中删除并显示下一个图像。
这是按钮的简化逻辑:
Image img;
var currentImage = ListOfUnsortedImages[0];
using (Stream stream = File.OpenRead(currentImage))
{
img = System.Drawing.Image.FromStream(stream);
// Do something with the image
img.Dispose();
stream.Dispose();
}
ListOfUnsortedImages.RemoveAt(0);
File.Delete(currentImage);
最后一行触发 IOException:
进程无法访问文件“文件名”,因为它正在被使用 由另一个进程
我尝试使用Dispose() 和/或using{} 来解决此问题,但没有成功。
【问题讨论】:
-
如果你跳过使用块,你可以删除文件吗?然后你就会知道问题是否在这个块内
-
如何将源代码放在路径下?可能不会被复制发布,所以你可以阅读,但不能删除?舒尔,没有进程使用源,而只有您的应用程序?
-
如果您不想更改代码 - 您可以创建转换器,该转换器将使用 BitmapCacheOption.OnLoad 从字符串创建
BitmapImage。