0

I have the following code:

imgScreenshot.Source = new BitmapImage(new Uri(ShellFolder.DocumentsFolder() + System.IO.Path.DirectorySeparatorChar + screenshot.Filename));
File.Delete(ShellFolder.DocumentsFolder() + System.IO.Path.DirectorySeparatorChar + screenshot.Filename);

I get an error:

{"The process cannot access the file 'C:\Users\rover\Documents\MagicScreenshot\vEhWg3Ra20M.jpg' because it is being used by another process."}

I thought about dispose BitmapImage, but this class does not realize this interface. How to write this code correctly?

Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
  • Try putting the code to delete the file in the event handler for the [`DownloadCompleted`](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.downloadcompleted.aspx) event. – Daniel Hilgarth May 13 '13 at 11:49

1 Answers1

2

Try:

        BitmapImage bi = new BitmapImage();
        bi.BeginInit();
        bi.UriSource = new Uri(ShellFolder.DocumentsFolder() + System.IO.Path.DirectorySeparatorChar + screenshot.Filename);
        bi.CacheOption = BitmapCacheOption.OnLoad;
        bi.EndInit();
        imgScreenshot.Source = bi;
        File.Delete(ShellFolder.DocumentsFolder() + System.IO.Path.DirectorySeparatorChar + screenshot.Filename);
Andy Hopper
  • 3,618
  • 1
  • 20
  • 26