Wednesday, March 15, 2006

Screen Capture for Invisible Windows

I was recently tinkering around in an admin tool that is used to build web-sites, and found myself wondering if it was possible to create a "preview" of the web-site as an image. The idea being, I want to show a list of pages in the website with thumbnails of page previews; thus the site administrator can see the entire site on a single page and would be able to know what they were editing before they opened the page configuration.

If you're like me, these kinds of problems drive me crazy. I ear mark them as "nifty ideas" and I slowly mull them over in my head when my head has idle time. (What, you don't have head idle time? Between 3-4 in the morning when I change sleeping positions, I have to force myself not to address my mental challenge To-do list. I recently discovered that our brains solve a lot of problems after only a few hours of sleep.)

So although I haven't actually cracked the code open to tackle this problem, here's what I envision the implementation to be:

  1. Use the .NET Process object to launch a window to the web page.
  2. Grab the handle of the Process and pass it to the GDI+ to convert into an image. This CodeProject article gives a really good overview of doing just that.

I'm hoping that if the .NET Process is launched with the ProcessWindowStyle set to "Hidden" that it still has a valid device context used to paint onto the screen. If that's true, there should be no reason why I couldn't convert the hidden window's content into a Bitmap.

Few other things to consider, such as when to determine that the browser has fully loaded the requested page? I might have to consider using the WebBrowser control to load the webpage and wait until the document onload event or page status changes....

Yet another task to play with...

submit to reddit

2 comments:

alexsaves said...

Hey did you make any progress with this? I am trying to do exactly the same thing. Thanks! alexei.white(at)gmail.com

bryan said...

Hey Alex,

I can't remember where I left on this, as this was one of those Wouldn't-it-be-cool-if rants I tend to go on. Around the time of this post, I found two really useful posts on CodeProject:

- Capture Entire Web Page
- Image Capture Whole Page using C#

Both links take a similar approach: they drive the WebBrowser programatically. By using the WebBrowser object you should be also be able to tap into the Browser's events, which would solve the onload issue. Interestingly enough, Selenium exposes a method in their API to capture screen shots -- useful if you want to create a snapshot when an automated web-test fails.

Regarding invisible windows, I'm not so sure that either of these approaches would work; most of the comments on the CodeProject articles refer to missing portions of active windows, etc. Though I haven't confirmed this.

Another approach would be to build an application that crawls your web site as a background service. The service would run under a different user account and may be able to interact with it's own shell/profile.

I haven't got too much time these days, but if you're interested in collaborating on something, please let me know.