本文介绍了以编程方式获取网页的屏幕快照(带有Silverlight控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示一些银色灯光控件的网页.我需要以编程方式对此网页进行截图.

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

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

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

Forms.WebBrowser可以正常屏幕截图时正常工作.但是,对于具有Silverlight控件的页面,它不起作用.

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

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

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

}

推荐答案

我使用以下代码获取网页的schreenshot:

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

Web浏览器控件的结构范围包含拍摄屏幕快照所需的一切.或者,如果您想将其用作缩略图,则可以减小位图的大小.

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.

这篇关于以编程方式获取网页的屏幕快照(带有Silverlight控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:56