Taking Screenshot of Web Page (with Silverlight Controls) Programatically

♀尐吖头ヾ 提交于 2019-12-09 19:00:53

问题


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

}


回答1:


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.



来源:https://stackoverflow.com/questions/2673842/taking-screenshot-of-web-page-with-silverlight-controls-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!