Webbrowser 获取验证码显示在图片框中

  '* t=0 默认为 im 传入的是图片 id 属性
    '* t=1 为 Alt 属性 t=2 为 Src 属性
    Private Function GetCode(ByVal wb As WebBrowser, 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 As WebBrowser, ByVal im As String, ByVal t As Integer) As IHTMLControlElement
        For Each image As HtmlElement In wb.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
                    Return DirectCast(image.DomElement, IHTMLControlElement)
                End If
            End If
        Next image
        Return Nothing '* 没有找到元素
    End Function

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值