代码之家  ›  专栏  ›  技术社区  ›  Dan Bailiff

将httpwebrequest与动态uri一起使用会导致image.fromstream中的“参数无效”

  •  1
  • Dan Bailiff  · 技术社区  · 16 年前

    我正在尝试获取一个图像来编码为WordML文档。这个函数的原始版本使用了文件,但是我需要更改它,以获得使用ASPX页面动态创建的图像。我已经修改了代码以使用httpwebrequest而不是webclient。问题是,我认为页面请求没有得到解决,因此图像流无效,在调用image.fromstream时生成错误“参数无效”。

        public string RenderCitationTableImage(string citation_table_id)
    {
        string image_content = "";
        string _strBaseURL = String.Format("http://{0}",
            HttpContext.Current.Request.Url.GetComponents(UriComponents.HostAndPort, UriFormat.Unescaped));
        string _strPageURL = String.Format("{0}{1}", _strBaseURL,
            ResolveUrl("~/Publication/render_citation_chart.aspx"));
    
        string _staticURL = String.Format("{0}{1}", _strBaseURL,
            ResolveUrl("~/Images/table.gif"));
    
        string _fullURL = String.Format("{0}?publication_id={1}&citation_table_layout_id={2}",
                                            _strPageURL, publication_id, citation_table_id);
    
    
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_fullURL);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream image_stream = response.GetResponseStream();
            // Read the image data
            MemoryStream ms = new MemoryStream();
            int num_read;
            byte[] crlf = System.Text.Encoding.Default.GetBytes("\r\n");
            byte[] buffer = new byte[1024];
            for (num_read = image_stream.Read(buffer, 0, 1024); num_read > 0; num_read = image_stream.Read(buffer, 0, 1024))
            {
                ms.Write(buffer, 0, num_read);
            }
    
            // Base 64 Encode the image data
            byte[] image_bytes = ms.ToArray();
            string encodedImage = Convert.ToBase64String(image_bytes);
            ms.Position = 0;
            System.Drawing.Image image_original = System.Drawing.Image.FromStream(ms); // <---error here: parameter is not valid
            image_stream.Close();
    
            image_content = string.Format("<w:p>{4}<w:r><w:pict><w:binData w:name=\"wordml://{0}\">{1}</w:binData>" +
                "<v:shape style=\"width:{2}px;height:{3}px\">" +
                "<v:imagedata src=\"wordml://{0}\"/>" +
                "</v:shape>" +
                "</w:pict></w:r></w:p>", _word_image_id, encodedImage, 800, 400, alignment.center);
    
            image_content = "<w:br w:type=\"text-wrapping\"/>" + image_content + "<w:br w:type=\"text-wrapping\"/>";
        }
        catch (Exception ex) 
        {
            return ex.ToString();
        }
        return image_content;
    

    使用静态URI,它可以正常工作。如果我在webrequest.create方法中将“staticurl”替换为“fullurl”,我会得到错误。关于页面请求为什么不能完全解决有什么想法吗?

    是的,完整的URL可以很好地解析,如果我把它发布到地址栏中,就会显示一个图像。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Justin Grant    16 年前

    更新:

    只需阅读更新后的问题。由于您遇到登录问题,请在执行请求之前尝试执行此操作:

    request.Credentials = CredentialCache.DefaultCredentials
    

    如果这不起作用,那么问题可能在于,认证不是在静态文件上强制执行,而是在动态文件上强制执行。在这种情况下,您需要先登录(使用客户端代码)并保留登录cookie(使用 HttpWebRequest.CookieContainer 在登录请求和第二个请求上)或在您尝试访问的页面上关闭身份验证。

    原件:

    由于它与一个HTTP URL一起工作,而不与另一个HTTP URL一起工作,因此开始诊断这一问题的地方就是在HTTP级别上找出两个请求之间的不同之处,这就解释了代码中行为的差异。

    为了找出区别,我会用小提琴( http://fiddlertool.com )比较两个请求。比较HTTP头。它们是一样的吗?尤其是,它们是相同的HTTP内容类型吗?如果不是,那很可能是你问题的根源。

    如果标题相同,请确保静态和动态图像在服务器上的内容和文件类型完全相同。(例如,使用“文件…”“另存为”将浏览器中的图像保存到磁盘)。然后使用fiddler的十六进制视图比较图像内容。你能看到明显的区别吗?

    最后,我确定你已经检查过了,但是要确保: /Publication/render_citation_chart.aspx 指的是实际的图像文件,而不是围绕img元素的HTML包装,对吗?这将解释您所看到的行为,即浏览器可以正常呈现图像,但您的代码不能。

    推荐文章