2012-07-07 72 views
0

我有下面的代碼編寫託管在Azure中ASHX文件:GDI +錯誤時ASHX部署到Azure的

public void ProcessRequest(HttpContext context) 
     { 
      int number; 
      try 
      { 
       number = int.Parse(context.Request.QueryString["number"]); 
      } 
      catch 
      { 
       number = -1; 
      } 

      var bitmap = new Bitmap(173, 173); 
      DrawImage(bitmap, number); 
      context.Response.ContentType = "image/png"; 
      bitmap.Save(context.Response.OutputStream, ImageFormat.Png); 
      context.Response.Flush(); 
     } 

     private void DrawImage(Bitmap bitmap, int number) 
     { 
      using (var graphics = Graphics.FromImage(bitmap)) 
      { 
       graphics.FillRectangle(GetBrush(number), 0, 0, 173, 173); 

       Font font = new Font("Segoe UI", 40f, FontStyle.Bold); 
       StringFormat textFormat = new StringFormat 
               { 
                Alignment = StringAlignment.Far, 
                LineAlignment = StringAlignment.Center 
               }; 
       Rectangle rectangle = new Rectangle(0, 0, 160, 173); 
       graphics.DrawString("76", font, Brushes.Black, rectangle, textFormat); 

       font = new Font("Arial", 16, FontStyle.Bold); 
       textFormat = new StringFormat 
           { 
            Alignment = StringAlignment.Near, 
            LineAlignment = StringAlignment.Near 
           }; 
       rectangle = new Rectangle(10, 10, 163, 163); 
       graphics.DrawString("YOUR NUMBER", font, Brushes.Black, rectangle, textFormat); 
      } 
     } 

在本地運行這段代碼完全進行處理併產生正確的結果。部署到Azure時,出現以下錯誤:

GDI +中發生了一般性錯誤。

描述:在執行 當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.Runtime.InteropServices.ExternalException:GDI +中發生 一般錯誤。

任何人有任何想法?在此先感謝

回答

0

所以,我找到了這個問題的答案,或至少一個這個問題的答案。

感謝Rick Strahls blog entry。雖然位圖克隆技術是朝着正確方向邁出的一步(爲什麼是微軟?)顯然,當您嘗試從ashx輸出PNG時,您必須特別考慮。

這種考慮基本上是將位圖保存到MemoryStream中,然後再將其保存到實際位置。