zoukankan      html  css  js  c++  java
  • C#: Generate WebPage Thumbmail Screenshot Image

    There are a few services out there that serve up screenshots of any webpage for you to display on your website. One popular one is Kwiboo; this is the one that DotNetKicks uses. For some time now I've wondered what the easiest way to do this in .NET was, and today I stumbled upon the undocumented WebBrowser.DrawToBitmap method that makes this extremely easy to do.

    By the way, I stumbled upon the WebBrowser.DrawToBitmap while taking a look at the source code for the WebPreview tool over at SmallSharpTools.com.

    Here's a sample method that returns a Bitmap representation of a webpage:

    public Bitmap GenerateScreenshot(string url)
    {
        // This method gets a screenshot of the webpage
        // rendered at its full size (height and width)
        return GenerateScreenshot(url, -1, -1);
    }
    
    public Bitmap GenerateScreenshot(string url, int width, int height)
    {
        // Load the webpage into a WebBrowser control
        WebBrowser wb = new WebBrowser();
        wb.ScrollBarsEnabled = false;
        wb.ScriptErrorsSuppressed = true;
        wb.Navigate(url);
        while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
    
    
        // Set the size of the WebBrowser control
        wb.Width = width;
        wb.Height = height;
    
        if (width == -1)
        {
            // Take Screenshot of the web pages full width
            wb.Width = wb.Document.Body.ScrollRectangle.Width;
        }
    
        if (height == -1)
        {
            // Take Screenshot of the web pages full height
            wb.Height = wb.Document.Body.ScrollRectangle.Height;
        }
    
        // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
        Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
        wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
        wb.Dispose();
    
        return bitmap;
    }

    Here are some example usages of the above method:

    // Generate thumbnail of a webpage at 1024x768 resolution
    Bitmap thumbnail = GenerateScreenshot("http://pietschsoft.com", 1024, 768);
    
    // Generate thumbnail of a webpage at the webpage's full size (height and width)
    thumbnail = GenerateScreenshot("http://pietschsoft.com");
    
    // Display Thumbnail in PictureBox control
    pictureBox1.Image = thumbnail;
    
    /*
    // Save Thumbnail to a File
    thumbnail.Save("thumbnail.png", System.Drawing.Imaging.ImageFormat.Png);
    */
  • 相关阅读:
    Codeforces Round #619 (Div. 2) ABC 题解
    Codeforces Round #669 ABC 题解
    中大ACM个人赛 ABC题题解
    Codeforces Round #601 (Div. 2) ABC 题解
    SCAU 2019年校赛 部分题解
    SCAU 2018年新生赛 初出茅庐 全题解
    Educational Codeforces Round 74 (Rated for Div. 2) ABC 题解
    Codeforces Round #603 (Div. 2) ABC 题解
    【题解】 CF767E Change-free 带悔贪心
    【题解】 NOIp2013 华容道 最短路+状态压缩 Luogu1979
  • 原文地址:https://www.cnblogs.com/happy-Chen/p/3680540.html
Copyright © 2011-2022 走看看