gpt4 book ai didi

c# - 不使用 WebBrowser 的 Html 渲染到图像(C#/Net)

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:48 25 4
gpt4 key购买 nike

我在数据库中存储了 html 字符串/文档(如果您在 IE 浏览器中查看任何网页的源代码,生成的 html 代码),我需要使用 C#/.Net wcf 服务将此 html 转换为图像。当前代码使用的是 WebBrowser,它工作正常,只是它非常慢并且偶尔会遇到问题。

我想在不使用 WebBrowser 的情况下替换此功能(html 转换为 .png 图像)。我尝试使用 TheArtofdev.HtmlRenderer 似乎可以解决大多数问题,但是当它遇到 html(包含多个“div>”可能是?)时似乎遇到了问题,就像下面的 html 并且它没有正确生成图像。它仅为该 html 生成部分图像。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style>p { padding: 0px; border: 0px ; margin: 0px;}ul { padding: 0px; border: 0px; margin: 0px;}</style><title>Item 18626</title></head><body><table border="0" width="700"><tr><td><strong><font face="verdana,geneva">Item 18626</font></strong><br></br></td></tr><tr><td><font face="verdana,geneva"><p style="font-weight:normal; font-weight:normal; ">Use the following passages to answer the question. </p><p style="font-weight:normal; ">&#xA0;</p><div align="center"><p><b>Mariana's Thesis Statement</b></p></div><div align="center"><table><colgroup style="width:46.6206em; " /><tbody><tr><td style="padding:0.2292em;  border:0.0834em solid #000000; "><p style="">The Cold War policy of containment was a success.</p></td></tr></tbody></table></div><p>&#xA0;</p><div align="center"><p><b>Information on the Vietnam War</b></p></div><div align="center"><table><colgroup style="width:46.6206em; " /><tbody><tr><td style="padding:0.2292em;  border:0.0834em solid #000000; "><p style="">The United States sent advisors to South Vietnam in 1954 to help the government fight communist guerrillas who wanted to reunite with communist North Vietnam. By 1964, the United States began sending combat troops. The U.S. military used extensive bombing and advanced technology; however, they were unable to turn the tide against communism. In 1973, the United States withdrew from South Vietnam, and in 1975, the government of South Vietnam fell to the communists and the country was reunited.</p></td></tr></tbody></table></div><p>&#xA0;</p><p>Mariana is writing a paper about the Cold War and discovers the information above on the Vietnam War. </p><p style="font-weight:normal; ">&#xA0;</p><p style="font-weight:normal; ">Describe one way the passage above refutes Mariana's thesis.</p><p style="font-weight:normal; ">&#xA0;</p><p style="font-weight:normal; ">Type your answer in the space provided.</p></font></td></tr><tr><td>&nbsp;</td></tr><tr><td><div id="001" style="border:solid black 1px;width:700px;overflow-x:hidden;overflow-y:visible;text-align:left;word-wrap:break-word"><p>it refutes because it dosent talk about the cold war it only talks about the vietnam war and it just says that the codntainment war was a success thats all it says about the containment war it &nbsp;really just talks about the vietnam war</p>
</div></td></tr></table><br></br></body></html>

我可以调整上面的 html 以某种方式使 HtmlRender 快乐吗?如果 HtmlRender 没有按预期工作,还有哪些其他选项(.Net/C#)?非常感谢任何帮助!!

最佳答案

您可以使用 headless browser 来做到这一点例如PhantomJS ,它有一个 NuGet package你可以安装在你的项目中。使用 PhantomJS,您可以 create a screenshot呈现的页面。

PhantomJS 包与 Selenium WebDriver 配对和 Selenium WebDriver Support Classes packages 允许您使用对另一个 stackoverflow 问题 Getting screenshot using PhantomJS in C# 的回答中给出的方法稍作修改来截取屏幕截图。 (请注意,您需要添加 OpenQA.Selenium.PhantomJS、OpenQA.Selenium.Support.Extensions 和 System.Drawing.Imaging 命名空间):

PhantomJSDriver driver = new PhantomJSDriver();
driver.Manage().Window.Maximize(); // optional
driver.Navigate().GoToUrl("file:///C://fullpath//file.html");

driver.TakeScreenshot().SaveAsFile("screenshot.png", ImageFormat.Png);

driver.Quit();

关于c# - 不使用 WebBrowser 的 Html 渲染到图像(C#/Net),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34299218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com