A generic error occurred in GDI+ error on live server. but not for all images
-
06-06-2023 - |
Question
I am having "A generic error occurred in GDI+" error on my live server. On local there is no any error. I've checked plenty of post about that. Poeople say it can happen because of write permission, or wrong path or some image formats.(like png)
But in my case non of them is the reason. Because I am be able upload mostly. So it means writing permission are ok. Image formats ok. etc.
But for some images, this error comes up. I am not be able to debug or get detail information about error because its only happens on live server. For same image which is throwing exception on live server, works on localhost.
Any idea? any another reason for this error that I have to look at?
Thanks in advance.
Save process
private string ResizeAndSaveImage(FileUpload file, int maxWidth, int maxHeight, string rootPath)
{
string ext = System.IO.Path.GetExtension(file.FileName).TrimStart(".".ToCharArray()).ToLower();
Bitmap uploadedImage = new Bitmap(file.FileContent);
Bitmap resizedImage = Utility.GetResizedImage(uploadedImage, maxWidth, maxHeight);
//Save the image
string urlName = System.Guid.NewGuid().ToString() + "." + ext;
string virtualPath = rootPath + urlName;
string tempFileName = Server.MapPath(virtualPath);
resizedImage.Save(tempFileName, uploadedImage.RawFormat);
return urlName;
}
Resize method
public static Bitmap GetResizedImage(Bitmap source, int width, int height)
{
//This function creates the thumbnail image.
//The logic is to create a blank image and to
// draw the source image onto it
Bitmap thumb = new Bitmap(width, height);
Graphics gr = Graphics.FromImage(thumb);
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.CompositingQuality = CompositingQuality.HighQuality;
gr.DrawImage(source, 0, 0, width, height);
return thumb;
}
event details
<Data>ExternalException</Data>
<Data>A generic error occurred in GDI+. at
System.Drawing.Image.Save(String filename, ImageCodecInfo encoder,
EncoderParameters encoderParams) at
OasisEstateUI.AdminPages.ApartmentManagement.ResizeAndSaveImage(FileUpload
file, Int32 maxWidth, Int32 maxHeight, String rootPath) at
OasisEstateUI.AdminPages.ApartmentManagement.btnSaveApartmentImage_Click(Object
sender, EventArgs e) at
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint)</Data>
No correct solution