2015-07-06 68 views
0

我使用HtmlRenderer从html标记生成jpg图像。然而,它没有从CSS应用背景图像:HtmlRenderer不会呈现来自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代码中拥有所有标记。

+0

http://www.codeproject.com/Articles/32376/A-Professional-HTML-Renderer-You-Will-使用,不确定,但它只说支持背景颜色! –

回答

1

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> 
+0

'background-image'作品,谢谢 –

+0

是否需要添加样式属性?如果我们使用css文件呢? – TechTurtle