Hello,

I have the code in the snippet that takes a long time to display and image. Whats the best way, I can optimize the response speed?

private void _GetImage(string text)
        {
            try
            {
                string html;
                using (WebClient wc = new WebClient())
                {
                    //Set the game's title ID page as the page's source that we're looking at
                    html = wc.DownloadString("http://marketplace.xbox.com/en-US/games/media/66acd000-77fe-1000-9115-d802" + text);
                }
                object[] oPageText = { html };
                HTMLDocument doc = new HTMLDocumentClass();
                IHTMLDocument2 doc2 = (IHTMLDocument2)doc;
                doc2.write(oPageText);

                foreach (IHTMLElement div in doc.getElementsByTagName("div"))
                {
                    if (div.className == "ImageArea")
                    {
                        string imagePath = div.innerHTML.Remove(0, 27);
                        pictureBox1.ImageLocation = imagePath.Remove(imagePath.Length - 2, 2);
                        break;
                    }
                }
            }
            catch
            {
                MessageBox.Show("An error has occured","Error");
            }
        }

Dani AI

Generated

Good thread. Main slow points are network latency and heavy DOM work: ’s use of the COM mshtml DOM is relatively expensive, and was right to suggest avoiding full COM parsing. Best overall gains come from three things: async I/O to avoid blocking, reusing HTTP connections, and using a lightweight HTML parser (not fragile regex or repeated COM writes).

Practical recipe:

  • Reuse a single HttpClient instance to avoid repeated TCP/TLS setup and socket exhaustion.
  • Run the fetch/parse on a background task (async/await) so the UI stays responsive.
  • Parse the returned HTML with a resilient parser (for example HtmlAgilityPack) to extract the image src, then load the image asynchronously into the PictureBox (LoadAsync or setting ImageLocation on the UI thread).
  • Cache resolved image URLs or downloaded bitmaps (MemoryCache or simple dictionary) so repeated requests are instant.

Example (new approach, not repeating earlier code):

private static readonly HttpClient _http = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };

private async Task LoadPosterAsync(string pageUrl)
{
    string html = await _http.GetStringAsync(pageUrl).ConfigureAwait(false);

    var doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(html);
    var img = doc.DocumentNode.SelectSingleNode("//div[contains(@class,'ImageArea')]//img");
    var src = img?.GetAttributeValue("src", null);
    if (string.IsNullOrEmpty(src)) return;

    pictureBox1.Invoke((Action)(() => pictureBox1.LoadAsync(src)));
}

Notes and caveats: regex is brittle for real HTML; some sites build the image client-side with JavaScript (in which case server HTML won't contain the img and a site API or headless renderer is needed); always set sensible timeouts and a User-Agent header if the site blocks default clients; handle relative URLs and exceptions; cache aggressively for repeated views. Combining async HTTP + parser + caching typically yields the biggest practical speedup.

Recommended Answers

All 2 Replies

You will struggle to make it an instant process; you will always have a delay based on your internet speed as the program needs to connect to the webpage and download the html. I did a couple of tests (far from exhasutive) and found that using a HttpWebRequet and HttpWebResponse to get the HTML source of the apge was consistently a second or two faster than using a WebClient:

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    Stream stream = httpWebResponse.GetResponseStream();
    StreamReader streamReader = new StreamReader(stream, Encoding.ASCII);
         
    string page = streamReader.ReadToEnd();

Also, once you have loaded the HTML into a string, rather than converting it to an HTML document, finding all the div tags and iterating through them, you could use a regular expression to search for the tag you need:

Regex imgTag = new Regex(@"img class=""GamePoster"" src=""(?<url>.*?)""");
    Match m = imgTag.Match(page);
    if (m.Success)
    {
       pictureBox1.ImageLocation = m.Groups["url"].Value;
    }
commented: I LOVE YOU :) +0

Thank You! Another paid forum was clueless. I am glade you knew what I was talking about.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.