Generate image from HTML markup
HTML Renderer can be used to generate image (pmg, jpeg, bmp...) from HTML markup string.Because of GDI text rendering issue with alpha channel clear type text rendering rendering to image requires special handling.
- Generate image with solid color background
- Generate image with image background
- Generate image with transparent background
HTML used in examples
<body style="font: 10pt Tahoma"> <h3 style="color: navy; margin-bottom: 8px">Render to Image</h3> <hr /> <table style="border: 1px solid maroon; margin-top: 5px"> <tr style="vertical-align: top;"> <td width="32" style="padding: 2px 0 0 0"> <img src="http://download-codeplex.sec.s-msft.com/Download?ProjectName=HtmlRenderer&DownloadId=770243" /> </td> <td>This <i>text</i> is inside a <b>table</b> <u>element</u>.<br /> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ornare mollis elit. </td> </tr> </table> <ul style="margin-top: 5px"> <li><span style="color: red">Colors</span></li> <li><span style="background-color: #8dd">Back colors</span></li> </ul> </body>
Solid color background
No size restrictions
Image image = HtmlRender.RenderToImage(html);
- Generated image size depends on HTML content greedy layout.
- Width will be set by the longest line without wrapping.
- Height will be set by exact height of the HTML.
- The default background color is used - white.
Fixed size image
Image image = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);
- Generate image of fixed size - 400x200 pixels.
- HTML layout is restricted by max width - 400 pixels.
- Image heights extend beyond the actual height of the HTML.
- If image height was smaller than actual HTML height the rendered HTML would be clipped.
- Use Color.Linen solid background color.