我有一个显示一些银色灯光控件的网页。我需要以编程方式截取此网页的截图。
目前使用System.Windows.Forms.WebBrowser控件进行截屏。
当我为正常页面截取屏幕时,Forms.WebBrowser工作正常。但是对于具有Silverlight控件的页面,它不起作用。
我的截屏代码如下: Bitmap bitmap = 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));
}
答案 0 :(得分:0)
我使用此代码来获取网页的schreenshot:
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浏览器控件的界限包含截取屏幕截图所需的一切。 或者,如果要将其用作缩略图,则可以减小位图的大小。