【问题标题】:GDI+ Generic Error ASP.NET MVCGDI+ 通用错误 ASP.NET MVC
【发布时间】:2011-01-11 19:22:08
【问题描述】:

我遇到了一个 GDI+ 通用错误我已经尝试了大家所说的,即确保包含正在读取的图像文件的文件夹像这样

  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 };
        }

我已将该文件夹的权限设置为所有人,但仍然出现此错误?

有什么想法吗?

GDI+ 中出现一般错误。 描述:未处理的异常 在执行过程中发生 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。

异常详情: System.Runtime.InteropServices.ExternalException: GDI+ 中出现一般错误。

来源错误:

产生了一个未处理的异常 在当前执行期间 网络请求。有关的信息 异常的起源和位置 可以使用异常识别 下面的堆栈跟踪。

堆栈跟踪:

[外部异常(0x80004005):A GDI+ 中出现一般错误。]
System.Drawing.Image.Save(流 流,ImageCodecInfo 编码器, 编码器参数编码器参数) +378002 System.Drawing.Image.Save(流 流,ImageFormat 格式)+36
哈瓦那.ImageResult.ExecuteResult(ControllerContext 上下文)在 C:\DropBox\My Dropbox\Havana\Havana.MVC\Infrastructure\ImageResult.cs:44 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext 控制器上下文,动作结果 行动结果)+10
System.Web.Mvc.c__DisplayClass11.b__e() +20 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter 过滤器,ResultExecutingContext preContext,Func1 continuation) +251 System.Web.Mvc.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 过滤器, ActionResult actionResult) +178
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext 控制器上下文,字符串动作名称) +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

【问题讨论】:

    标签: asp.net-mvc gdi+


    【解决方案1】:

    我已经找到了解决方案,通常是经验法则和我发现的所有内容都指向您正在从中读取图像的文件夹的安全权限。然而,情况并非总是如此。

    直到我真正通过远程访问进入服务器,并单步执行代码以查看它在哪里获取 GDI+ 通用异常......我才能够找到一篇关于 Rick Strahl 的精彩文章用我的解决方案写博客。 Common problems with rendiering bitmaps into ASP.NET Output stream

    基本上,您必须确保在处理完原始对象后将其处理掉。 例如,在我的 ImageResult 操作中,我这样做了

        Image FullsizeImage = Image.FromFile(
            imgstr.getFilePath(profile, false, PhotoDB.PhotoSize.None)
            );
    
    
        Image cropedImage = imgstr.Crop(FullsizeImage, width, width, PhotoDB.AnchorPosition.Center);
        FullsizeImage.Dispose();
    

    在我使用它并将它放入croppedImage 后我会处理它。我以前没有这样做过。所以我得到了 GDI+ 异常

    【讨论】:

    • 注意:您应该使用Using语句,例如using(var FullsizeImage = Image.FromFile(..)) { croppedImage = ... },并且不需要手动调用.Dispose()。好处是代码更简洁,而且如果在块中抛出异常,它仍然会被处理掉。
    猜你喜欢
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2014-01-21
    • 2012-08-25
    • 2017-08-03
    相关资源
    最近更新 更多