How can I detect when PictureBox is showing ErrorImage?

A simple problem but I can't see an obvious answer.

I want to detect when a user's image hasn't loaded for any reason. The obvious way seems to check if the image is ErrorImage but I can't work out how. Any assistance gratefully received.



Answer this question

How can I detect when PictureBox is showing ErrorImage?

  • Mark Flamer

    Just checking if e.Error is not null is enough, there is no need to check the message.
  • Drew Marsh

    You should get an error:

    The LoadCompleted occurs only when the image is loaded asynchronously using one of the LoadAsync methods and WaitOnLoad is false. If the image-load is cancelled by calling the CancelAsync method the Cancelled property of the AsyncCompletedEventArgs will be set to true. If an exception or error occurs during the load process, it will be caught and the Error property of the AsyncCompletedEventArgs will contain the exception information.


  • matt01

    Thank you Lucian,

    I have got there eventually. I find that the Cancelled property is an unreliable method of determining if the ErrorImage is displayed - it is set to False even if an illegal file type is loaded.

    The Error property seems a reliable indicator but being new to the .net game I find I can't test against it easily as the property is only valid if there is an error. So my final code looks bizarre but works.

    Private Sub PictureBox1_LoadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles PictureBox1.LoadCompleted

    Try

    If e.Error.Message <> "" Then PictureBox1.Visible = False 'Erroneous picture

    Catch ex As Exception

    PictureBox1.Visible = True 'Good picture

    End Try

    End Sub


  • davidmvp

    Thank you Lucian for your assistance - the right way to do it is:

    If e.Error Is Nothing Then

    PictureBox1.Visible = True 'Good picture

    Else

    PictureBox1.Visible = False 'Bad picture

    End If


  • Pedro Martins

    Have you tried listening to LoadCompleted event

    Andrej



  • hye_heena

    Unfortunately it appears that the LoadCompleted event is created whether the image is loaded successfully or not.

    Thank you for your suggestion though.


  • How can I detect when PictureBox is showing ErrorImage?