提取验证码到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;

        }
复制代码
posted @   树形图  阅读(477)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示