我有以下 C# 代码:
string selectedFile = "D:\..\IMGP2695.JPG";
MyImage myImage = new MyImage();
Image image = Image.FromFile(selectedFile);
string thumbnail_name = CreateThumbnail(image);
myImage.height = image.Height + "px";
myImage.width = image.Width + "px";
ftp.upload(myImage.internalName, selectedFile, directory);
The process cannot access the file `'D:\..\IMGP2695.JPG'` because it is being used by another process.
当代码尝试运行ftp.upload
时,我收到此错误。我知道错误意味着什么,但我不知道如何关闭已打开文件的句柄。
最佳答案
尝试通过使用using
包装Image.FromFile
来处置
图像对象:
string selectedFile = "D:\..\IMGP2695.JPG";
MyImage myImage = new MyImage();
string thumbnail_name = string.empty;
using(Image image = Image.FromFile(selectedFile)){
thumbnail_name = CreateThumbnail(image);
myImage.height = image.Height + "px";
myImage.width = image.Width + "px";
}
ftp.upload(myImage.internalName, selectedFile, directory);
关于C# 文件访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15943642/