c# - HtmlRenderer 不从 css 渲染背景图像

标签 c# html css .net html-rendering

我使用 HtmlRenderer从 html 标记生成 jpg 图像。但是它不应用来自 css 的背景图像:

<div style="background:url(https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTbNHtYU6bKKmxkJXlqDr7y7afZyZtw-WwDg6DoUNCb9nElkplEuw); width: 440px; height: 361px">
   Some content here.
</div>

这是 C# 代码:

String ecardHtml = File.ReadAllText("testWithGoodImage.html");
using (Image img = HtmlRender.RenderToImageGdiPlus(ecardHtml, maxWidth: 440, textRenderingHint: TextRenderingHint.AntiAliasGridFit))
{
    img.Save("result.jpg");
}

调试显示甚至没有请求图像,所以我怀疑它不被支持。然而<img>元素确实运作良好。

所以问题是如何让它发挥作用。

我知道我可以在 HtmlRenderer 中在图像上呈现 html,但我只想在 html 代码中包含所有标记。

最佳答案

HTML 渲染器支持背景图像 CSS,如果你真的想
在 HTML 中做它,那么像这样的东西就可以完成工作:

<div style="width: 440px; height: 361px;position:relative">
    <div style="position:absolute;top:0;left:0;z-index:8888"><img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTbNHtYU6bKKmxkJXlqDr7y7afZyZtw-WwDg6DoUNCb9nElkplEuw"/></div>
    <div style="position:absolute;top:0;left:0;z-index:9999">Some content here.</div>       
</div>

或者简单地添加背景图像而不仅仅是背景

<div style="background-image:url(https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTbNHtYU6bKKmxkJXlqDr7y7afZyZtw-WwDg6DoUNCb9nElkplEuw); width: 440px; height: 361px">
   Some content here.
</div>

关于c# - HtmlRenderer 不从 css 渲染背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31244279/

相关文章:

c# - 允许用户选择要导入的列标题

c# - 我们应该在 C++ 中使用装饰器模式吗?

JavaScript 在 HTML 中创建新行,onclick 将值粘贴到另一个下面

html - 组合悬停不起作用

javascript - 固定 div 的宽度,即使数据很小

c# - 将对象列表序列化为对象的属性

c# - 8 字节纯文本的 DES 加密结果为 16 字节数组

javascript - Flash 窃取浏览器焦点

javascript - 带有自动隐藏和显示的清除按钮的文本输入

html - css - 左侧边栏不在左侧