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 };
}
}
}