[英]File being used (without opening it) c#
I got a strange error with this program, It download a photo from internet, then I convert it to .jpeg and then I delete the first photo (in .png). 我在此程序中遇到一个奇怪的错误,它从互联网上下载了一张照片,然后将其转换为.jpeg,然后删除了第一张照片(.png)。 But i got an error: File is being used by another process. 但是我遇到一个错误:文件正在被另一个进程使用。 Why is happening this? 为什么会这样呢? I didn't open the file, and nobody is using it. 我没有打开文件,没有人使用它。
string outFile;
outFile = Path.GetTempFileName();
try
{
webClient.DownloadFile(foto, outFile);
if (foto.Substring(foto.Length - 3) == "png")
{
System.Drawing.Image image1 = System.Drawing.Image.FromFile(outFile);
foto = foto.Remove(foto.Length - 3) + "jpg";
string outFile2 = Path.GetTempFileName();
image1.Save(outFile2, System.Drawing.Imaging.ImageFormat.Jpeg);
System.IO.File.Delete(outFile);
outFile = outFile2;
}
}
FromFile is keeping the file open, you have to use something like this: FromFile使文件保持打开状态,您必须使用以下命令:
// Load image
FileStream filestream;
filestream = new FileStream("Filename",FileMode.Open, FileAccess.Read);
currentImage = System.Drawing.Image.FromStream(filestream);
filestream.Close();
The system.Drawing.Image is holding on to the file, Just wrap the image1 in a using statement. system.Drawing.Image保留在文件上,只需将image1包装在using语句中即可。
string foto = "http://icons.iconarchive.com/icons/mazenl77/I-like-buttons/64/Style-Config-icon.png";
string outFile = Path.GetTempFileName();
WebClient webClient = new WebClient();
try
{
webClient.DownloadFile(foto, outFile);
if (Path.GetExtension(foto).ToUpper() == ".PNG")
{
string outFile2 = Path.GetTempFileName();
using (System.Drawing.Image image1 = System.Drawing.Image.FromFile(outFile))
{
image1.Save(outFile2, System.Drawing.Imaging.ImageFormat.Jpeg);
}
System.IO.File.Delete(outFile);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.