שתף באמצעות


Refresh or Redraw picturebox in VB.net

Question

Saturday, November 2, 2013 3:04 AM

I'm trying to view youtube captcha in my vb application but I don't know how to refresh/redraw the picture box. The youtube captcha is in http://www.youtube.com/cimg and the captcha is changing everytime I refresh the page. How can I do that using button or timer in vb.net to change the captcha. Here is the code I used to load the captcha to picturebox.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        captchaBox.ImageLocation = "http://www.youtube.com/cimg" 
    End Sub

i try to use captchaBox.Refresh() but it's not working. I hope someone can help me. thx

All replies (8)

Saturday, November 2, 2013 3:34 AM ✅Answered | 1 vote

I'm not sure why you'd want this, but if you want to replenish the image anew, first set it's image property to nothing, then reload the URL:

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load

        PictureBox1.ImageLocation = "http://www.youtube.com/cimg"

    End Sub

    Private Sub btn_Refresh_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btn_Refresh.Click

        With PictureBox1
            .Image = Nothing
            .ImageLocation = "http://www.youtube.com/cimg"
        End With

    End Sub

End Class

That will show a new image each time.

Please call me Frank :)


Saturday, November 2, 2013 11:29 AM ✅Answered

hi when i try to press the btn refresh the form 2-3 seconds and this is showing "A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll"

I don't know - I just tried it again and clicked the button probably twenty times or so without a problem.

Do be sure that you understand that I didn't mean to imply that you should leave any of those four exception catches as empty. I don't think it's related to this, but if it is then you're "swallowing the exception" and you'll never know.

You might want to do this:

In the "Catch" of each of the four, put the following:

Stop

If an exception is thrown during your testing, that should then halt the code so that you can examine "ex".

Please call me Frank :)


Saturday, November 2, 2013 3:52 AM

I'm not sure why you'd want this, but if you want to replenish the image anew, first set it's image property to nothing, then reload the URL:

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load

        PictureBox1.ImageLocation = "http://www.youtube.com/cimg"

    End Sub

    Private Sub btn_Refresh_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btn_Refresh.Click

        With PictureBox1
            .Image = Nothing
            .ImageLocation = "http://www.youtube.com/cimg"
        End With

    End Sub

End Class

That will show a new image each time.

Please call me Frank :)

thx a lot. is there a way to tell if picturebox is empty? 


Saturday, November 2, 2013 3:57 AM

thx a lot. is there a way to tell if picturebox is empty? 

I'm not sure what you mean.

If you literally mean if the PictureBox's .Image property is nothing, then yes:

If PictureBox1.Image Is Nothing Then
**    '**
End If

I suspect that's not what you mean though?

Please call me Frank :)


Saturday, November 2, 2013 7:30 AM

thx a lot. is there a way to tell if picturebox is empty? 

I'm not sure what you mean.

If you literally mean if the PictureBox's .Image property is nothing, then yes:

If PictureBox1.Image Is Nothing Then
**    '**
End If

I suspect that's not what you mean though?

Please call me Frank :)

what i mean is when the picturebox failed to load image(showing the x picture), it will load a text that will tell "refresh to get new image"


Saturday, November 2, 2013 10:03 AM

what i mean is when the picturebox failed to load image(showing the x picture), it will load a text that will tell "refresh to get new image"

I can't think of a reliable way to do that - just because it doesn't show up in the PictureBox doesn't mean that it's actually "Nothing", nor does it mean the image's URL is invalid.

I would think that most logically-minded people would be able to see very well that the image didn't load and would then click the button again. ;-)

Please call me Frank :)


Saturday, November 2, 2013 10:26 AM

Let me follow up with this. This will get you part of the way - if the URL really is invalid, this will catch it:

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load

        Try
            With PictureBox1
                .WaitOnLoad = True
                .SizeMode = PictureBoxSizeMode.CenterImage
                .LoadAsync("http://www.youtube.com/cimgxxx")
            End With

        Catch ex As Net.WebException
            ' The URL didn't return an image
            ' so take whatever action here.

        Catch ex As Exception
            ' Report a general exception here.
        End Try

    End Sub

    Private Sub btn_Refresh_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btn_Refresh.Click

        Try
            With PictureBox1
                .Image = Nothing
                .WaitOnLoad = True
                .SizeMode = PictureBoxSizeMode.CenterImage
                .LoadAsync("http://www.youtube.com/cimg")
            End With

        Catch ex As Net.WebException
            ' The URL didn't return an image
            ' so take whatever action here.

        Catch ex As Exception
            ' Report a general exception here.
        End Try

    End Sub

End Class

Try that as it is - when the form loads, nothing will be see in the PictureBox because it threw a System.Net.WebException (because the URL doesn't exist) and as you can see, I'm handling it with ... well, nothing! Not a suggestion, just an explanation. Use that to trigger showing your other image.

When you then click the button, it will show the image because that's a valid URL.

Please call me Frank :)


Saturday, November 2, 2013 11:21 AM

Let me follow up with this. This will get you part of the way - if the URL really is invalid, this will catch it:

Option Strict On
Option Explicit On

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load

        Try
            With PictureBox1
                .WaitOnLoad = True
                .SizeMode = PictureBoxSizeMode.CenterImage
                .LoadAsync("http://www.youtube.com/cimgxxx")
            End With

        Catch ex As Net.WebException
            ' The URL didn't return an image
            ' so take whatever action here.

        Catch ex As Exception
            ' Report a general exception here.
        End Try

    End Sub

    Private Sub btn_Refresh_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btn_Refresh.Click

        Try
            With PictureBox1
                .Image = Nothing
                .WaitOnLoad = True
                .SizeMode = PictureBoxSizeMode.CenterImage
                .LoadAsync("http://www.youtube.com/cimg")
            End With

        Catch ex As Net.WebException
            ' The URL didn't return an image
            ' so take whatever action here.

        Catch ex As Exception
            ' Report a general exception here.
        End Try

    End Sub

End Class

Try that as it is - when the form loads, nothing will be see in the PictureBox because it threw a System.Net.WebException (because the URL doesn't exist) and as you can see, I'm handling it with ... well, nothing! Not a suggestion, just an explanation. Use that to trigger showing your other image.

When you then click the button, it will show the image because that's a valid URL.

Please call me Frank :)

hi when i try to press the btn refresh the form 2-3 seconds and this is showing "A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll"