1

I've read probably all the post regarding this. Can't seem to find the problem. this is my collection

private ObservableCollection<BitmapImage> _imageList = new ObservableCollection<BitmapImage>();

And this is how i load my images

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); 
}

The exception happens at the line when i try to delete, the file is in use.

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());
}

The Exception:

An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code.

Additional information: The process cannot access the file 'e:\pix\img.jpg' because it is being used by another process.

Can someone please reopen it? I figured out the problem, and it's not this code, it was in the section of the View, i would like to post an answer.

Akos
  • 210
  • 4
  • 14

2 Answers2

0

But what does the exception says? And on which line of code it gets fired? I would bet that your BitmapImage holds the file and that is why you can't delete it, you should firstly dispose it.

Sometimes WPF just holds the file, in that case it's good to just store the bytes from the file and leave it alone, something like:

  private BitmapImage loadImage(string imgPath)
    {
        BitmapImage myRetVal = null;
        if (imgPath != null)
        {
            BitmapImage img = new BitmapImage();
            using (FileStream stream = File.OpenRead(imgPath))
            {
                img.BeginInit();
                img.CacheOption = BitmapCacheOption.OnLoad;
                img.StreamSource = stream;
                img.EndInit();
            }
            myRetVal = image;
        }
        return myRetVal;
    }
alek kowalczyk
  • 4,896
  • 1
  • 26
  • 55
0

You remove the temp'th item from the list and immediately thereafter you reuse it. Given the fact, that there are enough items in the list, the wrong file will be deleted. Otherwise you get an IndexOutOfRangeException.

Furthermore, UriSource.AbsolutePath is a string, there's no point in using ToString() then.

From the posted code it cannot be seen, why the file may be in use. Please check, whether another application keeps the file open (or you, elsewhere).

JeffRSon
  • 10,404
  • 4
  • 26
  • 51
  • That is true, but still, even without that piece of code, i still get the same Error, the IO exception. I will edit it again to clarify – Akos Sep 28 '15 at 12:36
  • no other application uses the file, as soon as i close the app, i can delete it from win. explorer, before closing, it gives me a file in use error also. – Akos Sep 28 '15 at 12:39
  • Is there any other part in your code, where you access these files? – JeffRSon Sep 28 '15 at 12:47
  • I do have a public ObservableCollection ImageList wich is bound to the view, but i don't think it has anything to do with the files wich are loaded into the collection. – Akos Sep 28 '15 at 12:49
  • How is this other ImageList filled? – JeffRSon Sep 28 '15 at 12:54
  • The one i commented before just has the get set methods and returns the _imagelist which was populated in the code shows in the question. – Akos Sep 28 '15 at 13:08