Here is my Image resizing code.
private void ResizeImage(int width, int height, string szFilePath, string destinationPath)
{
//var watch = new System.Diagnostics.Stopwatch();
//watch.Start();
Serilog.Log.ForContext<ImageResizeHandler>().Information($"Image resize process start for file :{szFilePath}");
var instructions = getInstructions(width, height);
try
{
ImageResizer.ImageBuilder.Current.Build(szFilePath, szFilePath, instructions);
File.Move(szFilePath, destinationPath);
//watch.Stop();
Serilog.Log.ForContext<ImageResizeHandler>().Information($"Image resize process end for file :{szFilePath}");
}
catch (Exception ex)
{
Serilog.Log.ForContext<ImageResizeHandler>().Error(ex, $"Failed to Resize the Image - filePath: {szFilePath}");
// If memory leak exception or any other error then use default thumbnail image.
File.Copy(HttpContext.Current.Server.MapPath("~/Images/placeholder_160x180.png"), destinationPath);
}
}
While resizing image, I am getting following error
System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(Stream stream, ImageFormat format)
at ImageResizer.Plugins.Basic.DefaultEncoder.SavePng(Image img, Stream target)
at ImageResizer.Plugins.Basic.DefaultEncoder.Write(Image image, Stream s)
at ImageResizer.ImageBuilder.BuildJobBitmapToStream(ImageJob job, Bitmap source, Stream dest)
at ImageResizer.ImageBuilder.BuildJob(ImageJob job)
at ImageResizer.ImageBuilder.BuildInternal(ImageJob job)
at ImageResizer.ImageBuilder.BuildInQueue(ImageJob job, Boolean useSemaphore, Int32 maxQueuingMilliseconds, CancellationToken cancel)
at ImageResizer.ImageBuilder.Build(ImageJob job)
at ImageResizer.ImageBuilder.Build(Object source, Object dest, Instructions instructions)
at CampaignerImageResizer.ImageResizeHandler.ResizeImage(Int32 width, Int32 height, String szFilePath, String destinationPath) in C:agent_work49sCampaignerImageResizerImageResizeHandler.ashx.cs:line 56
getting exception "A generic error occurred in GDI+." on line ImageResizer.ImageBuilder.Current.Build(szFilePath, szFilePath, instructions);
I am using this library https://imageresizing.net/
Please help me on this issue.
2
Answers
This is likely because you’re writing to the same file you’re reading from. Write to destinationPath instead and the issue shojl
The code seems fine, but it is missing the implementation of the
getInstructions()
method used to retrieve the resizing instructions for theImageBuilder object.
To fix this error, you need to implement the
getInstructions()
method to return the correct instructions for resizing the image to the desired dimensions.