开发者

Take Screenshots of WebBrowser Control

开发者 https://www.devze.com 2023-01-15 19:55 出处:网络
Could someone please share the code to take screenshots of web browser control and save it in 开发者_运维百科predetermined path.

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 .


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");
    }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消