c# - 如何释放 BitMap 类持有的文件

标签 c#

我有以下代码:

Bitmap image = new Bitmap(filePath);
...
image.Save(someOtherPath);
image.Dispose();
File.Delete(filePath);
File.Move(someOtherPath, filePath);

File.Delete 行引发以下错误:

The process cannot access the file '...' because it is being used by another process.

如何让 C# 释放它对文件的锁定?

最佳答案

尝试

using(var image = new Bitmap(filepath))
{
    image.Save(someOtherPath);
}


File.Delete(filePath);
File.Move(someOtherPath, filePath);

关于c# - 如何释放 BitMap 类持有的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12174004/

相关文章:

c# - UWP XAML 弹出窗口不尊重垂直对齐?

c# - 如何获取带空格的 XElement 值?

c# - 从WinRT上的C#中的MP3流绘制波形

C# CefSharp 内存管理

c# - 在不同平台(64 位)上执行线程

c# - 如何防止列表中的项目在未填写且应为 "0"时返回 "null"?

c# - 将现有 PdfDocument 保存到文件

c# - 如何在一段时间后停止异步任务

c# - 如何让监控程序显示为墙纸?

c# - TcpListener HTTP 文件上传随机失败