0

This is the error i am getting right now

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)

i am using this same code on other website in same machine and there is no problem i really don't know what's going on please give me solution

here is my code

double newHeight = 0;
        double newWidth = 0;
        double scale = 0;

        //create new image object
        Bitmap curImage = new Bitmap(filePath);

        //Determine image scaling
        if (curImage.Height > curImage.Width)
        {
            scale = Convert.ToSingle(size) / curImage.Height;
        }
        else
        {
            scale = Convert.ToSingle(size) / curImage.Width;
        }
        if (scale < 0 || scale > 1) { scale = 1; }

        //New image dimension
        newHeight = Math.Floor(Convert.ToSingle(curImage.Height) * scale);
        newWidth = Math.Floor(Convert.ToSingle(curImage.Width) * scale);

        //Create new object image
        Bitmap newImage = new Bitmap(curImage, Convert.ToInt32(newWidth), Convert.ToInt32(newHeight));
        Graphics imgDest = Graphics.FromImage(newImage);
        imgDest.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        imgDest.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        imgDest.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        imgDest.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
        EncoderParameters param = new EncoderParameters(1);
        param.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

        //Draw the object image
        imgDest.DrawImage(curImage, 0, 0, newImage.Width, newImage.Height);

        //Save image file
        newImage.Save(saveFilePath, info[1], param);

        //Dispose the image objects
        curImage.Dispose();
        newImage.Dispose();
        imgDest.Dispose();
    }
10
  • Are you sure you have access to write to that path? And the directory exists? Commented Feb 21, 2014 at 23:13
  • What exactly is your code doing here? Are you writing into a stream? Commented Feb 21, 2014 at 23:14
  • Is there an InnerException to the error? Commented Feb 21, 2014 at 23:21
  • my code is to get image from one driectory and save into other and but also do resize aswell Commented Feb 21, 2014 at 23:22
  • @JayZee we will need some code... Commented Feb 21, 2014 at 23:23

1 Answer 1

2

You will get a GDI error message if the account you're running under cannot save to the path at saveFilePath on this line:

newImage.Save(saveFilePath, info[1], param);
Sign up to request clarification or add additional context in comments.

3 Comments

so what should i do ?
First verify this is your problem (you can try having it save a plain text file out to the same location.) Once you know that's the issue, you either need to run it under (or impersonate) a different account (one with the access), give the account it is running as access to that location, or save the file somewhere it already has access to. See here
You can also look at the user the App Pool is running under (maybe Network Service) and give that user appropriate rights to the folder, rather than using impersonation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.