Link to home
Start Free TrialLog in
Avatar of westdh
westdhFlag for United States of America

asked on

A generic error occurred in GDI+. using asp.net 4.0

trying to add an image (jpg) file.

A generic error occurred in GDI+.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: see details.
A generic error occurred in GDI+. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

Source Error: 


Line 1118:
Line 1119:             graph.DrawImage(image, rect);
Line 1120:             mainImage.Save(path + TreasureImageUploadFile.FileName, image.RawFormat);
Line 1121:
Line 1122:             TreasuresofLegacy.ImageResize thumb = new ImageResize(image.Width, image.Height, 150);
 

Source File: c:\HostingSpaces\hostadm\controlpanel.treasuresof.com\wwwroot\EditTreasure.aspx.cs    Line: 1120 

--------

Stack Trace: 


[ExternalException (0x80004005): A generic error occurred in GDI+.]
   System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +461268
   System.Drawing.Image.Save(String filename, ImageFormat format) +69
   Admin_EditTreasure.TreasureImageUpload_Manual(Object o, EventArgs e) in c:\HostingSpaces\hostadm\controlpanel.treasuresof.com\wwwroot\EditTreasure.aspx.cs:1120
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
kaufmed, you're right. It's safe to call System.IO.Directory.CreateDirectory( path ) even if path already exists.
I would also recommend using Path.Combine( path, file ) instead of path + file. So, the code would look as follows:

// using System.IO;

graph.DrawImage(image, rect);
Directory.CreateDirectory(path);
mainImage.Save(Path.Combine(path, TreasureImageUploadFile.FileName), image.RawFormat);
TreasuresofLegacy.ImageResize thumb = new ImageResize(image.Width, image.Height, 150);

Open in new window

Avatar of westdh

ASKER

This did not work:

I modified the code as per 'JustAndrei:' and now I do not get a GDI+ error.
I get no error just a [X] box image.

see code below with modification;
protected void TreasureImageUpload_Manual(object o, EventArgs e)
    {
         string path = System.Web.Configuration.WebConfigurationManager.AppSettings["ImagePath"];

         if (File.Exists(path + TreasureImageUploadFile.FileName))
         {
             ManualUploadError_FileExists.Visible = true;
         }
         else if (TreasureImageUploadFile.HasFile)
         {

             System.Drawing.Image image = System.Drawing.Image.FromStream(TreasureImageUploadFile.FileContent);

             ImageResize main = new ImageResize(image.Width, image.Height, 290);

             System.Drawing.Image mainImage = new System.Drawing.Bitmap(main.ThumbWidth, main.ThumbHeight, image.PixelFormat);
             System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, main.ThumbWidth, main.ThumbHeight);
             System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(mainImage);

             graph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
             graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             graph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

             /// Original code: this give GDI+ error
             /// graph.DrawImage(image, rect);
             /// mainImage.Save(path + TreasureImageUploadFile.FileName, image.RawFormat);
             /// TreasuresofLegacy.ImageResize thumb = new ImageResize(image.Width, image.Height, 150);

             /// Last Modify code: This gives a [+] blank image
             graph.DrawImage(image, rect);
             Directory.CreateDirectory(path);
             mainImage.Save(Path.Combine(path, TreasureImageUploadFile.FileName), image.RawFormat);
             TreasuresofLegacy.ImageResize thumb = new ImageResize(image.Width, image.Height, 150);

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of westdh

ASKER

Thanks