1

I have a custom window named ImageWindow, which has a Image control in it.

public void DownloadImageWithExtension(Uri url, ImageFormat format)
{
    if (File.Exists(path))
    {
        File.Delete(path));
    }
    using (WebClient client = new WebClient())
    {
        client.DownloadFile(url, path);
    }
    ImageWindow iw = new ImageWindow(path);
    iw.ShowDialog();
}

This is my main windows code, which calls ImageWindow (iw) passing the file to open, which has been downloaded.

public partial class ImageWindow : Window
{
    public ImageWindow(string fileName)
    {
        InitializeComponent();
        BitmapImage image = new BitmapImage(new Uri(fileName));
        image_Image.Source = image; //Set Image controls image source to BitmapImage
    }
}

Here i get the image, save it to BitmapImage and add to Source of the Image control inside the newly opened window.

After i close the window and try to open the window again, i get this I/O exception.

The process cannot access the file '\bin\Debug\10supercomfywallpapers!.Jpeg' because it is being used by another process.

I have found some fixes mentioning using dispose and such, but BitmapImage doesn't inherit IDisposable.

EDIT: The deletion will be moved to after the OpenDialog() returns, but i need to fix the window not disposing first to do that.

mm8
  • 163,881
  • 10
  • 57
  • 88
Tomato
  • 115
  • 1
  • 13
  • Possible duplicate of [How to dispose BitmapImage cache?](https://stackoverflow.com/questions/28364439/how-to-dispose-bitmapimage-cache) – Camilo Terevinto May 29 '17 at 13:44

1 Answers1

2

Try to set the CacheOption to BitmapCacheOption.OnLoad:

public partial class ImageWindow : Window
{
    public ImageWindow(string fileName)
    {
        InitializeComponent();
        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.UriSource = new Uri(fileName);
        image.EndInit();
        image.Freeze();
        image_Image.Source = image; //Set Image controls image source to BitmapImage
    }
}

This should prevent the file from being locked.

mm8
  • 163,881
  • 10
  • 57
  • 88