0

I'm trying to transform and resize the image before saving it.

When the system tries to save the Image, it shows an error message (System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.').

Could you please help me to understand why I have this error.

Find the code below:

private void ImageProcessingAndSaving(string imagePath, string exportPath)
        {
            Bitmap originalImage = new Bitmap(imagePath);
            int desiredWidth = 600;
            int desiredHeight = 600;
            //Create a new Bitmap with the desired dimensions
            Bitmap resizedImage = new Bitmap(desiredWidth, desiredHeight);

            //Set the resolution and interpolation mode: Adjust the resolution and interpolation mode to ensure high-quality resizing
            resizedImage.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
            using (Graphics graphics = Graphics.FromImage(resizedImage))
            {
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            }

            //Perform the resize operation: Use the Graphics.DrawImage method to resize the original image to the desired dimensions

            using (Graphics graphics = Graphics.FromImage(resizedImage))
            {
                graphics.DrawImage(originalImage, 0, 0, desiredWidth, desiredHeight);
                
                //ERROR HAPPENS HERE WHILE SAVING;

                //Save the resized image: Save the resized image to a file or any other desired output stream.
                resizedImage.Save(exportPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            //Dispose of the objects: Release the resources held by the Bitmap objects
            originalImage.Dispose();
            resizedImage.Dispose();

        }
Mike
  • 38
  • 7
  • Code works fine. Either the imagePath is incorrect or the file is incorrect/damaged – VDWWD Jul 03 '23 at 11:13
  • Do not save the image while the `Graphics` is still in use. Put `resizeImaged.Save` outside of the `using` block. Besides, using `Graphics.DrawImage` is not recommended in an ASP.NET application because it uses a process-wide lock so all of your concurrent resizing operations will be blocked. If you cannot migrate away from `System.Drawing` try to use [another way](https://docs.kgysoft.net/drawing/html/M_KGySoft_Drawing_BitmapExtensions_Resize.htm) for resizing ([NuGet](https://www.nuget.org/packages/KGySoft.Drawing)) Disclaimer: I'm the author of this library. – György Kőszeg Jul 03 '23 at 11:33
  • Furthermore, you might want to combine those separate `using` blocks. The 1st one has no effect as it just sets some parameters and then disposes the configured `Graphics` without doing anything with it. So the 2nd `Graphics` will just have the default parameters again. – György Kőszeg Jul 03 '23 at 11:42
  • @GyörgyKőszeg Thank you man, could you please add a small sample, so I can follow your steps to save the new image. Appreciated. – Mike Jul 04 '23 at 13:10
  • @GyörgyKőszeg I already installed your library, just I need a small sample method that get my image save successfully. Because I tried using this library but the output image is only black. – Mike Jul 04 '23 at 13:14
  • It's quite trivial to use it (just call `Resize` on a `Bitmap` and then save it) so if it's not working it can be because it's not quite supported in an ASP.NET service as noted in the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.drawing). See also [this](https://stackoverflow.com/q/1528525/5114784) question that looks quite similar to this one (ASP.NET, resizing by GDI+ `System.Drawing`, similar exceptions, saving into JPEG. – György Kőszeg Jul 04 '23 at 21:09

0 Answers0