基本信息
源码名称:C# 读取 WebBrowser中的图片验证码到pictureBox 【已测试通过,附完整源码】
源码大小:0.05M
文件格式:.zip
开发语言:C#
更新时间:2013-03-08
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

用webbrowser打开网页后,读取网页中验证码图片至 picturebox控件, .net framework 2.0和.netframework 4.0 已测试通过

注意:

 

在COM 页上, 选择 Microsoft HTML Object Library ,添加mshtml 的引用。

 使用命名空间  using mshtml。



using mshtml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebBrowser_Image
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.txtUrl.Text = "http://www.21744.com/login.aspx";
            this.webBrowser1.Navigate(this.txtUrl.Text);
        }

        private void btnGetImage_Click(object sender, EventArgs e)
        {
            //取得验证码
            HtmlElement ImgeTag = webBrowser1.Document.All["V_Img"];

            Image numPic = GetWebImage(webBrowser1, ImgeTag); // 得到验证码图片
            pictureBox1.Image = numPic;
        }
        /// <summary>
        /// 返回指定WebBrowser中图片<IMG></IMG>中的图内容
        /// </summary>
        /// <param name="WebCtl">WebBrowser控件</param>
        /// <param name="ImgeTag">IMG元素</param>
        /// <returns>IMG对象</returns>
        private Image GetWebImage(WebBrowser WebCtl, HtmlElement ImgeTag)
        {
            HTMLDocument doc = (HTMLDocument)WebCtl.Document.DomDocument;
            HTMLBody body = (HTMLBody)doc.body;
            IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
            IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址

            Image oldImage = Clipboard.GetImage();
            rang.add(Img);
            rang.execCommand("Copy", false, null);  //拷贝到内存
            Image numImage = Clipboard.GetImage();
            try
            {
                Clipboard.SetImage(oldImage);
            }
            catch
            {
            }

            return numImage;
        }
    }
}