wpf - 在运行时删除图像

标签 wpf image delete-file

我正在尝试使用以下方法删除图像: (文件路径100%正确)

if(File.Exists(filePath))
   File.Delete(filePath);

我收到以下异常:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'C:\visual_programming\yad2\yad2\bin\Debug\images\1.jpg' because it is being used by another process.

最佳答案

对于直接绑定(bind)到 Image 控件的 ImageSource 的图像来说,这是一个常见问题。您应该通过 BitmapImage 创建 ImageSource 并将 BitmapImage.CacheOption 属性设置为 BitmapCacheOption.OnLoad:

BitmapImage bi = new BitmapImage();

// Begin initialization.
bi.BeginInit();

// Set properties.
bi.CacheOption = BitmapCacheOption.OnLoad;
// 
bi.EndInit();

More Details about BitmapImage.CacheOption on MSDN

关于wpf - 在运行时删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4498769/

相关文章:

.net - 如何模拟文本输入到WPF文本框?

c# - WPF中的静态资源和动态资源有什么区别?

c# - 将 .jls 图像转换为字节 [] 数组时出现错误 : "Out of Memory ",

command-prompt - 命令提示符下的del命令

date - 删除日期后的所有文件,代码很慢

linux - 当新文件到达供应商时,bash 文件 cron 作业出现问题

c# - 如何绑定(bind)到 ObservableCollection 的一个元素

wpf - DataGrid表显示额外的不必要的空行

Python Wand 从 PDF 转换为 JPG 背景不正确

java - 如何将指定包中的图像添加到标签、框架等中