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