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

本文介绍了一种通过WebBrowser控件获取网页中的验证码并将其显示为图片的方法。该方法利用了复制粘贴机制实现了验证码从网页到图片框的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


  我们在验证码相关问题中,有许多需要解决的问题,就比如说目前我们需要面对的问题,就是如何在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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值