码迷,mamicode.com
首页 > Windows程序 > 详细

提取验证码到winform上webbroswer和axwebbroswer

时间:2014-07-26 00:51:06      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   color   os   io   for   re   

在网上只有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;

        }

提取验证码到winform上webbroswer和axwebbroswer,布布扣,bubuko.com

提取验证码到winform上webbroswer和axwebbroswer

标签:winform   style   blog   color   os   io   for   re   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!