2010-02-01 58 views
2

我有一個GDI +一般性錯誤,我已經試過什麼大家都說這是確保包含正在讀像這樣GDI +一般性錯誤ASP.NET MVC

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

我的圖像文件的文件夾已將該文件夾的權限設置給所有人,但仍然出現此錯誤?

任何想法爲什麼?

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, Func 1 continuation) +251 System.Web.Mvc.<>c__DisplayClass13.<InvokeActionResultWithFilters>b__10() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList 1 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

回答

2

我已經找到了我發現一點上的手指安全權限您是從讀取圖像的文件夾的東西,要解決這個,拇指的一般規則和所有。然而,這並非總是如此。

直到我真正通過遠程訪問進入服務器,然後逐步查看代碼,看看它在哪裏得到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 +的異常

+3

注意:您應該使用Using語句,例如'using(var FullsizeImage = Image.FromFile(..)){croppedImage = ...}',並且不需要手動調用.Dispose()。好處是更簡潔的代碼,再加上如果在塊中拋出異常,它仍然會被丟棄。 – gregmac 2013-01-08 23:37:40

相關問題