My program uses a Telerik ASyncUpload control in order to allow the user to upload images. 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).
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."
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.
I tried closing and disposing all the images as well as the file.InputStream, but still see the error. I also tried seeing if the UploadedFile could just be discarded, but did not find that method to exist.
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.