通过WebBrowser获取网页验证码
/// <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) { HTMLDocument doc = (HTMLDocument)WebCtl.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 ImgeTag = webBrowser2.Document.All["id"];//根据验证码id获取元素,根据实际情况更改 Image numPic = GetWebImage(webBrowser2, ImgeTag); // 得到验证码图片 piccode.Image = numPic;