2012-04-20 47 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。