截取 WebBrowser 控件的屏幕截图 [英] Take Screenshots of WebBrowser Control

查看:39
本文介绍了截取 WebBrowser 控件的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以分享代码来截取网页浏览器控件的屏幕截图并将其保存在预定路径中.

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

我正在使用 VS 2008 .Net 3.5.

I am working with VS 2008 .Net 3.5 .

推荐答案

您可以使用 Control.DrawToBitmap(),即使它在 VisualStudio 中对 Intellisense 隐藏.WebBrowser 仍然继承自基类 Control,因此该方法确实存在.但是我所做的是创建一个带有 MenuItem 的 MenuStrip,我用来测试它(这基本上只是一个标准的点击事件),而是创建了一个图形对象,并使用正确的坐标复制了屏幕的一部分.您真正需要调整的唯一事项是 WebBrowser 控件的名称,以及实际保存图像的行.

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

这篇关于截取 WebBrowser 控件的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆