WebBrowser get page size

萝らか妹 提交于 2019-12-11 15:14:57

问题


When working with WebBrowser on IE 9 and above the property

webBrowser.Document.Body.ScrollRectangle.Size

returns always 250 x 250 as body dimensions. Therefore I can't come across a way to check the current page size.

How can I check a html page actual size using IE 9 and above?


回答1:


This should get you the window size

webBrowser.Document.Window.Size




回答2:


I've found a solution that seemed to work all the time until now.

I hope it'll help people with the same problem as I do, as you might have figured out in IE 9 or above, the Body element contains the default size for the ScrollRectangle property, however, as I found out, some other elements do contain the ScrollRectangle property with different sizes.

Most certainly that the HTML element contains a correct ScrollRectangle property, but some other elements might contain a ScrollRectangle greater or smaller in size, and sometimes it fits better.

So I've come to a conclusion that checking all the elements for the ScrollRectangle property is the smartest thing to do, here's the script :

int
    CurrentWidth
    , CurrentHeight
    , CurrentMinWidth = WebBrowserOuterPanel.Width
    , CurrentMaxWidth = 0
    , CurrentMinHeight = WebBrowserOuterPanel.Height
    , CurrentMaxHeight = 0;

foreach(HtmlElement webBrowserElement in webBrowser.Document.All)
{
    if ((CurrentWidth = Math.Max(webBrowserElement.ClientRectangle.Width, webBrowserElement.ScrollRectangle.Width)) > CurrentMaxWidth)
        CurrentMaxWidth = CurrentWidth;

    if ((CurrentHeight = Math.Max(webBrowserElement.ClientRectangle.Height, webBrowserElement.ScrollRectangle.Height)) > CurrentMaxHeight)
        CurrentMaxHeight = CurrentHeight;
}

webBrowser.Size = new Size (CurrentMaxWidth > CurrentMinWidth ? CurrentMaxWidth : CurrentMinWidth, CurrentMaxHeight > CurrentMinHeight ? CurrentMaxHeight : CurrentMinHeight);

Another way but might be not correct, in idea for implementing

HtmlElement webBrowserElement = webBrowser.Document.Body.FirstChild;

CurrentMaxWidth = Math.Max(webBrowserElement.ClientRectangle.Width, webBrowserElement.ScrollRectangle.Width);

CurrentMaxHeight = Math.Max(webBrowserElement.ClientRectangle.Height, webBrowserElement.ScrollRectangle.Height);


来源:https://stackoverflow.com/questions/51638984/webbrowser-get-page-size

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