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");
            }
        }

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;
    }
Comments
I LOVE YOU :)

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

This question has already been answered. Start a new discussion instead.