从WebBrowser控件生成屏幕截图

时间:2010-09-30 02:56:20

标签: sharepoint browser screenshot

我从下面的链接中抓取了一些代码,以便从网络浏览器控件生成屏幕截图: http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx

然后,我修改为像这样的扩展方法:

public static Bitmap GetScreenshot(this WebBrowser webBrowser)
        {
            // Load the webpage into a WebBrowser control
            using (WebBrowser wb = new WebBrowser())
            {
                wb.ScrollBarsEnabled = false;
                wb.ScriptErrorsSuppressed = true;
                wb.Navigate(webBrowser.Url);
                while (wb.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                }

                // Set the size of the WebBrowser control
                wb.Width = wb.Document.Body.ScrollRectangle.Width;
                wb.Height = wb.Document.Body.ScrollRectangle.Height;

                // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
                Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
                wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));

                return bitmap;
            }
        }

它适用于大多数网页,但我尝试将其用于SharePoint网站,并且图像每次都以250x250的形式出现。 :-s

这里的任何天才能给我一个解决方案吗?

提前致谢

3 个答案:

答案 0 :(得分:0)

嗯,原因似乎是我的新WebBrowser(wb)即使在我调用Navigate()之后也未初始化。 Url属性显示为null。这很奇怪。这是否意味着我无法在代码中创建WebBrowser并且永远不会显示它?必须展示这件事是一种耻辱。 : - (

答案 1 :(得分:0)

您可以检查问题taking-screenshot-of-a-webpage-programmatically 它基于此webpage_thumbnailer,它使用Web浏览器控件。

答案 2 :(得分:0)

首先,webbrowser控件下不存在DrawToBitmap类。我建议使用CopyFromScreen。这是一段代码:

    Bitmap b = new Bitmap(wb.ClientSize.Width, webBrowser1.ClientSize.Height);
    Graphics g = Graphics.FromImage(b);
    g.CopyFromScreen(this.PointToScreen(wb.Location), new Point(0, 0), wb.ClientSize);
    //The bitmap is ready. Do whatever you please with it!
    b.Save(@"c:\screenshot.jpg", ImageFormat.Jpeg);
    MessageBox.Show("Screen Shot Saved!");
相关问题