Error when trying to overwrite an image (it succeeds the first time after iis reset )

Posted by Omu on Stack Overflow See other posts from Stack Overflow or by Omu
Published on 2010-05-01T15:22:03Z Indexed on 2010/05/01 15:27 UTC
Read the original article Hit count: 215

Filed under:
|
|
|
|

First time (after iis reset) I succeed to overwrite the image, but if I try again it gives me that GDI error

this is my code:

 [HttpPost]
    public ActionResult Change()
    {
        var file = Request.Files["fileUpload"];
        if (file.ContentLength > 0)
        {
            var filePath = @ConfigurationManager.AppSettings["storagePath"] + @"\Temp\" + User.Identity.Name + ".jpg";
            using (var image = Image.FromStream(file.InputStream))
            {
                var size = ResizeImage(image, filePath, 600, 480, true);
                return RedirectToAction("Crop", new CropDisplay {ImageWidth = size[0], ImageHeight = size[1]});
            }
        }

        return RedirectToAction("Index");
    }


     private int[] ResizeImage(Image image, string newFilePath, int newWidth, int maxHeight, bool onlyResizeIfWider)
        {
           ...

            using (var thumbnail = new Bitmap(newWidth, newHeight))
            {
                using (var graphic = Graphics.FromImage(thumbnail))
                {

                    graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    graphic.SmoothingMode = SmoothingMode.HighQuality;
                    graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    graphic.CompositingQuality = CompositingQuality.HighQuality;

                    graphic.DrawImage(image, 0, 0, newWidth, newHeight);

                    var info = ImageCodecInfo.GetImageEncoders();
                    var encoderParameters = new EncoderParameters(1);
                    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
//this is where I get the GDI error
                    thumbnail.Save(newFilePath, info[1], encoderParameters);

                    return new[] { newWidth, newHeight };
                }
            }
        }

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about image