0

我有以下代码:

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

我收到一个错误:

{“进程无法访问文件 'C:\Users\rover\Documents\MagicScreenshot\vEhWg3Ra20M.jpg',因为它正被另一个进程使用。”}

想过dispose BitmapImage,但是这个类没有实现这个接口。如何正确编写此代码?

4

1 回答 1

2

尝试:

        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);
于 2013-05-13T12:09:14.920 回答