2015-07-06 47 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