我在尝试根据完全加载的页面的内容以编程方式确定网络浏览器的宽度和高度时遇到困难。我需要此信息来捕获网页的屏幕截图。
这发生在按钮单击事件中
wbNY.Navigate(new Uri("http://www.website.com"), "_self");
wbNY.DocumentCompleted += wbNY_DocumentCompleted;
这是我的文档完成代码
private void wbNY_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url == wbNY.Url)
{
if (wbNY.ReadyState == WebBrowserReadyState.Complete)
{
DoPageChecks();
}
}
}
在 DoPageChecks 中我调用此方法
TakeScreenshot(wbNY);
这是我的 TakeScreenshot 方法
protected void TakeScreenshot(WebBrowser wb)
{
Size pageSize = new Size(wb.Document.Window.Size.Width,wb.Document.Window.Size.Height)
}
我的屏幕截图代码工作正常,所以我只是显示了我试图获取网络浏览器内容的高度和宽度的所有内容,以便我可以拍摄具有正确尺寸的屏幕截图。
我也尝试过
Size pageSize = new Size(wb.Document.Body.ScrollRectangle.Width,wb.Document.Body.ScrollRectangle.Height)
但这也没有给出正确的值。
更具体地说,高度为 0,有时约为 20px,而实际结果应接近 800px+
最佳答案
最终我继续发现mshtml有获取宽度/高度的工具。
最终的截图方法:
protected void TakeScreenshot(WebBrowser wb)
{
mshtml.IHTMLDocument2 docs2 = (mshtml.IHTMLDocument2)wbNY.Document.DomDocument;
mshtml.IHTMLDocument3 docs3 = (mshtml.IHTMLDocument3)wbNY.Document.DomDocument;
mshtml.IHTMLElement2 body2 = (mshtml.IHTMLElement2)docs2.body;
mshtml.IHTMLElement2 root2 = (mshtml.IHTMLElement2)docs3.documentElement;
int width = Math.Max(body2.scrollWidth, root2.scrollWidth);
int height = Math.Max(root2.scrollHeight, body2.scrollHeight);
// Resize the control to the exact size to display the page. Also, make sure scroll bars are disabled
wb.Width = width;
wb.Height = height;
Bitmap bitmap = new Bitmap(width, height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, width, height));
bitmap.Save(SaveImgDirectory + filename);
}
关于c# - Web浏览器控制内容宽度/高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26678279/