Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp C# Generate Website Screenshot And Save Thumbnail

C# Generate Website Screenshot And Save Thumbnail

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()
        {
            InitializeComponent();
        }
 
        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;
            }
            else
            {
                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);
 
                bmp.Save(thumb_file);
 
                bmp.Dispose();
            }
            else //Just make a copy
            {
                System.IO.File.Copy(source_file, thumb_file);
            }
 
            pic_tmp.Dispose();
        }
 
        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) { 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));
            wb.Dispose();
 
            return bitmap;
        }
 
    } 
}

using System; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } 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; } else { 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); bmp.Save(thumb_file); bmp.Dispose(); } else //Just make a copy { System.IO.File.Copy(source_file, thumb_file); } pic_tmp.Dispose(); } 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) { 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)); wb.Dispose(); 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.

Dec 8, 2011Hoan Huynh
iPhone 4s Capture ScreenTop iPhone/ iPad apps For IT Administrators Or IT Support
You Might Also Like:
  • C# Save Web Page URL To Image
  • ASP.NET Resize Image High Quality
  • YouTube Thumbnail URLs And Examples
  • ASP.Net C# Download Or Save Image File From URL
  • C# Generate Random Number Function
  • Visual Studio Auto generate comments using GhostDoc plugin
  • C# ASP.Net Validate Date
  • Share Page On Facebook With Thumbnail Featured Image
  • Asp.net C# Create MD5 Hash
  • C# Read File Content
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago CSharpBitmap, Screenshot, Thumbnail, WebBrowser, WebBrowserReadyState465
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
21,932 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
19,767 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,600 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,117 views
C# Read Json From URL And Parse/Deserialize Json
9,570 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Essay Writing Service Tips For The Online Essay
  • Research Paper Writing Service
  • Annotated Bibliography Example – How it Can Help You
  • Essay Writing Online Tips – How to Write Essays Online With Essay Proof Editing
  • Get Research Paper Assistance From Professional Help
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (64)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development