Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 463951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:13:35+00:00 2026-05-12T23:13:35+00:00

I’m trying to obtain an image to encode to a WordML document. The original

  • 0

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.

    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;

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?

And yes, the full URL resolves fine and shows an image if I post it in the address bar.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:13:35+00:00Added an answer on May 12, 2026 at 11:13 pm

    UPDATE:

    Just read your updated question. Since you’re running into login issues, try doing this before you execute the request:

    request.Credentials = CredentialCache.DefaultCredentials
    

    If this doesn’t work, then perhaps the problem is that authentication is not being enforced on static files, but is being enforced on dynamic files. In this case, you’ll need to log in first (using your client code) and retain the login cookie (using HttpWebRequest.CookieContainer on the login request as well as on the second request) or turn off authentication on the page you’re trying to access.

    ORIGINAL:

    Since it works with one HTTP URL and doesn’t work with another, the place to start diagnosing this is figuring out what’s different between the two requests, at the HTTP level, which accounts for the difference in behavior in your code.

    To figure out the difference, I’d use Fiddler (http://fiddlertool.com) to compare the two requests. Compare the HTTP headers. Are they the same? In particular, are they the same HTTP content type? If not, that’s likely the source of your problem.

    If headers are the same, make sure both the static and dynamic image are exactly the same content and file type on the server. (e.g. use File…Save As to save the image in a browser to your disk). Then use Fiddler’s Hex View to compare the image content. Can you see any obvious differences?

    Finally, I’m sure you’ve already checked this, but just making sure: /Publication/render_citation_chart.aspx refers to an actual image file, not an HTML wrapper around an IMG element, right? This would account for the behavior you’re seeing, where a browser renders the image OK but your code doesn’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.