1

I'm working on a website where the user can upload some images. And I'm using HttpPostedFileBase to get the uploaded image

Here is the Action header:

public async Task<ActionResult> Create(Advert advert, HttpPostedFileBase FileURL)

I can save the image using FileURL.SaveAs(HttpContext.Server.MapPath(fileName)) where fileName is a parameter i generate randomly, and everything is working perfectly.

Now I want to generate a thumbnail for my image, so I wrote the next method:

public static void SaveJpeg(string path, HttpPostedFileBase uploadedFile, int quality)
    {
        if (quality < 0 || quality > 100)
            throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");

        // Encoder parameter for image quality 
        EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        // JPEG image codec 

        ImageCodecInfo codec = GetEncoderInfo("image/jpeg");
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;
        Image img = Image.FromStream(uploadedFile.InputStream);
        var thumbnail = img.GetThumbnailImage(img.Width / 5, img.Height / 5, null, IntPtr.Zero);
        //img.Save(path, codec, encoderParams);
        thumbnail.Save(path, codec, encoderParams);
    }

But I'm getting A generic error occured in GDI+ error when saving the thumbnail

PS: I tested my code on local image (passing Image to the method) and everything worked well.

Please help.

Thanks in advance :)

wael razouk
  • 88
  • 2
  • 8
  • I've never used that function, but the MSDN docs [say](https://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage(v=vs.110).aspx) that `callback` is required : "You must create a delegate and pass a reference to the delegate as the callback parameter, but the delegate is not used." Passing in `null` might be a problem in some cases. – Bradley Uffner Sep 22 '17 at 19:59
  • Scroll down to the "Caution" section at MSDN, https://msdn.microsoft.com/en-us/library/system.drawing(v=vs.110).aspx. Microsoft does not support using such classes in ASP.NET. You have to use third party solutions. – Lex Li Sep 23 '17 at 00:42

1 Answers1

0

You may already have set this, but this kind of error can occur if the permissions are not set correctly on the directory you are saving to. By default, sites in IIS don't have permissions to write to the IIS directories. The default user is IIS_IUSRS and will need to be given Modify access if this is the issue.

enter image description here

Joshua Morgan
  • 678
  • 6
  • 18
  • I don't think this is the case, since I was able to save images in the same directory using `FileURL.SaveAs(HttpContext.Server.MapPath(fileName))` – wael razouk Sep 23 '17 at 07:21