以编程方式截取网页屏幕截图(使用 Silverlight 控件)

发布于 2024-08-29 11:40:09 字数 1494 浏览 6 评论 0原文

我有一个网页显示一些银光控件。我需要以编程方式截取该网页的屏幕截图。

当前使用 System.Windows.Forms.WebBrowser 控件进行屏幕截图。

当我为普通页面截屏时,Forms.WebBrowser 工作正常。但是对于带有 Silverlight 控件的页面,它不起作用。

我的截图代码如下: 位图 位图 = null; 使用 (WebBrowser webBrowser = new WebBrowser()) { webBrowser.ScrollBarsEnabled = false; webBrowser.ScriptErrorsSuppressed = true;

            // Set the size of the WebBrowser control
            webBrowser.Width = width;
            webBrowser.Height = height;

            // Load the webpage into a WebBrowser control
            webBrowser.Navigate(url);
            while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            if (width == -1)
            {
                // Take Screenshot of the web pages full width
                webBrowser.Width = webBrowser.Document.Body.ScrollRectangle.Width;
            }

            if (height == -1)
            {
                // Take Screenshot of the web pages full height
                webBrowser.Height = webBrowser.Document.Body.ScrollRectangle.Height;

            }

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

}

I have a web page that displays some silver light controls. I need to take screenshot of this web page programmatically.

Currently using the System.Windows.Forms.WebBrowser control for taking screenshot.

Forms.WebBrowser works fine when I take screenshot for normal pages. However for the pages with Silverlight controls it does not work.

My code for taking screenshot is as follows:
Bitmap bitmap = null;
using (WebBrowser webBrowser = new WebBrowser())
{
webBrowser.ScrollBarsEnabled = false;
webBrowser.ScriptErrorsSuppressed = true;

            // Set the size of the WebBrowser control
            webBrowser.Width = width;
            webBrowser.Height = height;

            // Load the webpage into a WebBrowser control
            webBrowser.Navigate(url);
            while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }

            if (width == -1)
            {
                // Take Screenshot of the web pages full width
                webBrowser.Width = webBrowser.Document.Body.ScrollRectangle.Width;
            }

            if (height == -1)
            {
                // Take Screenshot of the web pages full height
                webBrowser.Height = webBrowser.Document.Body.ScrollRectangle.Height;

            }

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2024-09-05 11:40:09

我使用此代码来截取网页的屏幕截图:

    string myPicsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\SchreenshotFolder\";
    if (System.IO.Directory.Exists(myPicsPath))
    {
      Rectangle bounds = yourBrowser.Bounds;
      using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
      {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
          g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
        }
        bitmap.Save(myPicsPath + System.IO.Path.GetRandomFileName() + ".png", System.Drawing.Imaging.ImageFormat.Png);
      }
    }
    else
    {
      System.IO.Directory.CreateDirectory(myPicsPath);
      MessageBox.Show("Screenshot directory created in " + Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\" + " named SchreenshotFolder\nScreenshot is not saved. Hit button again to save it.");
    }

Web 浏览器控件的结构边界包含截取屏幕截图所需的所有内容。
或者,如果您想将位图用作缩略图,则可以减小位图的大小。

I use this code to take schreenshot of web page:

    string myPicsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\SchreenshotFolder\";
    if (System.IO.Directory.Exists(myPicsPath))
    {
      Rectangle bounds = yourBrowser.Bounds;
      using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
      {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
          g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
        }
        bitmap.Save(myPicsPath + System.IO.Path.GetRandomFileName() + ".png", System.Drawing.Imaging.ImageFormat.Png);
      }
    }
    else
    {
      System.IO.Directory.CreateDirectory(myPicsPath);
      MessageBox.Show("Screenshot directory created in " + Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\" + " named SchreenshotFolder\nScreenshot is not saved. Hit button again to save it.");
    }

Structure Bounds of web browser control contains everything you need for taking a screenshot.
Or you can reduce size of bitmap if you want to use it as thumbnail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文