The site was set up mainly for accounting users with the progress of development and guide for usage of Skinstudio and IconDeveloper. Brought to you by Adam Najmanowicz - the lead developer of SkinStudio & IconDeveloper.

If you're not interested in the techincal details but rather just want the working program, You can just get it HERE

A friend of mine has recently asked if it's possible to write a console .net application to make a thumbnail of a website. The task is pretty trivial with Windows forms actually. But him being the Linux guy and all... I decided to pick up the challenge

An interesting use case. In winForms all you really need to do is drop a WebBrowser on your form and once it's loaded the page call:

webBrowser.DrawToBitmap(bitmap, bitmapRect);

When it gets tricky is when you want to do it in a console application is 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. Which might probably be enough for a quick solution, but I wanted to publish this piece of code, so I would actually NOT take a pride in something like that.

How is it done the proper way then?

So we instantiate the web control (I've written a separate class to do the dirty work for me called WebPageBitmap..

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 the 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.

Since the call is asynchronous a simple webBrowser.Navigate(url); won't cut it. Since we are in a single thread and the browser does not create a separate thread for that. Which makes sense by the old windows rule: Only th 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. 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 I used before.

 

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 here.

Also there is a compiled... binary... ready to use... compiled with love... for your downloading pleasure... HERE


Comments
on Feb 12, 2008
I couldn’t get this to work. I ended up using Snapcasa which provides free web page snapshots. Their service offers several different thumbnail sizes and the snapshots always appear really fast without the “Thumbnail Queued” image I used to get from other similar services. I also like how there is no script need to use it just an img tag in my html. Best of all its on their servers so I don’t have to use my system resources (web page snapshots take a lot) to generate thumbs. The site is snapcasa.com

Thanks
on Jul 10, 2010

One alternate to snapcasa is to use http://www.sitethumbshot.com/. fees are less and services are good.

on Jul 10, 2010

I believe the thumbnail has become a toenail after 2-1/2 years since the last post. 

on Jul 10, 2010

I believe the thumbnail has become a toenail after 2-1/2 years since the last post.

And eeew, the toe jam is right royally on the nose.

 

(We-e-e-ell... it has been bloody quiet around here, and a bloke's gotta do summat, right!)