A generic error occurred in GDI+ using SharpMap

Topics: Algorithms, CompactFramework, Data Access, Español, General Topics, Italiano, SharpMap Project, SharpMap v0.9 / v1.x, SharpMap v2.0, Web Controls, WinForms Controls
Feb 14, 2011 at 5:15 PM

Hi all,

I am getting an error when I try to save a image via Map class, with code below:


SharpMap.Map map = new SharpMap.Map(new System.Drawing.Size(width, hight));


System.Drawing.Image mapImage = map.GetMap();
mapImage.Save(outputStream, ImageFormat.Png);

Exception below:

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+.

Stack Trace:

[ExternalException (0x80004005): A generic error occurred in GDI+.]
   System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +461522
   System.Drawing.Image.Save(Stream stream, ImageFormat format) +36
   MyApp.Domain.Services.Helpers.MapImageHelper.GenerateTimeLineMapImage(List`1 shapes, String disclaimer, Int32 width, Int32 hight, Stream& output) in d:\projects\Observatory\Trunk\Observatory.Domain\Services\Helpers\MapImageHelper.cs:92
   MyApp.Domain.Services.ShapeService.GenerateTimeLineMapImage(Stream output, List`1 shapes, Int32 width, Int32 height) in d:\projects\MyApp\Trunk\MyApp.Domain\Services\ShapeService.cs:551
   MyApp.Domain.Services.ShapeService.GenerateTimelineImage(Int32 subjectId, Int32 parentId, Int32 periodicityId, DateTime period, Stream output, Int32 userId, Int32 width, Int32 height) in d:\projects\MyApp\Trunk\MyApp.Domain\Services\ShapeService.cs:352
   MyApp.Web.Areas.Service.Controllers.ShapeController.TimelineImage(Int32 subjectId, Int32 parentId, Int32 periodicityId, DateTime period) in d:\projects\MyApp\Trunk\MyApp.Web\Areas\Service\Controllers\ShapeController.cs:117
   lambda_method(Closure , ControllerBase , Object[] ) +261
   System.Web.Mvc.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8836913
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

Feb 15, 2011 at 3:06 PM

Hello kingofwebguru,

this is a known issue with SharpMap in server contexts. Since I don't do any SharpMap with servers, I cannot help you with it.

Cheers FObermaier

Feb 15, 2011 at 6:50 PM

It's hard to understand what's happens using this poor information, I suppose that you're using sharpmap 0.9: I've used a lot in server contexts (a WMS server) without any kind of problem,

so I think that the first thing you can check is for concurrency uses of the map object.

try to lock the code that generates the map and see what happens.

lock (myStaticObject)
System.Drawing.Image mapImage = map.GetMap();
mapImage.Save(outputStream, ImageFormat.Png);

if this works we can start fix your code.

Feb 15, 2011 at 7:54 PM

It is used in a Web application. Could it be something to do with the temporary folder that is used to store temporary file by ASP.NET behind the scenes, and the folder does NOT has write permission?

Feb 15, 2011 at 7:55 PM


Thank you for help alway!

Feb 15, 2011 at 8:04 PM

A generic error in GSI+can be many things. My bet would be a problem with multiple access to outputStream. I'd follow D Guidi's suggestion and try to determine if the problem is related to concurrent access.

Feb 15, 2011 at 8:21 PM

Thanks. That could be a reason. However, the error occured when there is only ONE Image instance and one outputStream being used. Furthermore, it is working in Windows 7 development pc. But the error occured on a Cloud enviorment.

Feb 16, 2011 at 6:45 AM

>Furthermore, it is working in Windows 7 development pc. But the error occured on a Cloud enviorment.

So in a local environment all works fine? Maybe cloud environment have some GDI restrictions? The outputstream is from what kind of resource? Try to use a memorystream and see if the error happens again.

Feb 16, 2011 at 12:21 PM



In ASP.NET, Response object is used! MemeryStream can resolve the problem, which leads to my thinking that it relates to write permission issue.


Thanks again!