Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

C# - Know the height of HTML content in a console application

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

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

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...