c# 获取 webbrowser的image数据流



常用于模拟登录 获取验证码,再整合打码平台。

/// <summary>
/// 返回指定WebBrowser中图片<IMG></IMG>中的图内容
/// </summary>
/// <param name=”WebCtl”>WebBrowser控件</param>
/// <param name=”ImgeTag”>IMG元素</param>
/// <returns>IMG对象</returns>
private Image GetWebImage(WebBrowser WebCtl, HtmlElement ImgeTag)
{
//numImage ; //从 Clipboard中取图

HTMLDocument doc = (HTMLDocument)WebCtl.Document.Window.Frames[0].Document.DomDocument;
HTMLBody body = (HTMLBody)doc.body;
IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址

Image oldImage = Clipboard.GetImage();
rang.add(Img);
rang.execCommand(“Copy”, false, null); //拷贝到内存
Image numImage = Clipboard.GetImage();
try
{
Clipboard.SetImage(oldImage);
}
catch
{
}
return numImage;//还原

}

 

实际调用:

HtmlElement hl = webBrowser1.Document.Window.Frames[0].Document.GetElementById(“ValidateImage”).Parent;
pictureBox1.Image = GetWebImage(webBrowser1, hl.FirstChild);