检测PictureBox中的错误图像何时使用

Dom*_*c K 6 c# image picturebox

我在谷歌上找到了这个,点击这里,有人问了一个类似的问题,收到一个回复​​,他们应该检查他们的文件是否存在.但是,我正在从Web链接加载图像,如果A)找不到图片或B)如果像Photobucket这样的图像托管服务显示"超出带宽"图像,则会显示错误图像.有没有办法检测是否显示错误图像或图像是否无效?

Han*_*ant 10

是的,LoadCompleted事件告诉您出了什么问题:

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

也可能存在图像加载正确完成但图像文件本身出现问题的情况:

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

此事件处理程序也捕获加载错误,因此可能是您要使用的错误.