I have a WPF application which successively displays images from a ObservableCollection
(includes strings with the image paths)
<Image Source="{Binding ListOfUnsortedImages[0], UpdateSourceTrigger=PropertyChanged}" />
This is the collection including the image paths:
private ObservableCollection<String> _listOfUnsortedImages = new ObservableCollection<String>();
The application displays the image and after clicking a button, the application copies the image, removes it from the collections and shows the next image.
This is the reduced logic of the button:
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);
The last line fires the IOException:
The process cannot access the file 'filename' because it is being used by another process
I've tried to solve the issue by using Dispose()
and/or using{}
but without success.