2
votes

I am having a GDI+ generic error I've tried what everyone says which is make sure that the folder containing the image file that is being read in like so

  public ImageResult ProfileAsset(string profile, int width, int height) {
            PhotoDB imgstr = new PhotoDB();

            Image FullsizeImage = Image.FromFile(
                imgstr.getFilePath(profile, false, PhotoDB.PhotoSize.None)
                );


            Image cropedImage = imgstr.Crop(FullsizeImage, width, width, PhotoDB.AnchorPosition.Center);
            return new ImageResult { Image = cropedImage, ImageFormat = ImageFormat.Png };
        }

I have set the permissions on that folder to everyone but still get this error?

Any ideas why?

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:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ExternalException (0x80004005): A generic error occurred in GDI+.]
System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +378002 System.Drawing.Image.Save(Stream stream, ImageFormat format) +36
Havana.ImageResult.ExecuteResult(ControllerContext context) in C:\DropBox\My Dropbox\Havana\Havana.MVC\Infrastructure\ImageResult.cs:44 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +10
System.Web.Mvc.<>c__DisplayClass11.b__e() +20 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) +251 System.Web.Mvc.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +178
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399 System.Web.Mvc.Controller.ExecuteCore() +126 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

1

1 Answers

2
votes

I have found the solution to this, generally the rule of thumb and all of the stuff I have found point it's finger to security rights to the folder you are are reading the image from. However this is not always the case.

It wasn't until I actually went up into the server via my remote access, and stepped through the code to see where it was getting the GDI+ Generic Exception... that I was able to find a great article on Rick Strahl's blog with my solution. Common problems with rendiering bitmaps into ASP.NET Output stream

Basically it comes down to you got to make sure to dispose the original object when done with it. for instance in my ImageResult action I did this

    Image FullsizeImage = Image.FromFile(
        imgstr.getFilePath(profile, false, PhotoDB.PhotoSize.None)
        );


    Image cropedImage = imgstr.Crop(FullsizeImage, width, width, PhotoDB.AnchorPosition.Center);
    FullsizeImage.Dispose();

Notice after I use it and put it into croppedImage I dispose of it.. I didn't do this before.. and so I was getting the GDI+ exception