A quick and dirty way is to use WinForms WebBrowser and draw it in a bitmap. Doing this in a stand-alone console application is somewhat difficult because you need to be aware of the consequences of hosting STAThread when using a fundamentally asynchronous programming pattern. But here is a working proof of the concept that captures a web page in a 800x600 BMP file:
namespace WebBrowserScreenshotSample { using System; using System.Drawing; using System.Drawing.Imaging; using System.Threading; using System.Windows.Forms; class Program { [STAThread] static void Main() { int width = 800; int height = 600; using (WebBrowser browser = new WebBrowser()) { browser.Width = width; browser.Height = height; browser.ScrollBarsEnabled = true;
To compile this, create a new console application and be sure to add assembly links for System.Drawing and System.Windows.Forms .
UPDATE: I rewrote the code to avoid using the WaitOne / DoEvents pattern for hacker polling. This code should be closer to the following guidelines.
UPDATE 2:. You indicate that you want to use this in a Windows Forms application. In this case, forget about dynamically creating the WebBrowser . You want to create a hidden (Visible = false) instance of WebBrowser in your form and use it in the same way as shown above. Here is another example that shows part of the form’s user code with a text box ( webAddressTextBox ), a button ( generateScreenshotButton ) and a hidden browser ( WebBrowser ). Although I worked on this, I found a feature that I could not handle before - the DocumentCompleted event can be raised several times depending on the nature of the page. This sample should work as a whole, and you can expand it to do whatever you want:
namespace WebBrowserScreenshotFormsSample { using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { this.InitializeComponent();
bobbymcr Dec 30 '09 at 19:26 2009-12-30 19:26
source share