C#利用webBrowser显示验证码并可刷新显示
2012-12-20 11:13
531 查看
今天在做邮箱批量注册功能,想将验证码显示在控件pictureBox上,在网上找了好多方法,有的是利用webBrowser 自带的DrawToBitmap方法将图片赋给pictureBox控件,但有的验证码获取下来显示是空白,所以抛弃了这个方法,然后利用自带的Clipboard复制功能进行图片的复制然后将其显示,这个方法可以保证显示下来的和页面显示的图片是保持一致的,然后就是点击按钮实现验证码的刷新功能了,刷新功能我也是模拟点击看不清字样然后再将刷新后的图片显示在pictureBox控件上,但显示下来要有一个过程,我在这里用个一个等待2秒钟的时间处理,结果都保持了一致,现在在想要是是flash的验证码要怎么弄了,这个还在思索中,希望有实现的朋友也能够分享出来~~我先将核心代码贴出来,有不明白的欢迎留言~~~
这是点击按钮第一次显示
下面的是点击看不清刷新显示
try { HTMLDocument html = (HTMLDocument)this.webBrowser1.Document.DomDocument; foreach (HtmlElement f in webBrowser1.Document.Images) { if (f.GetAttribute("src").ToLower().IndexOf("/cgi/pin.php") > -1) { //将元素绝对定位到页面左上角 //f.Style = "position: absolute; z-index: 9999; top: 0px; left: 0px"; //抓图 IHTMLControlElement img = (IHTMLControlElement)f.DomElement; IHTMLControlRange range = (IHTMLControlRange)((HTMLBody)html.body).createControlRange(); range.add(img); range.execCommand("Copy", false, null); img = null; range = null; html = null; if (Clipboard.ContainsImage()) { this.pictureBox1.Image = null; this.pictureBox1.Image = Clipboard.GetImage(); } Clipboard.Clear(); //pictureBox1.ImageLocation = f.GetAttribute("src"); break; } } } catch (Exception msg) { throw (new Exception(msg.Message + msg.StackTrace + msg.Source)); return; }
这是点击按钮第一次显示
下面的是点击看不清刷新显示
private void button2_Click(object sender, EventArgs e) { foreach (HtmlElement f in webBrowser1.Document.Links) { if (f.GetAttribute("innerHTML").ToLower().IndexOf("看不清") > -1) { f.InvokeMember("click"); wybsemtool.Delay(2000); } } try { HTMLDocument html = (HTMLDocument)this.webBrowser1.Document.DomDocument; foreach (HtmlElement f in webBrowser1.Document.Images) { if (f.GetAttribute("src").ToLower().IndexOf("/cgi/pin.php") > -1) { //将元素绝对定位到页面左上角 //f.Style = "position: absolute; z-index: 9999; top: 0px; left: 0px"; //抓图 IHTMLControlElement img = (IHTMLControlElement)f.DomElement; IHTMLControlRange range = (IHTMLControlRange)((HTMLBody)html.body).createControlRange(); range.add(img); range.execCommand("Copy", false, null); img = null; range = null; html = null; if (Clipboard.ContainsImage()) { this.pictureBox1.Image = null; this.pictureBox1.Image = Clipboard.GetImage(); } Clipboard.Clear(); //pictureBox1.ImageLocation = f.GetAttribute("src"); break; } } } catch (Exception msg) { throw (new Exception(msg.Message + msg.StackTrace + msg.Source)); return; } }
相关文章推荐
- C#利用mshtml的COM接口,将WebBrowser中的验证码图片读入内存
- C#利用mshtml的COM接口,将WebBrowser中的验证码图片读入内存
- C#利用mshtml的COM接口,将WebBrowser中的验证码图片读入内存
- C#利用mshtml的COM接口,将WebBrowser中的验证码图片读入内存
- C#利用WebBrowser截取验证码图片 识别率一半
- C#利用webBrowser怎么显示网页中只想显示的部分
- C#利用WebBrowser截取验证码图片
- 如何利用 WebBrowser 控件,显示 .GIF 动画?
- C# mvc js cookie 接收手机验证码并防止页面刷新后重复获取验证码
- C# 利用正则表达式进行忽略大小写的字符串替换的应用--自定义高亮显示
- ThinkPHP3.2.3验证码显示、刷新、校验
- 利用Ajax实现在页面显示局部刷新
- C#下利用WebBrowser完整获取COOKIE
- C# 利用TCP/IP协议发送图片文件,并显示
- 利用JS实现局部刷新验证码(非Ajax)
- WINCE5.0利用C#语言编写的串口采集、显示和存储
- ThinkPHP3.2.3验证码显示、刷新、校验
- C# WebBrowser控件读html流并显示
- C# 定时器定时刷新数据库并显示
- C#利用WebBrowser获取完整COOKIE介绍 (转帖)