复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private WebBrowser _webBrowser;
        public Form1()
        {
            InitializeComponent();
        }
        public void GetThumbNail(string url)
        {
            _webBrowser = new WebBrowser();
            _webBrowser.ScrollBarsEnabled = false; //不显示滚动条
            _webBrowser.Navigate(url);
            _webBrowser.DocumentCompleted = new WebBrowserDocumentCompletedEventHandler(Completed);
            while (_webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                System.Windows.Forms.Application.DoEvents(); //避免假死,若去掉则可能无法触发 DocumentCompleted 事件。
            }
        }
        public void Completed(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //设置浏览器宽度、高度为文档宽度、高度,以便截取整个网页。
            _webBrowser.Width = _webBrowser.Document.Body.ScrollRectangle.Width;
            _webBrowser.Height = _webBrowser.Document.Body.ScrollRectangle.Height;
            using (Bitmap bmp = new Bitmap(_webBrowser.Width, _webBrowser.Height))
            {
                _webBrowser.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
                bmp.Save("Capture.png", System.Drawing.Imaging.ImageFormat.Png);
                pictureBox1.ImageLocation = "Capture.png";
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            GetThumbNail(textBox1.Text);
        }
    }
}
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com