0
votes

Is there any way to detect whether or not an image has not loaded/is broken in a webbrowser control? I am loading html from a file like so:

Here is some html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <META content="text/html; charset=unicode" http-equiv=Content-Type>
    <META name=GENERATOR content="MSHTML 11.00.10586.589">
  </HEAD>
  <BODY>
    <A href="https://web.archive.org/web/20120124023601/http://www.flatfeets.com/wp-content/uploads/2012/01/shoes-for-flat-feet.jpg">
      <IMG title="shoes for flat feet" class="alignleft size-medium wp-image-18" alt="" src="https://web.archive.org/web/20120124023601im_/http://www.flatfeets.com/wp-content/uploads/2012/01/shoes-for-flat-feet-300x238.jpg">
    </A>
  </BODY>
</HTML>

And simple load this into webbrowser

webbrowser1.DocumentText = thehtml

I would just like to be able to detect whether or not the image has loaded properly. This should work for all images on the page.

1

1 Answers

0
votes

You could create a separate WebClient request for each image in the html file and then see if any return a html response error code.

You would first have to parse the html and make a list of all the images urls. I would suggest using a package like HTML Agility Pack to easily parse out the image urls. Then you could use this code to identify any bad paths.

WebClient requester = new WebClient();
foreach (string url in urls)
{
    try
    {
         Byte[] imageBytes = requester.DownloadData(url);                    
    }
    catch(Exception ex)
    {
         //Do something here to indicate that the image file doesn't exist or couldn't be downloaded
    }
}

You can also convert the byte array to an Image and then make sure that it is RGB Encoded since that is the only encoding that can reliably be displayed in a web browser.