gpt4 book ai didi

C#文件访问错误

转载 作者:行者123 更新时间:2023-11-30 22:18:43 25 4
gpt4 key购买 nike

我有以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com