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:
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()
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 here.
Also there is a compiled... binary... ready to use... compiled with love... for your downloading pleasure... HERE