Click here to Skip to main content
Click here to Skip to main content

How To Get A Website Thumbnail in a C# Application Without Creating A Form (console)

, 5 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
The article describes how to get a thumbnail of a Website in .NET Framework 2.0+ without launching a fully interactive WinForms application.


I've updated the code and the binary with the great improvements that Piers Lawson suggested in the comments. The app should no longer have problems taking snapshots of some images with JavaScript or just plain random problems. It is also slightly optimized with suggestions from Frank Herget. It looks like he's based a very nice service around it on his site - check it out!

Thanks again for your great support!


The article describes a console-like application that loads a Web page, makes a screenshot of it and saves it as a JPG file.

Our beloved sys admin - (we all bow to him and worship his skills) has recently asked if it's possible to write a .NET application to make a thumbnail of a Website. The task is pretty trivial with Windows Forms actually. But with him being the Linux guy and all... I decided to pick up the more challenging part of it being the console app. An interesting use case anyway.

In WinForms, all you really need to do is drop a WebBrowser from your Toolbox on your form and once it's loaded the page call:

Bitmap bitmap = new Bitmap(width, height);
    new Rectangle(webBrowser1.Location.X, webBrowser1.Location.Y, 
        webBrowser1.Width, webBrowser1.Height));

Obvious enough. When it gets tricky is when you want to do it in a console application in a way that can take a shot of multitude of Websites provided in a batch file. There is a dirty way of instantiating a whole form, making it show (or not), doing the work and then exiting the WinForms app. This might probably be enough for a quick solution, but I wanted a clean piece of code, so I would actually NOT take pride in something in that tone.

How is it done then...

So we instantiate the Web control in our class constructor...

public WebPageBitmap(string url, int width, int height, bool scrollBarsEnabled)
    this.url = url;
    this.width = width;
    this.height = height;
    webBrowser = new WebBrowser();
    webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(documentCompletedEventHandler);
    webBrowser.Size = new Size(width, height);
    webBrowser.ScrollBarsEnabled = scrollBarsEnabled;

Easy so far and pretty similar to what the regular app would do anyway. The documentCompletedEventHandler is a delegate to tell that it has loaded. (I initially wanted to use that for drawing the bitmap but deferred that to the point where the bitmap is actually fetched after I added the resizing part.) Now comes the interesting case.

The Neat Part

Since the call is asynchronous, a simple webBrowser.Navigate(URL); just won't cut it. We are in a single thread and the browser does not create a separate thread for that. This makes sense by the canonical windows rule: Only the thread that creates a control, accesses the control. We need to somehow allow the control to take the flow of the thread and do its work. Navigate only tells it that it should perform the action and immediately exits. The developer's responsibility then is to know when the control is ready for consumption. Which is the case when the webBrowser.ReadyState progresses to (or returns to) the state of WebBrowserReadyState.Complete.

The Solution

To pass the flow to the app controls, you need to perform Application.DoEvents(); which was a bit of a wild guess when I used it. Surprise, surprise, it works just like it did in other Windows frameworks that I used before.

public void Fetch()
        while (webBrowser.ReadyState != WebBrowserReadyState.Complete)

The effect is a tiny and neat (I hope) app that pulls a Web page from the net and makes a screenshot off of it (with possible rescaling).

You can get the source code or get the app directly. App usage:

GetSiteThumbnail.exe thumbnail.jpg 
  [browser_width(defaults to 800) browser_height (defaults to 600) ] 
  [thumbnail_width thumbnail_height]

GetSiteThumbnail.exe cognifide.jpg 1280 1024 640 480


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Poland Poland
Adam Najmanowicz is a Senior Developer at Cognifide Poland.

His previous experience includes Delphi, .Net as well as Java Development.

Currently working on ASP.NET + MSSql Server(Oracle) solutions for enterprises.

Also developing desktop applications for Stardock Systems.

Comments and Discussions

QuestionSpurious behaviour Pinmembermillerbill2-Oct-12 2:35 
GeneralThank you Pinmemberquannt9124-Jul-12 19:22 
GeneralAgain ... NICE JOB! PinmemberBHUSCH16-Jul-09 10:10 
GeneralNice work PinmemberOwen Gunter5-Jul-09 12:42 
Generalnice app! PinmemberMember 223292820-Aug-08 11:40 
GeneralRe: nice app! PinmemberAdamNajmanowicz24-Aug-08 5:04 
GeneralRe: nice app! PinmemberMember 223292825-Aug-08 5:04 
Generalthankx PinmemberHari Om Prakash Sharma7-Jul-08 0:21 
GeneralA Better Way to capture website thumbnails PinmemberMember 456781829-May-08 15:58 
After spending a lot of time tiring to set up a reliable way to capture webpage snapshots I gave up! Here are some issues I ran into along the way:

1. Capturing snapshots is a resource hog.
2. Security was a big issue; websites containing harmful script cannot be captured because they have to be loaded on your server making it vulnerable
3. Huge bandwidth consumption
4. There are so many different websites and issues you will run into such as pop up messages, flash, and JavaScript to name a few. It is difficult to capture a wide range of websites correctly.

My final solution was to abandon my attempt and use a free website thumbnail generator. Snapcasa servers about 16000+ website snapshots daily on three of my domains for free and without a watermark. There are many snapshot providers online but I found to be the best. Save yourself the frustration I went through and let one of the online snapshot providers worry about it.
GeneralRe: A Better Way to capture website thumbnails PinmemberOwen Gunter5-Jul-09 12:46 
QuestionSystem.UnauthorizedAccessException [modified] Pinmemberapokalypse_14-Sep-07 13:08 
GeneralDoesn't work for me Pinmemberras7820-Jul-07 0:32 
GeneralWindows namespace or type not found Pinmemberpinkarton7-May-07 16:07 
QuestionCan Java do it? Pinmemberpinkarton23-Apr-07 16:18 
AnswerRe: Can Java do it? PinmemberAdamNajmanowicz24-Apr-07 8:37 
GeneralThumbnail width and height parameters Pinmembertbenami28-Mar-07 21:25 
GeneralRe: Thumbnail width and height parameters Pinmembergafrancisco20-Jul-10 14:38 
QuestionBlank Image on Websites with Java Applets PinmemberJon Ebersole9-Feb-07 13:29 
Questionwhy I just get blank image? Pinmemberkai.ma24-Dec-06 22:15 
AnswerRe: why I just get blank image? Pinmembermethai189-Apr-07 14:31 
GeneralRe: why I just get blank image? PinmemberZhongjie Li9-May-07 10:09 
GeneralRe: why I just get blank image? PinmemberZhongjie Li10-May-07 4:47 
GeneralRe: why I just get blank image? PinmemberPiers Lawson25-May-07 3:37 
GeneralRe: why I just get blank image? Pinmembertreeleung22-Apr-08 13:27 
GeneralRe: why I just get blank image? PinmemberilKaleez15-May-08 4:12 
GeneralRe: why I just get blank image? PinmemberAdamNajmanowicz5-Jul-08 3:09 
AnswerRe: why I just get blank image? [modified] PinmemberoCasper14-Oct-07 12:42 
GeneralWhy It Does not work with some sites Pinmembermhariri27-Nov-06 6:39 
GeneralAxWebBrowser Vs. WebBrowser (.NET2) events PinmemberAssaf Koren23-Nov-06 9:43 
GeneralRe: AxWebBrowser Vs. WebBrowser (.NET2) events PinmemberAdamNajmanowicz28-Nov-06 9:23 
GeneralGetting it to work in an ASP.NET application [modified] PinmemberdB.20-Nov-06 21:51 
GeneralRe: Getting it to work in an ASP.NET application Pinmemberligaz25-Nov-06 5:06 
GeneralRe: Getting it to work in an ASP.NET application PinmemberdB.25-Nov-06 6:11 
GeneralRe: Getting it to work in an ASP.NET application PinmemberAdamNajmanowicz28-Nov-06 9:19 
GeneralRe: Getting it to work in an ASP.NET application Pinmemberchr872-Mar-07 3:19 
GeneralRe: Getting it to work in an ASP.NET application PinmemberChris Vann23-Mar-08 11:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 5 Jul 2008
Article Copyright 2006 by AdamNajmanowicz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid