C# Generate Website Screenshot And Save Thumbnail
Posted by in CSharp December 8, 2011 2 Comments

There are some online services (websnapr.com, thumbalizr.com, thumboo.com, etc) that generate screen shot of a particular webpage via its URL. Or technorati.com, buysellads.com and social bookmarking websites display website thumbnail along with its information.

I’m not sure which program/technic they’re using to capture screenshot of websites but I have another solution to do that.

C# source code below allows you to generate screen shot of a provided URL then resize and save its thumbnail to local disk.

1. C# Generate Screenshot And Save Thumbnail

using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
    public partial class Form1 : Form
        public Form1()
        private void Form1_Load(object sender, EventArgs e)
        private void btnGenerate_Click(object sender, EventArgs e)
            int thumb_width = int.Parse(txtWidth.Text);
            int thumb_height = int.Parse(txtHeight.Text);
            int screen_width = int.Parse(txtScreenWidth.Text);
            int screen_height = int.Parse(txtScreenHeight.Text);
            string url = txtUrl.Text;
            Bitmap thumbnail = GenerateScreenshot(url, screen_width, screen_height);
            string file_name = new_guid();
            string screenshot_file = "D:/dev/WindowsFormsApplication1/WindowsFormsApplication1/" + file_name + ".png";
            string thumb_file = "D:/dev/WindowsFormsApplication1/WindowsFormsApplication1/" + file_name + "_thumb" + ".png";
            // Save Thumbnail to a File
            thumbnail.Save(screenshot_file, System.Drawing.Imaging.ImageFormat.Png);
            // Resize the thumbnail to a smaller size
            save_thumbnail(screenshot_file, thumb_file, thumb_width, thumb_height);
            MessageBox.Show("A website thumbnail has been generated sucessfully!\r\nIt is located at: " + thumb_file);
        public String new_guid()
            return System.Guid.NewGuid().ToString().Replace("-", "");
        public void save_thumbnail(string source_file, string thumb_file, int width, int height)
            System.Drawing.Image pic_tmp = System.Drawing.Image.FromFile(source_file);
            int src_width = pic_tmp.Width;
            int src_height = pic_tmp.Height;
            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
            nPercentW = ((float)width / (float)src_width);
            nPercentH = ((float)height / (float)src_height);
            if (width == 0)//Auto width
                nPercent = nPercentH;
            else if (height == 0)//auto height
                nPercent = nPercentW;
            else if (nPercentW > nPercentH)
                nPercent = nPercentH;
                nPercent = nPercentW;
            int thumb_width = 0;
            int thumb_height = 0;
            if (nPercent < 1)
                thumb_width = (int)(src_width * nPercent);
                thumb_height = (int)(src_height * nPercent);
                Bitmap bmp = new Bitmap(thumb_width, thumb_height);
                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumb_width, thumb_height);
                gr.DrawImage(pic_tmp, rectDestination, 0, 0, src_width, src_height, GraphicsUnit.Pixel);
            else //Just make a copy
                System.IO.File.Copy(source_file, thumb_file);
        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;
            while (wb.ReadyState != WebBrowserReadyState.Complete) { System.Windows.Forms.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));
            return bitmap;

2. Form User Interface

Form Generate Website ScreenShot Internface

Form Generate Website ScreenShot Internface

As you can see from the C# source code & form interface above, there are few things you need to notice:

  • 1. Screen Width & Screen Height: the Screen Resolution that loads the web page. We have some popular screen resolution: 1600×900, 1280×720, 1280×1024, 1280×800, 1280×768, 1024×768, etc. Let try some to choose a best suit to you.
  • 2. Thumbnail Width & Thumbnail Height: this is size of the thumbnail which is resized from the site screenshot. Also, you can change that values and generate to several thumbnail size such as: Small (150×100), Medium (200×150) or Large (300×250)
  • 3. You need to change location of original screen shot & thumb file as it’s hard code from my environment.

+ Download the C# Windows Form Source Code above.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.move-my-site.com/migration-services/server-transfer/ Server Transfer

    I have seen a very informative blog. Really I like this blog. This blog gives us very good knowledge about web development.

  • http://www.book-marky.de favoriten verwalten

    Hello my family member! I wish to say that this article is awesome, great written and include almost all important infos. I would like to look more posts like this .