Asp. Net gets the example code for the website screenshot

  • 2020-06-15 08:05:43
  • OfStack


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; // Scroll bars are not displayed 
            _webBrowser.Navigate(url);
            _webBrowser.DocumentCompleted = new WebBrowserDocumentCompletedEventHandler(Completed);
            while (_webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                System.Windows.Forms.Application.DoEvents(); // Avoid feign death. It may not trigger if removed  DocumentCompleted  Events. 
            }
        }
        public void Completed(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Set the browser width and height to document width and height to capture the entire page. 
            _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);
        }
    }
} 

Related articles: