简体   繁体   English

通过Http范围请求快速检测GIF尺寸(本地部署,在Azure上失败)

[英]Fast Detection of GIF Dimensions Via Http Range Requests (Works On-Premises, Fails on Azure)

Something more or less like the routine below works in couple of different environments for a given URL (eg http://covers.oreilly.com/images/0636920022886/bkt.gif ) but fails on Azure with the following exception: 对于给定的URL(例如http://covers.oreilly.com/images/0636920022886/bkt.gif ),下面的例程或多或少地在几个不同的环境中工作,但在Azure上失败,但以下情况除外:

System.ArgumentException: Parameter is not valid. System.ArgumentException:参数无效。 at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) at System.Drawing.Image.FromStream(Stream stream) 在System.Drawing.Image.FromStream(Stream stream)处,在System.Drawing.Image.FromStream(Stream stream,布尔useEmbeddedColorManagement,布尔validateImageData)处

In all cases, the assembly in quetion is System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, so I'm assuming the data received is different somehow on Azure. 在所有情况下,所要查询的程序集都是System.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;
    }

Please don't focus on the number of bytes requested (this is a simplified version) but on what settings in the networking stack of .NET could account for a difference response from server to server? 请不要只关注请求的字节数(这是简化版本),而是要关注.NET网络堆栈中的哪些设置可以解释服务器之间的响应差异?

In both cases, I've logged the response headers so far and they're identical, no network traces yet: 在这两种情况下,到目前为止,我已经记录了响应头,并且它们是相同的,还没有网络跟踪:

Date:Fri, 20 Apr 2012 11:47:05 GMT,Server: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,Cache-Control:max-age=2592000,Expires:Sun, 20 May 2012 11:47:05 GMT,Connection:close 日期:2012年4月20日星期五,格林尼治标准时间,服务器:Apache,接受范围:字节,上次修改时间:2012年2月24日星期五,格林尼治标准时间,内容范围:字节0-2048 / 3556 ,Content-Length:2049,Content-Type:image / gif,Cache-Control:max-age = 2592000,Expires:Sun,2012年5月20日11:47:05 GMT,Connection:close

UPDATE : I've logged the bytes received in both environments and they happen to be identical! 更新 :我已经记录了在两种环境下收到的字节,它们恰好是相同的! So same response headers, same response length, same response content, same assembly, different behavior. 因此,相同的响应标头,相同的响应长度,相同的响应内容,相同的程序集,不同的行为。

According to http://en.wikipedia.org/wiki/Graphics_Interchange_Format , a gif file begins with the character “GIF87a” or “GIF89a”, followed by its width/height. 根据http://en.wikipedia.org/wiki/Graphics_Interchange_Format的说明 ,gif文件以字符“ GIF87a”或“ GIF89a”开头,然后是宽度/高度。 So if all you need is the width/height information, you can read bytes 6-9. 因此,如果您只需要宽度/高度信息,则可以读取6-9字节。 The GDI+ (System.Drawing.dll) is not needed. 不需要GDI +(System.Drawing.dll)。 From my experience, using System.Drawing.dll in server environments and in particular Windows Azure can lead to unpredictable results. 根据我的经验,在服务器环境(尤其是Windows Azure)中使用System.Drawing.dll可能会导致不可预测的结果。 If you need advanced bitmap processing, you can use WIC or WPF (which uses WIC under the hook). 如果需要高级位图处理,则可以使用WIC或WPF(在挂钩下使用WIC)。

Best Regards, 最好的祝福,

Ming Xu. 徐明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure中的WCF配置错误:无法识别的属性'protectTokens'。 在本地.NET 4环境中正常工作 - WCF config error in Azure: Unrecognized attribute 'protectTokens'. Works fine in on-premises .NET 4 environment 从Azure群集访问本地网络共享 - Accessing on-premises network share from Azure cluster 使用 Azure Function 更新本地 AD 用户配置文件 - Update On-premises AD user profile using Azure Function Azure Cloud Service主机本地应用程序服务器 - Azure Cloud Service host on-premises application server 用于本地 Web 应用的 Azure Application Insight 遥测 - Azure Application Insight Telemetry for On-Premises web app 使用Azure上托管的Web应用程序访问本地SQL数据库 - Accessing on-premises SQL database with web application hosted on Azure 从本地IIS应用程序访问Azure Blob存储 - Access Azure Blob Storage from on-premises IIS application .NET本地部署? - .NET Batch on-premises? 我正在尝试使用 Windows 身份验证从我的 Azure 函数连接 SQL Server(本地) - I am trying to connect SQL Server(on-premises) from my Azure Function with windows authentication 通过AMQP从Linux上的Java客户端使用本地内部服务总线时,我们到底需要做什么? - What exactly do we need to do to work with Service Bus on-premises from a Java client on Linux via AMQP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM