1

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.

4

3 回答 3

3

There is a remark on Imagaining Save method MSDN page:

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(f.e. using MemoryStream).

于 2012-08-07T06:20:10.853 回答
2

Stop your local IIS Server from the bottom-right right corner of your screen, from the taskbar.

于 2013-08-03T07:30:55.087 回答
1

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.

于 2012-08-07T19:21:58.977 回答