Detecting when error image in PictureBox is used

10,375

Solution 1

Yes, the LoadCompleted event tells you what went wrong:

private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) {
  if (e.Error != null) {
    // You got the Error image, e.Error tells you why
  }
}

There could also be a case where the image load completed properly but there was something wrong with the image file itself:

private void pictureBox1_Paint(object sender, PaintEventArgs e) {
  if (pictureBox1.Image == pictureBox1.ErrorImage) {
    // You got the Error image
  }
}

This event handler catches load errors too so might be the one you want to use.

Solution 2

There is no standard way of checking for valid pictures like you would wish to do. The 'Bandwidth exceeded' image is a perfectly valid picture in the eyes of the computer.

Nevertheless you could try some tricks for filtering out at least a few "wrong" images:

  • If you are loading the images, set up a web connection that does no automatic redirects. You could set up some kind of semantic that categorizes the image as "invalid" if you are redirected to some other place, where possibly the 'Bandwidth exceeded' image lies. Drawback of this method is of course that you are possibly filtering out images that lie behind a redirect and that are valid.
  • Simply check for the name of the picture delivered by the web server. If you connect to a adress like "http://test.tld/image.jpg" but retrieving a "bandwidth_exceeded.jpg" or something similar, the case should be clear. This method requires that you know how image hoster name their 'Bandwidth exceeded' or 'no longer available' images.
  • Some kind of image recognition checking against known 'bad' images. A rather sophisticated one.

You see, those semantic black lists are every thing else than perfect, maybe even worse filter out good images.

Share:
10,375
Dominic K
Author by

Dominic K

Updated on June 04, 2022

Comments

  • Dominic K
    Dominic K over 1 year

    I found this on Google, click here, which someone asked a similar question, receiving a response that they should check if their file exists. However, I'm loading images from web links, in which it displays an error image if A)The picture is not found or B)If, like in image hosting services like Photobucket, displays the 'Bandwidth exceeded' image. Is there a way to detect if either an error image is showing or if a image is invalid?