zoukankan      html  css  js  c++  java
  • 没有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;
            }

  • 相关阅读:
    memcached stats 命令
    C# 压缩文件 ICSharpCode.SharpZipLib.dll
    复选框的全选与反选
    Internet Explorer 9 已安装在此系统上
    C# AES 加密与解密
    C# List<T> 泛型
    C# DataTbale详细操作
    C# 日志系统 log4net 配置及使用
    Web.config配置文件详解
    字符串比较
  • 原文地址:https://www.cnblogs.com/zzh1236/p/1578163.html
Copyright © 2011-2022 走看看