C# - 了解控制台应用程序中 HTML 内容的高度 [英] C# - Know the height of HTML content in a console application

查看:28
本文介绍了C# - 了解控制台应用程序中 HTML 内容的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的 HTML 代码 -

I have HTML code something like -

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

在一个特定的需求中,我需要提前知道这个 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.

我的想法是在WebBrowser控件中渲染这段代码,然后取高度.

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;

但在控制台应用程序中,我无法使用 WebBrowser 控件.我还有其他方法可以做到这一点吗?

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

推荐答案

回答我自己的问题 -

Answering my own question -

我需要做的就是在我的控制台应用程序中添加对 System.Windows.Forms 的引用.之后,我可以直接在无头表单中使用 WebBrowser,而无需创建任何表单元素.

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.

使用 WebBrowser 后,我设置了合理数字的高度/宽度.像下面这样 -

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;

这篇关于C# - 了解控制台应用程序中 HTML 内容的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆