生活随笔
收集整理的這篇文章主要介紹了
通过webbrowser控件获取验证码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、首先介紹下基本控件(拖控件大家都會,我就不一一介紹了),看下圖;
2、添加MSHTML引用,步驟如下:
解決方案—右鍵“引用”—?添加引用—在.NET下找到Microsoft.mshtml組件—點擊確定;
3、編寫代碼,雙擊button1?按鈕添加事件;(注意在引用類中加上using mshtml;)
目標網站:http://admin.3cjz.cn/reg.php?username=ghdhdd
完整代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using mshtml; namespace GetCodeImg { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { webBrowser1.Navigate(textBox1.Text.Trim()); while (webBrowser1.ReadyState < WebBrowserReadyState.Complete) Application.DoEvents(); HtmlElement ImageCodeTag = webBrowser1.Document.GetElementById("vcodesrc"); Image FinalImage = GetCodeImage(webBrowser1, ImageCodeTag); pictureBox1.Image = FinalImage; } private Image GetCodeImage(WebBrowser WebControlName, HtmlElement ImgeTag) { HTMLDocument hdoc = (HTMLDocument)WebControlName.Document.DomDocument; HTMLBody hbody = (HTMLBody)hdoc.body; IHTMLControlRange hcr = (IHTMLControlRange)hbody.createControlRange(); IHTMLControlElement hImg = (IHTMLControlElement)ImgeTag.DomElement; hcr.add(hImg); hcr.execCommand("Copy", false, null); Image CodeImage = Clipboard.GetImage(); return CodeImage; } } } 4、驗證程序
PS:本人還是還是菜鳥級程序猿,如有編寫錯誤,歡迎指正。
總結
以上是生活随笔為你收集整理的通过webbrowser控件获取验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。