• 没有ID的验证码图片,调用方法将图片保存到本地


    首先须要添加mshtml的引用,之后using mshtml;

    pictureBox1.Image = GetRegCodePic(webBrowser1, "", "code.en.img.php", "");  //第2-4个参数自己填
      //第2个参数为网页中图片的name(可能或ID也好用).
      //第3个参数为网页中图片的Src.
      //第2个参数为网页中图片的Alt.
      ////例<IMG height=80 alt="Registration Verification Code" src="......" width=290 border=0>
      pictureBox1.Image.Save("L:\\test\\111.bmp", pictureBox1.Image.RawFormat); //保存图片到本地

     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;
            }

  • 相关阅读:
    X-Plosives (并查集)
    HDU1272小希的迷宫 (并查集)
    React 初学
    js 插件 issue
    js常用方法
    常用网址
    js 零零散散的总结。
    git 常用命令
    es6 babel编译
    屏幕适配
  • 原文地址:https://www.cnblogs.com/zzh1236/p/1578163.html
Copyright © 2020-2023  润新知