- bits & pieces about software development HostingAbc Logo
Show all posts.
I was wondering how to save small bitmaps programmatically of websites so I can show a gallery of these web pages on an ASP.Net page.

Obviously the first step in achieving it is to use a webbrowser control in .NET and to navigate to the specified webpage.
The second step would be to copy the image from the web browser's window to our own Bitmap using the BitBlt WinAPI method and the last step would be to save this bitmap somewhere on the file system or maybe a database.

So let's see the method which captures the content of a web browser window and saves it to a Bitmap object:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private Bitmap CaptureBrowserImage(WebBrowser browser)
{
    Bitmap resultBitmap = new Bitmap(browser.Width, browser.Height);
    Graphics g = Graphics.FromImage(resultBitmap);

    IntPtr hdcSrc = GetWindowDC(browser.Handle);
    IntPtr hdcDest = g.GetHdc();

    BitBlt(hdcDest, 0, 0, browser.Width, browser.Height, hdcSrc, 0, 0,
        (uint)TernaryRasterOperations.SRCCOPY);

    g.ReleaseHdc(hdcDest);
    ReleaseDC(browser.Handle, hdcSrc);

    return resultBitmap;
}


It might also be possible to use the StretchBlt WinAPI method to generate a smaller image, but I just wanted to save the image as it is - later on I will be able to generate thumbnails out of the saved images if I want to.

(big thanks to András for the ideea and help on using BitBlt)
add linkThe last comments:lucifer says:oh, i see now :D lucifer says:"If the LINQ runtime just thrown an exception similar to this one to your face". which one? :)apco says:1) Define the original issue. 2) The sorted list is sorted on value and there is not a key! 3) dark green text with black background: not too easy to read.moszidev says:Actually I've made these 2 forms with Expression Blend. I'm not sure if the extensive designing capabilities will be included in the VS designer ...Venemo says:I've just tried out Visual Studio 2008 Express, and it doesn't have the functionality to rotate the controls, but when I copied your source code, it recognised it. Anyway, it seems very similar to VS 2005, and only has a few improvements in its interBéla says:At long last! I'm looking forward to it! Keep it up! and so on ;)
Copyright (C) 2007, Molnar Szilveszter m@il me