提取验证码到winform上webbroswer和axwebbroswer

在网上只有webbroswer的代码,所以自己又修改了修改改成axwebbroswer的

public static class yanZhengMaHelp
    {
        //webbrowser验证码
        public static Image GetRegCodePic(WebBrowser wbMail, string ImgName, string Src, string Alt)
        {
            HTMLDocument doc = (HTMLDocument)wbMail.Document.DomDocument;
            HTMLBody body = (HTMLBody)doc.body;
            IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
            IHTMLControlElement Img;
            if (ImgName == "") //如果没有图片的名字,通过Src或Alt中的关键字来取
            {
                int ImgNum = GetPicIndex(wbMail, Src, Alt);
                if (ImgNum == -1)
                {
                    return null;
                }
                Img = (IHTMLControlElement)wbMail.Document.Images[ImgNum].DomElement;
            }
            else
                Img = (IHTMLControlElement)wbMail.Document.All[ImgName].DomElement;

            rang.add(Img);
            rang.execCommand("Copy", false, null);
            Image RegImg = Clipboard.GetImage();
            Clipboard.Clear();
            return RegImg;
        }

        public static int GetPicIndex(WebBrowser wbMail, string Src, string Alt)
        {
            int imgnum = -1;
            for (int i = 0; i < wbMail.Document.Images.Count; i++) //获取所有的Image元素
            {
                IHTMLImgElement img = (IHTMLImgElement)wbMail.Document.Images[i].DomElement;
                if (Alt == "")
                {
                    if (img.src.Contains(Src)) return i;
                }
                else
                {
                    if (!string.IsNullOrEmpty(img.alt))
                    {
                        if (img.alt.Contains(Alt)) return i;
                    }
                }
            }
            return imgnum;
        }
        //axwebbroswer 验证码  提取
        public static Image GetRegCodePic(AxWebBrowser axwbMail, string ImgName, string Src, string Alt)
        {
           
            HTMLDocument doc = (HTMLDocument)axwbMail.Document;
            HTMLBody body = (HTMLBody)doc.body;
            IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
            IHTMLControlElement Img;
            if (ImgName == "") //如果没有图片的名字,通过Src或Alt中的关键字来取
            {
                Img = (IHTMLControlElement)GetPicIndex(axwbMail, Src, Alt);
                if (Img == null)
                {
                    return null;
                }


            }
            else
            {
                Img = (IHTMLControlElement)doc.getElementsByName(ImgName).item(null, 0);
            }

            rang.add(Img);
            rang.execCommand("Copy", false, null);
            Image RegImg = Clipboard.GetImage();
            Clipboard.Clear();
            return RegImg;
        }
        public static IHTMLImgElement GetPicIndex(AxWebBrowser axwbMail, string Src, string Alt)
        {

            HTMLDocument doc = (HTMLDocument)axwbMail.Document;

            foreach (IHTMLImgElement img in (IHTMLElementCollection)doc.getElementsByTagName("img")) //获取所有的Image元素
            {

                if (Alt == "")
                {
                    if (img.src.Contains(Src)) return img;
                }
                else
                {
                    if (!string.IsNullOrEmpty(img.alt))
                    {
                        if (img.alt.Contains(Alt)) return img;
                    }
                }
            }
            return null;

        }
原文地址:https://www.cnblogs.com/weiwin/p/3868576.html