[英]System.IO exception “The process cannot access the file because it is being used by another process.”
My program uses a Telerik ASyncUpload control in order to allow the user to upload images. 我的程序使用Telerik ASyncUpload控件,以便允许用户上传图像。 The control has been configured to accept only images, and in its FileUploaded event, there's some extra code doing some additional work (resizing (twice) and converting to JPEG).
该控件已配置为仅接受图像,并且在其FileUploaded事件中,有一些额外的代码可以完成一些其他工作(调整大小(两次)并转换为JPEG)。
However, after the code block below finishes, and my image is saved, the program throws a System.IO exception: "The process cannot access the file because it is being used by another process." 但是,下面的代码块完成并保存了我的图像之后,该程序将引发System.IO异常:“该进程无法访问该文件,因为该文件正在被另一个进程使用。”
protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e) {
ListViewItem lvwItem = lvwItems.EditItem;
RadAsyncUpload upl = (RadAsyncUpload)lvwItem.FindControl("RadAsyncUpload1");
string target = MapPath(upl.TargetFolder);
try
{
foreach (UploadedFile file in upl.UploadedFiles) {
using (Bitmap originalImage = new Bitmap(file.InputStream)) {
System.Drawing.Image cloneImage = (Bitmap)originalImage.Clone();
// Resize the picture to max 200 * 200
Size mySize = new Size(200, 200);
cloneImage = Imaging.resizeImage(originalImage, mySize);
// Create a bitmap from the cloned image. Bitmap is needed for saveJpeg routine
Bitmap newImage = new Bitmap(cloneImage);
// Convert to jpg if necessary
if (file.GetExtension() != ".jpeg" || file.GetExtension() != ".jpg") {
Imaging.saveJpeg(Path.Combine(target, file.GetNameWithoutExtension() + ".jpg"), newImage, 100);
}
else {
}
// Now create a thumbnail
Size thumbSize = new Size(50, 50);
cloneImage = Imaging.resizeImage(originalImage, thumbSize);
Bitmap thumbImage = new Bitmap(cloneImage);
Imaging.saveJpeg(Path.Combine(target, file.GetNameWithoutExtension() + "_lille.jpg"), thumbImage, 100);
uploadedFileName = file.GetNameWithoutExtension() + ".jpg";
}
}
}
catch (IOException ex)
{
}
}
At this point, the two files (200*200 and 50*50) will exist, but upon exiting the routine, I receive the error. 此时,两个文件(200 * 200和50 * 50)将存在,但是退出例程后,我收到错误消息。
I tried closing and disposing all the images as well as the file.InputStream, but still see the error. 我尝试关闭并处置所有图像以及file.InputStream,但仍然看到错误。 I also tried seeing if the UploadedFile could just be discarded, but did not find that method to exist.
我还尝试查看UploadedFile是否可以被丢弃,但是没有找到该方法。
I'm fairly certain that there's a simple solution to it, but I've been staring at this for hours, and simply do not see it. 我相当确定有一个简单的解决方案,但是我已经盯着这个看了好几个小时了,根本看不到它。
Thanks. 谢谢。
There is a remark on Imagaining Save method MSDN page : 在“保存方法” MSDN页面上有一条评论:
Saving the image to the same file it was constructed from is not allowed and throws an exception.
不允许将图像保存到构造该图像的相同文件中,并引发异常。
Try to save Your file with different name. 尝试用其他名称保存您的文件。 If thats not possible, You can work around this,by reading file to memory first(fe using MemoryStream).
如果那不可能,则可以解决此问题,方法是先将文件读取到内存中(例如使用MemoryStream)。
从屏幕的右下角,从任务栏停止本地IIS服务器。
It turns out that my error was caused by the line: 原来,我的错误是由以下行引起的:
using (Bitmap originalImage = new Bitmap(file.InputStream))
I replaced that with: 我将其替换为:
using (Stream fileStream = file.InputStream) {
using (System.Drawing.Image originalImage = System.Drawing.Image.FromStream(fileStream)) {
and the error is gone. 错误消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.