如何在pictureBox中显示验证码图像

时间:2014-02-28 18:45:56

标签: vb.net browser captcha picturebox

我有webbrowser控件来打开网站 我希望获得此网页上显示的验证码图像,以便在pictureBox中显示.....

<img src="/alpha/captcha.php?1393609547" width="150" height="25" title="Click for another image" alt="CAPTCHA" id="asc" />

这个例子

enter image description here

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

首先,我看到图片src网址是相对网址。这意味着您需要通过执行以下操作来使其成为绝对:

Dim absoluteURL As String = "domain of captcha" & captchaURL

然后,您需要通过执行以下操作将图像下载到字节中:

Dim PictureBytes As Byte()

' Convert String to a Uri
Dim address As New Uri(absoluteURL, UriKind.Absolute)

' Create the web request  
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(address), HttpWebRequest)

' Set type to GET  
request.Method = "GET"

Try
    ' Get response  
    Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

    ' Get the response stream into a reader  
    Dim stream As Stream = response.GetResponseStream
    Dim reader New BinaryReader(stream)

    PictureBytes = reader.ReadBytes(stream.Length)
Finally
    If Not response Is Nothing Then response.Close()
End Try

最后,您需要将字节转换为位图并将该位图放在图片框中

Dim Captcha As New Bitmap(new MemoryStream(PictureBytes));
pictureBox.Image = Captcha

如果您遇到任何问题,请发表评论。如果此代码有效,请考虑将此标记正确并进行投票。请注意,投票或标记答案是正确的,不能减去声誉。