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