以编程方式截取网页屏幕截图(使用 Silverlight 控件)
我有一个网页显示一些银光控件。我需要以编程方式截取该网页的屏幕截图。
当前使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用此代码来截取网页的屏幕截图:
Web 浏览器控件的结构边界包含截取屏幕截图所需的所有内容。
或者,如果您想将位图用作缩略图,则可以减小位图的大小。
I use this code to take schreenshot of web page:
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.