Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take Screenshots of WebBrowser Control

Tags:

c#

winforms

Could someone please share the code to take screenshots of web browser control and save it in predetermined path.

I am working with VS 2008 .Net 3.5 .

like image 374
Sandeep Pathak Avatar asked Sep 10 '10 06:09

Sandeep Pathak


2 Answers

You could use Control.DrawToBitmap(), even though it is hidden from Intellisense in VisualStudio. The WebBrowser still inherits from the base class Control, so this method does exist. But what I did was create a MenuStrip with a MenuItem that I used to test this (this is basically just a standard click-event), and instead created a graphics object, and copied a portion of the screen using the correct cordinates. The only things you will need to adjust really, is the name of the WebBrowser control, and the line that actually saves the image.

private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
    int width, height;
    width   = webBrowser1.ClientRectangle.Width;
    height  = webBrowser1.ClientRectangle.Height;
    using (Bitmap image = new Bitmap(width, height)) {
        using (Graphics graphics = Graphics.FromImage(image)) {
            Point p, upperLeftSource, upperLeftDestination;
            p                       = new Point(0, 0);
            upperLeftSource         = webBrowser1.PointToScreen(p);
            upperLeftDestination    = new Point(0, 0);
            Size blockRegionSize = webBrowser1.ClientRectangle.Size;
            graphics.CopyFromScreen(upperLeftSource, upperLeftDestination, blockRegionSize);
        }
        image.Save("C:\\Test.bmp");
    }
}
like image 112
David Anderson Avatar answered Nov 11 '22 06:11

David Anderson


Here's an article illustrating this. And there's another. And even more.

like image 31
Darin Dimitrov Avatar answered Nov 11 '22 06:11

Darin Dimitrov