2012-04-20 51 views
0

或多或少像下面的例程適用於給定URL的幾個不同環境(例如http://covers.oreilly.com/images/0636920022886/bkt.gif),但在Azure上失敗以下例外:通過HTTP範圍請求快速檢測GIF尺寸

System.ArgumentException:參數無效。在在 System.Drawing.Image.FromStream(流流)

System.Drawing.Image.FromStream(流流,布爾 useEmbeddedColorManagement,布爾validateImageData)在所有情況下,在quetion該組件系統。 Drawing,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a,所以我假設收到的數據在Azure上不同。

public static DimensionResult Is404(string url) 
    { 
     DimensionResult result = null; 

     HttpWebRequest request = Http.PrepareGetRequest(new Uri(url), false, false, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"); 
     request.Timeout = 2500; 
     request.Method = "GET"; 
     request.AddRange(0, 2048); 
     request.KeepAlive = false; 
     request.AllowAutoRedirect = true; 

     try 
     { 
      result = new DimensionResult(); 

      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       result.ContentEncoding = response.ContentEncoding; 
       result.Url = response.ResponseUri.ToString(); 
       result.Is404 = (response.StatusCode != HttpStatusCode.PartialContent && response.StatusCode != HttpStatusCode.OK) || System.Text.RegularExpressions.Regex.IsMatch(response.ContentType, "text|html", System.Text.RegularExpressions.RegexOptions.IgnoreCase); 

       if (!result.Is404) 
         using (System.Drawing.Image image = System.Drawing.Image.FromStream(response.GetResponseStream())) 
         { 
          result.Width = image.Width; 
          result.Height = image.Height; 
         } 
      } 
     } 
     catch (Exception ex) 
     { 
      result.Exception = ex; 
      result.Is404 = true; 
     } 

     return result; 
    } 

請不要集中在請求的字節數(這是一個簡化的版本),但在.NET的網絡協議棧什麼設置可以解釋從服務器到服務器的一個差異反應?

在這兩種情況下,我到目前爲止已記錄的響應頭和他們是一樣的,沒有網絡痕跡尚未:

日期:星期五,二○一二年四月二十〇日11時47分05秒 GMT,服務器:Apache,Accept-Ranges:bytes,Last-Modified:Fri,24 Feb 2012 17:21:00 GMT,Content-Range:bytes 0-2048/3556,Content-Length:2049,Content-Type:image/GIF,緩存控制:最大年齡= 2592000,截止日期:孫老師,2012 GMT 11時47分05秒,連接 5月20日:關閉

更新: 我已經記錄了在兩種環境中收到的字節,它們碰巧是相同的!所以相同的響應頭,相同的響應長度,相同的響應內容,相同的程序集,不同的行爲。

回答

0

根據http://en.wikipedia.org/wiki/Graphics_Interchange_Format,gif文件以字符「GIF87a」或「GIF89a」開頭,後跟寬度/高度。因此,如果您只需要寬度/高度信息,則可以讀取字節6-9。 GDI +(System.Drawing.dll)不是必需的。根據我的經驗,在服務器環境中使用System.Drawing.dll,尤其是Windows Azure可能會導致不可預知的結果。如果您需要高級位圖處理,則可以使用WIC或WPF(在鉤子下使用WIC)。

最好的問候,

Ming Xu。