您的位置:首页 > 编程语言 > C#

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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: