如何将Webbrowser获取的验证码显示在图片框中

来源:互联网 发布:宾馆摄像头直播软件 编辑:程序博客网 时间:2024/05/16 05:51


  我们在验证码相关问题中,有许多需要解决的问题,就比如说目前我们需要面对的问题,就是如何在webbrowser中获得的验证码生产在图片框中,这对于我们一些程序工作者来说可能一时间不能够找到比较好的解决办法,那么今天知码网就为大家来介绍一段关于webbrowser获取验证码显示在图片中的方法。



  '* t=0 默认为 im传入的是图片 id属性

    '* t=1 Alt属性 t=2 Src 属性

    Private Function GetCode(ByVal wb AsWebBrowser, ByVal im As String, Optional ByVal t As Integer = 0) As Image

        Try

            Dim doc As HTMLDocument =DirectCast(wb.Document.DomDocument, HTMLDocument)

            Dim body As HTMLBody = DirectCast(doc.body,HTMLBody)

            Dim rang As IHTMLControlRange =DirectCast(body.createControlRange(), IHTMLControlRange)

            Dim img As IHTMLControlElement

            If t = 0 Then

                img =DirectCast(wb.Document.All(im).DomElement, IHTMLControlElement)

            Else

                img = GetPicElement(wb, im, t)

            End If

            If img Is Nothing Then

                Return Nothing

            End If

            rang.add(img)

            Clipboard.Clear()

            rang.execCommand("Copy", False,Nothing)

            doc = Nothing

            body = Nothing

            rang = Nothing

            img = Nothing

            If Clipboard.ContainsImage Then

                Return Clipboard.GetImage()

            Else

                Return Nothing

            End If

        Catch exp As Exception

            Return Nothing

        End Try

    End Function

    '* 获取网页中的某一个图片元素

    Private Function GetPicElement(ByVal wb AsWebBrowser, ByVal im As String, ByVal t As Integer) As IHTMLControlElement

        For Each image As HtmlElement Inwb.Document.Images

            Dim img As IHTMLImgElement =DirectCast(image.DomElement, IHTMLImgElement)

            If t = 1 Then

                If img.alt.Contains(im) Then

                    Return DirectCast(image.DomElement,IHTMLControlElement)

                End If

            Else

                If img.src.Contains(im) Then

                    ReturnDirectCast(image.DomElement, IHTMLControlElement)

                End If

            End If

        Next image

        Return Nothing '* 没有找到元素

    End Function

  应该说整个过程是太难,我们只需要将代码进行调用