0

I'm trying to scale an image and save that image to a new location. Now i'm having this error "A generic error occurred in GDI+ at System.Drawing.Image.Save".

I've found a couple of answers on stackoverflow, unfortunately without any results.

    public static void SaveImageScaled(String imagePath, string destPath, Size newSize, String filename)
    {
        using (Bitmap bmpOriginal = (Bitmap)Image.FromFile(imagePath))
        using (Bitmap bmpResized = new Bitmap(newSize.Width, newSize.Height))
        using (Graphics g = Graphics.FromImage(bmpResized))
        {
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(
                bmpOriginal,
                new Rectangle(Point.Empty, bmpResized.Size),
                new Rectangle(Point.Empty, bmpOriginal.Size),
                GraphicsUnit.Pixel);

            // Lower the quality;
            long qualityPercentage = 75L;

            ImageCodecInfo[] myImageCodecInfoList = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo myImageCodecInfo = myImageCodecInfoList.Where(x => x.FormatID == bmpOriginal.RawFormat.Guid).First();

            Encoder myEncoder = Encoder.Quality;
            EncoderParameters myEncoderParameters = new EncoderParameters(1);
            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, qualityPercentage);
            myEncoderParameters.Param[0] = myEncoderParameter;


            if (!Directory.Exists(destPath))
                Directory.CreateDirectory(destPath);

            // ERROR
            bmpResized.Save(destPath + filename, myImageCodecInfo, myEncoderParameters);               
        }
    }
Mark Rijsmus
  • 627
  • 5
  • 15
  • 1
    http://stackoverflow.com/questions/15862810/a-generic-error-occured-in-gdi-in-bitmap-save-method – Michael B. Aug 03 '14 at 11:44
  • Thank you Michael, although the endresult is the same, the root of my problem was that we did not have write access in the directory it just created. Problem is solved. – Mark Rijsmus Aug 04 '14 at 06:15

1 Answers1

0

This code created a directory with insufficient write-acces. Changed the permissions on a higher directory level, and solved the issue.

        if (!Directory.Exists(destPath))
            Directory.CreateDirectory(destPath);
Mark Rijsmus
  • 627
  • 5
  • 15