Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have HTML code something like -

<html>
<body>
<table><tr><td><h1>Heading</h1></td></tr></table>
</body>
</html>

In a specific requirement, I need to know in advance that how much height will this HTML will take to display in fullscreen. So that I keep that much space calculated specifically for that content.

What I thought was to render this code in WebBrowser control, and then take the height.

        this.webBrowser1.Url = new Uri(htmlFilePath);
        //The below code will force the webbrowser control to load the HTML in it.
        while (this.webBrowser1.Document.Body == null)
        {
            Application.DoEvents();
        }
        int height = this.webBrowser1.Document.Body.ScrollRectangle.Height;

But in console application I can't use WebBrowser control. Is there any other way I can accomplish this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
208 views
Welcome To Ask or Share your Answers For Others

1 Answer

Answering my own question -

All I needed to do was adding reference of System.Windows.Forms in my console application. After that I was able to use WebBrowser in headless form directly without creating any form element.

After using WebBrowser, I set the height/width of reasonable number. Something like below -

WebBrowser wb = new WebBrowser();
//Setting the page height and width to a reasonable number. So that whatever the content loaded in it gets aligned properly.
//For me 3000 works fine for my requirements.
wb.Width = 3000; 
wb.Height = 3000;

wb.Url = new Uri(htmlFilePath);
//the below code will force the webbrowser control to load the html in it.
while (wb.Document.Body == null)
{
    Application.DoEvents();
}
int height = wb.Document.Body.ScrollRectangle.Height;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...