2012-02-07 81 views
0

我正在部署一個MVC3應用程序,它將圖像存儲在數據庫中。要求是做一個GET從數據庫中檢索圖像,然後將圖像寫入響應流。所有工作都很好(當然)。SecurityException調用Graphics.DrawImage

我使用共享的託管服務提供商(是的,沒有完全信任,<trust>被阻止)。我用這ImageResult返回圖像:

public override void ExecuteResult(ControllerContext context) 
     { 
      using (Bitmap bitmap = new Bitmap(width, height)) 
      { 
       using (Graphics graphics = Graphics.FromImage(bitmap)) 
       { 
        Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(width - 1, height - 1)); 

        graphics.DrawImage(Image.FromHbitmap(_imageBitmap.GetHbitmap()), 0, 0); 

        context.HttpContext.Response.ContentType = "image/jpg"; 
        bitmap.Save(context.HttpContext.Response.OutputStream, ImageFormat.Jpeg); 
       } 
      } 
     } 

我已經找到了錯誤的地方。我打電話graphics.DrawImage()行。這裏是堆棧:

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. 
    at TM.Web.ImageResult.ExecuteResult(ControllerContext context) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) 
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) 
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) 
    at System.Web.Mvc.Controller.ExecuteCore() 
    at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) 
    at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) 
    at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5() 
    at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0() 
    at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() 
    at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d() 
    at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) 
    at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) 
    at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) 
    at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
The action that failed was: 
LinkDemand 
The type of the first permission that failed was: 
System.Security.Permissions.SecurityPermission 

我也知道在網絡上使用GDI +並不完全支持。有沒有更好的方法來做到這一點?或者至少有一種方法不會讓部分信任失效?多謝。

回答

1

那麼,事實證明我過去了,這使複雜化。我發現這個good post解釋了爲什麼我不能將數據庫中的位圖保存到輸出流。

這導致我試圖消除GDI +呼叫。我結束了這個工程很漂亮(_imageBitmap已從數據庫中拉出):

public override void ExecuteResult(ControllerContext context) 
{ 
    using (Bitmap bitmap = new Bitmap(_imageBitmap)) 
    { 
      context.HttpContext.Response.ContentType = "image/jpg"; 
      bitmap.Save(context.HttpContext.Response.OutputStream, ImageFormat.Jpeg); 
    } 
}