How to get Web Site Thumbnail in C#

Neatly

Friday, November 17, 2006 by Adam Najmanowicz | Discussion: Software Development

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

gregrrr
Reply #1 Tuesday, February 12, 2008 7:28 PM
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
syntha3920
Reply #2 Saturday, July 10, 2010 11:57 AM

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

Hankers
Reply #3 Saturday, July 10, 2010 3:08 PM

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

starkers
Reply #4 Saturday, July 10, 2010 3:42 PM

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!)

Please login to comment and/or vote for this skin.

Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:

  • Richer content, access to many features that are disabled for guests like commenting on the forums and downloading skins.
  • Access to a great community, with a massive database of many, many areas of interest.
  • Access to contests & subscription offers like exclusive emails.
  • It's simple, and FREE!

Articles Filters

Category:
View:
Search:
Apply



walnut2