Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing HttpWebRequest with dynamic URI causes "parameter is not valid" in Image.FromStream
    primarykey
    data
    text
    <p>I'm trying to obtain an image to encode to a WordML document. The original version of this function used files, but I needed to change it to get images created on the fly with an aspx page. I've adapted the code to use HttpWebRequest instead of a WebClient. The problem is that I don't think the page request is getting resolved and so the image stream is invalid, generating the error "parameter is not valid" when I invoke Image.FromStream.</p> <pre><code> 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}&amp;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 &gt; 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); // &lt;---error here: parameter is not valid image_stream.Close(); image_content = string.Format("&lt;w:p&gt;{4}&lt;w:r&gt;&lt;w:pict&gt;&lt;w:binData w:name=\"wordml://{0}\"&gt;{1}&lt;/w:binData&gt;" + "&lt;v:shape style=\"width:{2}px;height:{3}px\"&gt;" + "&lt;v:imagedata src=\"wordml://{0}\"/&gt;" + "&lt;/v:shape&gt;" + "&lt;/w:pict&gt;&lt;/w:r&gt;&lt;/w:p&gt;", _word_image_id, encodedImage, 800, 400, alignment.center); image_content = "&lt;w:br w:type=\"text-wrapping\"/&gt;" + image_content + "&lt;w:br w:type=\"text-wrapping\"/&gt;"; } catch (Exception ex) { return ex.ToString(); } return image_content; </code></pre> <p>Using a static URI it works fine. If I replace "staticURL" with "fullURL" in the WebRequest.Create method I get the error. Any ideas as to why the page request doesn't fully resolve?</p> <p>And yes, the full URL resolves fine and shows an image if I post it in the address bar. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload