Quantcast
Channel: HtmlRenderer Wiki & Documentation Rss Feed
Viewing all 73 articles
Browse latest View live

Updated Wiki: Image generation

$
0
0

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 and clear type text rendering, rendering to image requires special handling.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background

HTML used in examples

img.png
<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>

Expected result (as rendered in Google Chrome):
Expected.png

Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Using "HtmlRender.RenderToImage" methods.

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.
html0.png

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.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png

Image background

  • Using GDI text rendering.


Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • Using "HtmlRender.RenderToImageGdiPlus" methods.

Default TextRenderingHint - SingleBitPerPixelGridFit

Image image = HtmlRender.RenderToImageGdiPlus(html, 460);
  • The image background is fully transparent.
  • Rendered text is not as smooth as with GDI text rendering.
  • Used SingleBitPerPixelGridFit TextRenderingHint
html3.png

AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, 460, 0, TextRenderingHint.AntiAlias);
  • The image background is fully transparent.
  • Rendered text is blurred by Anti-Aliasing.
html4.png

GDI vs. GDI+

Image loading optimization





Updated Wiki: Image generation

$
0
0

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 and clear type text rendering, rendering to image requires special handling, for details see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is whatever the generated image need to be transparent or can it be filled by background.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background

HTML used in examples

img.png
<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>

Expected result (as rendered in Google Chrome):
Expected.png

Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Using "HtmlRender.RenderToImage" methods.

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.
html0.png

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.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png

Image background

  • Using GDI text rendering.


Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • Using "HtmlRender.RenderToImageGdiPlus" methods.

Default TextRenderingHint - SingleBitPerPixelGridFit

Image image = HtmlRender.RenderToImageGdiPlus(html, 460);
  • The image background is fully transparent.
  • Rendered text is not as smooth as with GDI text rendering.
  • Used SingleBitPerPixelGridFit TextRenderingHint
html3.png

AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, 460, 0, TextRenderingHint.AntiAlias);
  • The image background is fully transparent.
  • Rendered text is blurred by Anti-Aliasing.
html4.png

GDI vs. GDI+ text rendering

Image loading optimization




Updated Wiki: Quick start

Updated Wiki: Documentation

$
0
0
The rich formating power of HTML in your .NET applications without WebBrowser control or MSHTML.

Navigation:

Getting Started

  • Download the binaries.
  • Run the Demo application
    • Check HTMLs showcasing renderer capabilities.
    • Paste your own HTML to see the power of the renderer.
  • Add the dll to Visual Studio Toolbox
  • Drag-n-drop Html WinForms controls on your controls.
  • Set the Text property with HTML and see it renders.

History

For years, I (Jose) have been planning for a project like this. I prepared my self quite well. I went through the entire CSS Level 2 specification along with the HTML 4.01 specification.

One of the most interesting things I found is this: Drawing HTML is no more than laying out a bunch of boxes with borders margins and padding's. Once you overpass this paradigm, everything else is to help the code actually place the boxes on the right place, and then paint the string each box contains.

In October 2012 I (Arthur) was looking to replace the usage of WinForms WebBrowser control by something that can render complex html and have good performance and stability characteristics.

HTML Renderer project showed great promise but had significant performance issues, lacked many features and wasn't updated for more than 3 years. Realizing there is no alternative I embraced the project making it my baby.

...

See Also

Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is whatever the generated image need to be transparent or can it be filled by background, be it a solid color (like web-browsers white) or existing image to render the HTML on it.

The second criteria in image generation is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires, controlled by different method overloads provided. In all cases HTML layout is executed to find the rendered html desired size and layout the HTML in the given restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. GDI vs. GDI+ text rendering
  5. Image loading optimization

HTML used in examples

img.png
<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>

Expected result (as rendered in Google Chrome):
Expected.png

Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Using "HtmlRender.RenderToImage" methods.

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.
html0.png

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.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png

Image background

  • Using GDI text rendering.


Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • Using "HtmlRender.RenderToImageGdiPlus" methods.

Default TextRenderingHint - SingleBitPerPixelGridFit

Image image = HtmlRender.RenderToImageGdiPlus(html, 460);
  • The image background is fully transparent.
  • Rendered text is not as smooth as with GDI text rendering.
  • Used SingleBitPerPixelGridFit TextRenderingHint
html3.png

AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, 460, 0, TextRenderingHint.AntiAlias);
  • The image background is fully transparent.
  • Rendered text is blurred by Anti-Aliasing.
html4.png

GDI vs. GDI+ text rendering

Image loading optimization




Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. GDI vs. GDI+ text rendering
  5. Image loading optimization

HTML used in examples

img.png
<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>

Expected result (as rendered in Google Chrome):
Expected.png

Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Using "HtmlRender.RenderToImage" methods.

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.
html0.png

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.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png

Image background

  • Using GDI text rendering.


Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • Using "HtmlRender.RenderToImageGdiPlus" methods.

Default TextRenderingHint - SingleBitPerPixelGridFit

Image image = HtmlRender.RenderToImageGdiPlus(html, 460);
  • The image background is fully transparent.
  • Rendered text is not as smooth as with GDI text rendering.
  • Used SingleBitPerPixelGridFit TextRenderingHint
html3.png

AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, 460, 0, TextRenderingHint.AntiAlias);
  • The image background is fully transparent.
  • Rendered text is blurred by Anti-Aliasing.
html4.png

GDI vs. GDI+ text rendering

Image loading optimization




Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. GDI vs. GDI+ text rendering
  5. Image loading optimization

HTML used in examples

img.png
<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>

Expected result (as rendered in Google Chrome):
Expected.png

Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Using "HtmlRender.RenderToImage" methods.

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.
html0.png

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.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png

Image background

  • Using GDI text rendering.


Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • Using "HtmlRender.RenderToImageGdiPlus" methods.

Default TextRenderingHint - SingleBitPerPixelGridFit

Image image = HtmlRender.RenderToImageGdiPlus(html, 460);
  • The image background is fully transparent.
  • Rendered text is not as smooth as with GDI text rendering.
  • Used SingleBitPerPixelGridFit TextRenderingHint
html3.png

AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, 460, 0, TextRenderingHint.AntiAlias);
  • The image background is fully transparent.
  • Rendered text is blurred by Anti-Aliasing.
html4.png

GDI vs. GDI+ text rendering

I have written a "little" on this issue: I strongly recommend to read the links above nevertheless I will provide a brief explanation here.
...

Image loading optimization




Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. HTML used in examples
  5. GDI vs. GDI+ text rendering
  6. Image loading optimization

1. Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

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.
html0.png

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.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png


2. Image background

  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • See the HTML used in examples.



3. Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • TextRenderingHint can be controlled, default: AntiAlias.
  • Better used for high DPI devices like print.
  • Less sharp and may have letter-symbols alignments small issues (font dependent).
  • Slower than GDI (80%-150% slower, see The wonders of text rendering and GDI).
  • Using "HtmlRender.RenderToImageGdiPlus" methods.
  • See the HTML used in examples.

AntiAlias (default) TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, 460);
  • The image background is fully transparent.
  • Rendered text is blurred a little by Anti-Aliasing (compared to GDI text rendering).
html4.png

SingleBitPerPixelGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, 460, 0, TextRenderingHint.SingleBitPerPixelGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or AntiAlias text rendering.
  • Used SingleBitPerPixelGridFit TextRenderingHint
html3.png


4. HTML used in examples

img.png
<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>

Expected result (as rendered in Google Chrome):
Expected.png

5. GDI vs. GDI+ text rendering

I have written a "little" on this issue: I strongly recommend to read the links above.
Nevertheless I will provide a brief explanation here:
...

6. Image loading optimization





Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. HTML used in examples
  5. GDI vs. GDI+ text rendering
  6. Image loading optimization

1. Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = HtmlRender.RenderToImage(html);
  • The default background color is used - white.
  • 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.
html0.png

Fixed size image

Image image = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);
  • Use Color.Linen solid background color.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png


2. Image background

  • Using GDI text rendering.
  • Excellent performance.
  • No new image object is created, the HTML is rendered on the given image object.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15,10));
  • bla
html5.png

Max size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15, 10), new Size(550, 120));
  • bla
html6.png

3. Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • TextRenderingHint can be controlled, default: AntiAlias.
  • Better used for high DPI devices like print.
  • Less sharp and may have letter-symbols alignments small issues (font dependent).
  • Slower than GDI (80%-150% slower, see The wonders of text rendering and GDI).
  • Using "HtmlRender.RenderToImageGdiPlus" methods.
  • See the HTML used in examples.

AntiAlias (default) TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, 460);
  • The image background is fully transparent.
  • Rendered text is blurred a little by Anti-Aliasing (compared to GDI text rendering).
  • 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.
html3.png

SingleBitPerPixelGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, 460, 0, TextRenderingHint.SingleBitPerPixelGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or AntiAlias text rendering.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html4.png


4. HTML used in examples

HTML snippet
<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>

References image in the HTML snippet
img.png

Background Image used in Generate image with image background
Background image

Expected result (as rendered in Google Chrome):
Expected.png

5. GDI vs. GDI+ text rendering

I have written a "little" on this issue: I strongly recommend to read the links above.
Nevertheless I will provide a brief explanation here:
...

6. Image loading optimization




Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. HTML used in examples
  5. GDI vs. GDI+ text rendering
  6. Image loading optimization

1. Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = HtmlRender.RenderToImage(html);
  • The default background color is used - white.
  • 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.
html0.png

Fixed size image

Image image = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);
  • Use Color.Linen solid background color.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png


2. Image background

  • Using GDI text rendering.
  • Excellent performance.
  • No new image object is created, the HTML is rendered on the given image object.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15,10));
  • bla
html5.png

Max size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15, 10), new Size(550, 120));
  • bla
html6.png

3. Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • TextRenderingHint can be controlled, default: AntiAlias.
  • Better used for high DPI devices like print.
  • Less sharp and may have letter-symbols alignments small issues (font dependent).
  • Slower than GDI (80%-150% slower, see The wonders of text rendering and GDI).
  • Using "HtmlRender.RenderToImageGdiPlus" methods.
  • See the HTML used in examples.

AntiAlias (default) TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html);
  • The image background is fully transparent.
  • Rendered text is blurred a little by Anti-Aliasing (compared to GDI text rendering).
  • 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.
html3.png

SingleBitPerPixelGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(400, 200), TextRenderingHint.SingleBitPerPixelGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or AntiAlias text rendering.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html4.png


4. HTML used in examples

HTML snippet
<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>

References image in the HTML snippet
img.png

Background Image used in Generate image with image background
Background image

Expected result (as rendered in Google Chrome):
Expected.png

5. GDI vs. GDI+ text rendering

I have written a "little" on this issue: I strongly recommend to read the links above.
Nevertheless I will provide a brief explanation here:
...

6. Image loading optimization




Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. HTML used in examples
  5. GDI vs. GDI+ text rendering
  6. Image loading optimization

1. Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = HtmlRender.RenderToImage(html);
  • The default background color is used - white.
  • 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.
html0.png

Fixed size image

Image image = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);
  • Use Color.Linen solid background color.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png


2. Image background

  • Using GDI text rendering.
  • Excellent performance.
  • No new image object is created, the HTML is rendered on the given image object.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15,10));
  • bla
html5.png

Max size restriction

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15, 10), new Size(550, 120));
  • bla
html6.png

3. Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • TextRenderingHint can be controlled, default: AntiAlias.
  • Better used for high DPI devices like print.
  • Less sharp and may have letter-symbols alignments small issues (font dependent).
  • Slower than GDI (80%-150% slower, see The wonders of text rendering and GDI).
  • Using "HtmlRender.RenderToImageGdiPlus" methods.
  • See the HTML used in examples.

No size restrictions with AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html);
  • The image background is fully transparent.
  • The default TextRenderingHint is used - AntiAlias.
  • Rendered text is blurred a little by Anti-Aliasing (compared to GDI text rendering).
  • 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.
html3.png

Fixed size image with AntiAliasGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(400, 200), TextRenderingHint.AntiAliasGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to to fir into the restricted width.
  • 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.
html4.png

Min/Max size restrictions with SingleBitPerPixelGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(650, 50), new Size(1400, 106), TextRenderingHint.SingleBitPerPixelGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html7.png


4. HTML used in examples

HTML snippet
<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>

Referenced image in the HTML snippet
img.png

Background Image used in Generate image with image background
Background image

Expected result (as rendered in Google Chrome):
Expected.png

5. GDI vs. GDI+ text rendering

I have written a "little" on this issue: I strongly recommend to read the links above.
Nevertheless I will provide a brief explanation here:
...

6. Image loading optimization




Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. HTML used in examples
  5. GDI vs. GDI+ text rendering
  6. Image loading optimization

1. Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = HtmlRender.RenderToImage(html);
  • The default background color is used - white.
  • 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.
html0.png

Fixed size image

Image image = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);
  • Use Color.Linen solid background color.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png


2. Image background

  • Using GDI text rendering.
  • Excellent performance.
  • No new image object is created, the HTML is rendered on the given image object.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No explicit size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15,10));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15, 10) offset to better fit over the background image.
  • HTML layout is restricted by image width.
    • Text is wrapped to fit into the restricted width.
  • If image height was smaller than actual HTML height the rendered HTML would be clipped.
html5.png

Max size restriction

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15, 10), new Size(550, 120));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15, 10) offset to better fit over the background image.
  • HTML layout is restricted by given max-width.
    • Actual max height\width must is also restricted by image size.
    • Text is wrapped to fit into the restricted width.
  • HTML is clipped by the max height if cannot fit in.
html6.png


3. Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • TextRenderingHint can be controlled, default: AntiAlias.
  • Better used for high DPI devices like print.
  • Less sharp and may have letter-symbols alignments small issues (font dependent).
  • Slower than GDI (80%-150% slower, see The wonders of text rendering and GDI).
  • Using "HtmlRender.RenderToImageGdiPlus" methods.
  • See the HTML used in examples.

No size restrictions with AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html);
  • The image background is fully transparent.
  • The default TextRenderingHint is used - AntiAlias.
  • Rendered text is blurred a little by Anti-Aliasing (compared to GDI text rendering).
  • 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.
html3.png

Fixed size image with AntiAliasGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(400, 200), TextRenderingHint.AntiAliasGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html4.png

Min/Max size restrictions with SingleBitPerPixelGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(650, 50), new Size(1400, 106), TextRenderingHint.SingleBitPerPixelGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html7.png


4. HTML used in examples

HTML snippet
<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>

Referenced image in the HTML snippet
img.png

Background Image used in Generate image with image background
Background image

Expected result (as rendered in Google Chrome):
Expected.png

5. GDI vs. GDI+ text rendering

I have written a "little" on this issue: I strongly recommend to read the links above.
Nevertheless I will provide a brief explanation here:
...

6. Image loading optimization




Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. HTML used in examples
  5. GDI vs. GDI+ text rendering
  6. Image loading optimization

1. Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = HtmlRender.RenderToImage(html);
  • The default background color is used - white.
  • 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.
html0.png

Fixed size image

Image image = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);
  • Use Color.Linen solid background color.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png


2. Image background

  • Using GDI text rendering.
  • Excellent performance.
  • No new image object is created, the HTML is rendered on the given image object.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No explicit size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15,10));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15,10) offset to better fit over the background image.
  • HTML layout is restricted by image width.
    • Text is wrapped to fit into the restricted width.
  • If image height was smaller than actual HTML height the rendered HTML would be clipped.
html5.png

Explicit max size restriction

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15, 10), new Size(550, 120));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15,10) offset to better fit over the background image.
  • HTML layout is restricted by given max-width.
    • Actual max height\width must is also restricted by image size.
    • Text is wrapped to fit into the restricted width.
  • HTML is clipped by the max height if cannot fit in.
html6.png


3. Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • TextRenderingHint can be controlled, default: AntiAlias.
  • Better used for high DPI devices like print.
  • Less sharp and may have letter-symbols alignments small issues (font dependent).
  • Slower than GDI (80%-150% slower, see The wonders of text rendering and GDI).
  • Using "HtmlRender.RenderToImageGdiPlus" methods.
  • See the HTML used in examples.

No size restrictions with AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html);
  • The image background is fully transparent.
  • The default TextRenderingHint is used - AntiAlias.
  • Rendered text is blurred a little by Anti-Aliasing (compared to GDI text rendering).
  • 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.
html3.png

Fixed size image with AntiAliasGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(400, 200), TextRenderingHint.AntiAliasGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html4.png

Min/Max size restrictions with SingleBitPerPixelGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(650, 50), new Size(1400, 106), TextRenderingHint.SingleBitPerPixelGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html7.png


4. HTML used in examples

HTML snippet
<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>

Referenced image in the HTML snippet
img.png

Background Image used in Generate image with image background
Background image

Expected result (as rendered in Google Chrome):
Expected.png

5. GDI vs. GDI+ text rendering

I have written a "little" on this issue: I strongly recommend to read the links above.
Nevertheless I will provide a brief explanation here:
...

6. Image loading optimization




Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of GDI text rendering issues with alpha channel and clear type text rendering, rendering to image requires special handling that this page is dedicated to.
For technical details on the issues see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

Note: If the rendered HTML define its own background, either by color or image, it will hide the background defined by the rendering method. Though you can always use opacity or margins so the defined background will be visible nevertheless therefor this scenario is not covered for simplicity.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. HTML used in examples
  5. GDI vs. GDI+ text rendering
  6. Image loading optimization

1. Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = HtmlRender.RenderToImage(html);
  • The default background color is used - white.
  • 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.
html0.png

Fixed size image

Image image = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);
  • Use Color.Linen solid background color.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png


2. Image background

  • Using GDI text rendering.
  • Excellent performance.
  • No new image object is created, the HTML is rendered on the given image object.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No explicit size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15,10));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15,10) offset to better fit over the background image.
  • HTML layout is restricted by image width.
    • Text is wrapped to fit into the restricted width.
  • If image height was smaller than actual HTML height the rendered HTML would be clipped.
html5.png

Explicit max size restriction

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15, 10), new Size(550, 120));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15,10) offset to better fit over the background image.
  • HTML layout is restricted by given max-width.
    • Actual max height\width is also restricted by image size.
    • Text is wrapped to fit into the restricted width.
  • HTML is clipped by the max height if cannot fit in.
html6.png


3. Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • TextRenderingHint can be controlled, default: AntiAlias.
  • Better used for high DPI devices like print.
  • Less sharp and may have letter-symbols alignments small issues (font dependent).
  • Slower than GDI (80%-150% slower, see The wonders of text rendering and GDI).
  • Using "HtmlRender.RenderToImageGdiPlus" methods.
  • See the HTML used in examples.

No size restrictions with AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html);
  • The image background is fully transparent.
  • The default TextRenderingHint is used - AntiAlias.
  • Rendered text is blurred a little by Anti-Aliasing (compared to GDI text rendering).
  • 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.
html3.png

Fixed size image with AntiAliasGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(400, 200), TextRenderingHint.AntiAliasGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html4.png

Min/Max size restrictions with SingleBitPerPixelGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(650, 50), new Size(1400, 106), TextRenderingHint.SingleBitPerPixelGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html7.png


4. HTML used in examples

HTML snippet
<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>

Referenced image in the HTML snippet
img.png

Background Image used in Generate image with image background
Background image

Expected result (as rendered in Google Chrome):
Expected.png

5. GDI vs. GDI+ text rendering

I have written a "little" on this issue: I strongly recommend to read the links above.
Nevertheless I will provide a brief explanation here:
...

6. Image loading optimization




Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of technical limitations, rendering to image requires special handling, this page goal is to provide all the tools required for successful and high-quality HTML image generation.
For technical details see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. HTML used in examples
  5. GDI vs. GDI+ text rendering

1. Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = HtmlRender.RenderToImage(html);
  • The default background color is used - white.
  • 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.
html0.png

Fixed size image

Image image = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);
  • Use Color.Linen solid background color.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png


2. Image background

  • Using GDI text rendering.
  • Excellent performance.
  • No new image object is created, the HTML is rendered on the given image object.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No explicit size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15,10));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15,10) offset to better fit over the background image.
  • HTML layout is restricted by image width.
    • Text is wrapped to fit into the restricted width.
  • If image height was smaller than actual HTML height the rendered HTML would be clipped.
html5.png

Explicit max size restriction

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15, 10), new Size(550, 120));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15,10) offset to better fit over the background image.
  • HTML layout is restricted by given max-width.
    • Actual max height\width is also restricted by image size.
    • Text is wrapped to fit into the restricted width.
  • HTML is clipped by the max height if cannot fit in.
html6.png


3. Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • TextRenderingHint can be controlled, default: AntiAlias.
  • Better used for high DPI devices like print.
  • Less sharp and may have letter-symbols alignments small issues (font dependent).
  • Slower than GDI (80%-150% slower, see The wonders of text rendering and GDI).
  • Using "HtmlRender.RenderToImageGdiPlus" methods.
  • See the HTML used in examples.

No size restrictions with AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html);
  • The image background is fully transparent.
  • The default TextRenderingHint is used - AntiAlias.
  • Rendered text is blurred a little by Anti-Aliasing (compared to GDI text rendering).
  • 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.
html3.png

Fixed size image with AntiAliasGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(400, 200), TextRenderingHint.AntiAliasGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html4.png

Min/Max size restrictions with SingleBitPerPixelGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(650, 50), new Size(1400, 106), TextRenderingHint.SingleBitPerPixelGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html7.png


4. HTML used in examples

HTML snippet
<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>

Referenced image in the HTML snippet
img.png

Background Image used in Generate image with image background
Background image

Expected result (as rendered in Google Chrome):
Expected.png

5. GDI vs. GDI+ text rendering

In a nutshell, GDI text rendering provides the best performance and visual quality.
Reasons:
  1. GDI+ is resolution independent, it may cause glyphs to increase in width tightening up the text.
  2. The font hinting of GDI+ has lower quality than GDI, especially for lower sizes.
  3. GDI+ is stateless in nature casing it to set and reset its context multiple times.
But GDI text rendering has two limitations:
  1. Direct rendering to image device results in corrupted pixels.
  2. Text rendering with transparent background results in corrupted pixels.
result.png

The first issue can be addressed by in-memory bitmap buffer tricks, this is what happens in "RenderToImage" methods under the hood.
The second issue cannot be solved using GDI text rendering, therefor "RenderToImageGdiPlus" methods are provided that use GDI+ text rendering.

Even more details:

Updated Wiki: Home

$
0
0
The rich formatting power of HTML in your .NET applications using simple controls or static rendering code.
The library is 100% managed C# code without any external dependencies (no WebBrowser control, ActiveX / COM or MSHTML dll), the only requirement is .NET 2.0 or higher.

Renderer1.png

Features and Benefits

  • Extensive HTML 4.01 and CSS level 2 specifications support.
  • Support separating CSS from HTML by loading stylesheet code separately.
  • Support text selection, copy-paste and context menu.
  • WinForms controls: HtmlPanel, HtmlLabel and HtmlToolTip.
  • Create images from HTML snippets.
  • Handles "real world" malformed HTML, it doesn't have to be XHTML.
  • 100% managed code and no external dependencies.
  • Supports .NET 2.0 or higher including Client Profile.
  • Lightweight single dll (~250K).
  • High performance and low memory footprint.
  • Extendable and configurable.
  • Powerful Demo application to explore and learn the library.

WinForms controls

  • HtmlPanel - The full power of HTML control build to replace WinForms WebBrowser control, accepts HTML, text selection, scrollbars, link click intercept, image load intercept and much more.
  • HtmlLabel - As WinForms label but accepts HTML, text selection, auto-size capabilities, transparent background and more.
  • HtmlToolTip - As WinForms ToolTip control but accepts HTML and ability to handle links.

Sample application

  • Render HTML content generated by rich web editors like forums, blogs, etc.
  • Render Office documents converted to HTML.
  • Create complex WinForms UI with HTML instead of numerous WinForms controls with complex layout logic.
  • Create WinForms UI that requires text selection with clipboard support.
  • Create images from HTML code snippets.

NuGet package install

NuGet package

Manual install

  • Download the binaries.
  • Reference the proper .NET release (2.0, 3.5, 4.0, 4.5) in your project.

Usage

  • Add HtmlRenderer to Visual Studio Toolbox (drag-drop the dll on it).
  • Drag-n-drop HtmlPanel, HtmlLabel or HtmlToolTip from the Toolbox.
  • Set the Text property with your HTML.

Source

Authors

Updated Wiki: Image generation

$
0
0

Generate image from HTML markup

HTML Renderer can be used to generate image (pmg, jpeg, bmp, etc.) from HTML markup snippet.
Because of technical limitations, rendering to image requires special handling, this page goal is to provide all the tools required for successful and high-quality HTML image generation.
For technical details see GDI vs. GDI+ text rendering.

When approaching image generation the most critical question is the generated image background, must the background have transparency (full or partial) or can it be completely filled by non-transparent color, be it solid color (like web-browsers white), gradient or existing image to render the HTML on it. Therefor the page presents image rendering via 3 options: solid color, image and transparent background.

The second criteria is the generated image size: is it required to be of specific size, restricted by minimum and maximum values or free to be as large as the HTML requires. In all cases, HTML layout is executed to find the rendered html desired size and to layout the HTML in the given restrictions. Therefor each background rendering options has overloads to handle the different size restrictions.

  1. Generate image with solid color background
  2. Generate image with image background
  3. Generate image with transparent background
  4. HTML used in examples
  5. GDI vs. GDI+ text rendering

1. Solid color background

  • Render HTML with solid background color.
  • Using GDI text rendering.
  • Excellent performance.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No size restrictions

Image image = HtmlRender.RenderToImage(html);
  • The default background color is used - white.
  • 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.
html0.png

Fixed size image

Image image = HtmlRender.RenderToImage(html, new Size(400, 200), Color.Linen);
  • Use Color.Linen solid background color.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html1.png

Min/Max size restrictions

Image image = HtmlRender.RenderToImage(html, new Size(650, 50), new Size(1400, 106), Color.SeaShell);
  • Generate image with minimum and maximum size restrictions.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html2.png


2. Image background

  • Using GDI text rendering.
  • Excellent performance.
  • No new image object is created, the HTML is rendered on the given image object.
  • Sharp and excellent aligned text.
  • Using "HtmlRender.RenderToImage" methods.
  • See the HTML used in examples.

No explicit size restrictions

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15,10));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15,10) offset to better fit over the background image.
  • HTML layout is restricted by image width.
    • Text is wrapped to fit into the restricted width.
  • If image height was smaller than actual HTML height the rendered HTML would be clipped.
html5.png

Explicit max size restriction

Image image = Image.FromFile("base.png");
HtmlRender.RenderToImage(image, html, new Point(15, 10), new Size(550, 120));
  • HTML is rendered on top of the given background image.
  • HTML is rendered at (15,10) offset to better fit over the background image.
  • HTML layout is restricted by given max-width.
    • Actual max height\width is also restricted by image size.
    • Text is wrapped to fit into the restricted width.
  • HTML is clipped by the max height if cannot fit in.
html6.png


3. Transparent background

  • Render HTML with transparent background.
  • Using GDI+ text rendering.
  • TextRenderingHint can be controlled, default: AntiAlias.
  • Better used for high DPI devices like print.
  • Less sharp and may have letter-symbols alignments small issues (font dependent).
  • Slower than GDI (80%-150% slower, see The wonders of text rendering and GDI).
  • Using "HtmlRender.RenderToImageGdiPlus" methods.
  • See the HTML used in examples.

No size restrictions with AntiAlias TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html);
  • The image background is fully transparent.
  • The default TextRenderingHint is used - AntiAlias.
  • Rendered text is blurred a little by Anti-Aliasing (compared to GDI text rendering).
  • 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.
html3.png

Fixed size image with AntiAliasGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(400, 200), TextRenderingHint.AntiAliasGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Generate image of fixed size - 400x200 pixels.
  • HTML layout is restricted by max width - 400 pixels.
    • Text is wrapped to fit into the restricted width.
  • 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.
html4.png

Min/Max size restrictions with SingleBitPerPixelGridFit TextRenderingHint

Image image = HtmlRender.RenderToImageGdiPlus(html, new Size(650, 50), new Size(1400, 106), TextRenderingHint.SingleBitPerPixelGridFit);
  • The image background is fully transparent.
  • Rendered text is not as smooth as GDI or non GridFit text rendering.
  • Restricted max width is above the max required width to layout the longest text without wrapping so it has no effect.
  • Restricted min width is also above the required layout width (541px) therefore the html is extended.
  • the resulting image width is 650px and the html uses the full width to layout.
  • Restricted max height is lower than the required height to fully render the html therefore it is clipped at the restricted height value: 106px.
html7.png


4. HTML used in examples

HTML snippet
<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>

Referenced image in the HTML snippet
img.png

Background Image used in Generate image with image background
Background image

Expected result (as rendered in Google Chrome):
Expected.png

5. GDI vs. GDI+ text rendering

In a nutshell, GDI text rendering provides the best performance and visual quality.
Reasons:
  1. GDI+ is resolution independent, it may cause glyphs to increase in width tightening up the text.
  2. The font hinting of GDI+ has lower quality than GDI, especially for lower sizes.
  3. GDI+ is stateless in nature casing it to set and reset its context multiple times.
But GDI text rendering has two limitations:
  1. Direct rendering to image device results in corrupted pixels.
  2. Text rendering with transparent background results in corrupted pixels.
result.png

The first issue can be addressed by in-memory bitmap buffer tricks, this is what happens in "RenderToImage" methods under the hood.
The second issue cannot be solved using GDI text rendering, therefor "RenderToImageGdiPlus" methods are provided that use GDI+ text rendering.

Even more details:

Updated Wiki: Home

$
0
0
Cross framework (WinForms/WPF/PDF/Metro/Mono/etc.), Multipurpose (UI Controls / Image generation / PDF generation / etc.), 100% managed (C#), High performance HTML Rendering library.

The library is 100% managed C# code without any external dependencies (no WebBrowser control, ActiveX / COM or MSHTML dll), the only requirement is .NET 2.0 or higher.

WinForms

Features and Benefits

  • Extensive HTML 4.01 and CSS level 2 specifications support.
  • Support separating CSS from HTML by loading stylesheet code separately.
  • Support text selection, copy-paste and context menu.
  • WinForms controls: HtmlPanel, HtmlLabel and HtmlToolTip.
  • WPF controls: HtmlPanel and HtmlLabel.
  • Works on Mono.
  • Create images from HTML snippets.
  • Handles "real world" malformed HTML, it doesn't have to be XHTML.
  • 100% managed code and no external dependencies.
  • Supports .NET 2.0 or higher including Client Profile.
  • Lightweight, just two DLLs (~300K).
  • High performance and low memory footprint.
  • Extendable and configurable.
  • Powerful Demo application to explore and learn the library.

WinForms/WPF controls

  • HtmlPanel - The full power of HTML control build to replace WebBrowser control, accepts HTML, text selection, scrollbars, link click intercept, image load intercept and much more.
  • HtmlLabel - As WinForms label but accepts HTML, text selection, auto-size capabilities, transparent background and more.
  • HtmlToolTip - As WinForms ToolTip control but accepts HTML and ability to handle links (WinForms only).

Sample application's

  • Render HTML content generated by rich web editors like forums, blogs, etc.
  • Render Office documents converted to HTML.
  • Create UI that requires text selection with clipboard support.
  • Create images from HTML code snippets (Image generation).
  • Create PDF document from HTML code snippets.

NuGet packages

Source

Authors

Updated Wiki: Home

$
0
0
Cross framework (WinForms/WPF/PDF/Metro/Mono/etc.), Multipurpose (UI Controls / Image generation / PDF generation / etc.), 100% managed (C#), High performance HTML Rendering library.

The library is 100% managed C# code without any external dependencies (no WebBrowser control, ActiveX / COM or MSHTML dll), the only requirement is .NET 2.0 or higher.

WinForms

Features and Benefits

  • Extensive HTML 4.01 and CSS level 2 specifications support.
  • Support separating CSS from HTML by loading stylesheet code separately.
  • Support text selection, copy-paste and context menu.
  • WinForms controls: HtmlPanel, HtmlLabel and HtmlToolTip.
  • WPF controls: HtmlPanel and HtmlLabel.
  • Works on Mono.
  • Create images from HTML snippets.
  • Handles "real world" malformed HTML, it doesn't have to be XHTML.
  • 100% managed code and no external dependencies.
  • Supports .NET 2.0 or higher including Client Profile.
  • Lightweight, just two DLLs (~300K).
  • High performance and low memory footprint.
  • Extendable and configurable.
  • Powerful Demo application to explore and learn the library.

WinForms/WPF controls

  • HtmlPanel - The full power of HTML control build to replace WebBrowser control, accepts HTML, text selection, scrollbars, link click intercept, image load intercept and much more.
  • HtmlLabel - As WinForms label but accepts HTML, text selection, auto-size capabilities, transparent background and more.
  • HtmlToolTip - As WinForms ToolTip control but accepts HTML and ability to handle links (WinForms only).

Sample application's

  • Render HTML content generated by rich web editors like forums, blogs, etc.
  • Render Office documents converted to HTML.
  • Create UI that requires text selection with clipboard support.
  • Create images from HTML code snippets (Image generation).
  • Create PDF document from HTML code snippets.

NuGet packages

Source

Authors

Updated Wiki: PDF generation

Viewing all 73 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>