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?
Detecting when error image in PictureBox is used
Asked Answered
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.
Of course this one would tell you that a image could not be loaded (404 from the sever for example), but it won't filter out any images, that are sent by the hoster ('Bandwidth exceeded' image for example, or an image that the server sends if the image was deleted on the server). –
Nevarez
+1 and accepted as answer, your second block of code comparing the image and the errorimage works perfectly. –
Corneliacornelian
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.
Well, I don't think I need to go that far, as the Bandwidth exceeded image appeared as an X, possibly because it was an invalid format? –
Corneliacornelian
© 2022 - 2024 — McMap. All rights reserved.