0

如何在C# 4.0中连续做一个图像文件,保存和删除功能

我正在寻找修改图像文件,如旋转 90 度。首先,我保存了备用名称并删除了原始文件并将备用文件名重命名为原始文件名。

在 iis 中托管我的应用程序后,在这里遇到此错误消息“该进程无法访问该文件,因为它正在被另一个进程使用。”

Image i= Image.FromFile(fileName);
i.Save(svfilename);
System.IO.File.Delete(fileName);
System.IO.File.Move(svfilename, fileName);
4

2 回答 2

3

You used the Image.FromFile method to create an Image object representing the image contained in that file

Image i= Image.FromFile(fileName);

That's why you cannot delete the file: because your process is still using it! You won't be able to delete the file on disk until the Image object you created from it is disposed, freeing the lock on the file.

The documentation for the FromFile method confirms this:

The file remains locked until the Image is disposed.

To ensure that an object is disposed, wrap its creation and use inside of a using block.

于 2013-07-05T09:31:31.893 回答
0

解决方案可能是这样的

Image i= Image.FromFile(fileName);
i.Save(svfilename);
i.Dispose();
System.IO.File.Delete(fileName);
System.IO.File.Move(svfilename, fileName);
于 2013-07-05T10:12:56.473 回答