Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / Visual Basic
Article

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

Rate me:
Please Sign up or sign in to vote.
4.66/5 (14 votes)
5 Jul 2008CPOL3 min read 170.5K   6   73   36
The article describes how to get a thumbnail of a Website in .NET Framework 2.0+ without launching a fully interactive WinForms application.

UPDATE

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!

Introduction

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:

C#
Bitmap bitmap = new Bitmap(width, height);
webBrowser1.DrawToBitmap(bitmap, 
    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...

C#
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.

C#
public void Fetch()
{
    webBrowser.Navigate(url);
        while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
        {
            Application.DoEvents();
        }
}

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 http://www.yoursite.com/ thumbnail.jpg 
  [browser_width(defaults to 800) browser_height (defaults to 600) ] 
  [thumbnail_width thumbnail_height]

Sample:
GetSiteThumbnail.exe http://www.cognifide.com/ cognifide.jpg 1280 1024 640 480

License

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


Written By
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 Pin
millerbill2-Oct-12 1:35
millerbill2-Oct-12 1:35 
GeneralThank you Pin
quannt9124-Jul-12 18:22
quannt9124-Jul-12 18:22 
GeneralAgain ... NICE JOB! Pin
BHUSCH16-Jul-09 9:10
BHUSCH16-Jul-09 9:10 
GeneralNice work Pin
Owen Gunter5-Jul-09 11:42
Owen Gunter5-Jul-09 11:42 
Generalnice app! Pin
Member 223292820-Aug-08 10:40
Member 223292820-Aug-08 10:40 
GeneralRe: nice app! Pin
AdamNajmanowicz24-Aug-08 4:04
AdamNajmanowicz24-Aug-08 4:04 
GeneralRe: nice app! Pin
Member 223292825-Aug-08 4:04
Member 223292825-Aug-08 4:04 
Generalthankx Pin
Hari Om Prakash Sharma6-Jul-08 23:21
Hari Om Prakash Sharma6-Jul-08 23:21 
GeneralA Better Way to capture website thumbnails Pin
Member 456781829-May-08 14:58
Member 456781829-May-08 14:58 
GeneralRe: A Better Way to capture website thumbnails Pin
Owen Gunter5-Jul-09 11:46
Owen Gunter5-Jul-09 11:46 
QuestionSystem.UnauthorizedAccessException [modified] Pin
J. Verdurmen14-Sep-07 12:08
J. Verdurmen14-Sep-07 12:08 
GeneralDoesn't work for me Pin
ras7819-Jul-07 23:32
ras7819-Jul-07 23:32 
I ported this app into mine, which is web-based and written in c# 2.0. I've a problem with this:

webBrowser.Navigate(url);<br />
    while (webBrowser.ReadyState != WebBrowserReadyState.Complete)<br />
    {<br />
        Application.DoEvents();<br />
    }


The DoEvents() method fire the html fetched from url to my browser, so I have to download the html file. This should not happens since the code would call the method to create the thumbnail. Any idea?
GeneralWindows namespace or type not found Pin
pinkarton7-May-07 15:07
pinkarton7-May-07 15:07 
QuestionCan Java do it? Pin
pinkarton23-Apr-07 15:18
pinkarton23-Apr-07 15:18 
AnswerRe: Can Java do it? Pin
AdamNajmanowicz24-Apr-07 7:37
AdamNajmanowicz24-Apr-07 7:37 
GeneralThumbnail width and height parameters Pin
tbenami28-Mar-07 20:25
tbenami28-Mar-07 20:25 
GeneralRe: Thumbnail width and height parameters Pin
Gilberto Francisco20-Jul-10 13:38
Gilberto Francisco20-Jul-10 13:38 
QuestionBlank Image on Websites with Java Applets Pin
Jon Ebersole9-Feb-07 12:29
Jon Ebersole9-Feb-07 12:29 
Questionwhy I just get blank image? Pin
kai.ma24-Dec-06 21:15
kai.ma24-Dec-06 21:15 
AnswerRe: why I just get blank image? Pin
methai189-Apr-07 13:31
methai189-Apr-07 13:31 
GeneralRe: why I just get blank image? Pin
Zhongjie Li9-May-07 9:09
Zhongjie Li9-May-07 9:09 
GeneralRe: why I just get blank image? Pin
Zhongjie Li10-May-07 3:47
Zhongjie Li10-May-07 3:47 
GeneralRe: why I just get blank image? Pin
Piers Lawson25-May-07 2:37
Piers Lawson25-May-07 2:37 
GeneralRe: why I just get blank image? Pin
treeleung22-Apr-08 12:27
treeleung22-Apr-08 12:27 
GeneralRe: why I just get blank image? Pin
ilKaleez15-May-08 3:12
ilKaleez15-May-08 3:12 

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

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