Click here to Skip to main content
12,547,806 members (30,847 online)
Click here to Skip to main content
Add your own
alternative version


515 bookmarked

A Professional HTML Renderer You Will Use

, 29 Jan 2009 BSD
Rate this:
Please Sign up or sign in to vote.
100% managed code that draws HTML on any device

(See history for list of changes.) 



This is a library of 100% managed code that draws beautifully formatted HTML. It comes along with three WinForms controls:

  • HtmlPanel
  • HtmlLabel
  • HtmlTooltip

And a static method ready to draw HTML:

  • HtmlRenderer.Render(Graphics g, string html, RectangleF area, bool clip) 

Note: The drawing engine is based on the CSS Level 2 specification.


For years, I have been planning for a project like this. I prepared myself 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 paddings. 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.

Imagine the power that drawing full-rich-formatted HTML on your controls can give to your applications. Use bold when you need it, italics on every message, and borders and fonts as you may like or need everywhere on the desktop application. One of the first projects where I will use it is on the tooltips of my Ribbon Project.

Although I have not tested it on mono yet, there should be no problem at all, since all of the code in the library is managed code and the methods it uses to paint are quite basic. It draws lines, rectangles, curves and text.

For now, the render looks really nice. Sometimes it can fool you to think you're using a real Web browser, trust me, download the demo, it is just an EXE and a DLL.

Using the Code

The library locates the code under the System.Drawing.Html namespace. The controls that render HTML are under the System.Windows.Forms namespace.

The renderer follows the CSS Box Model. Box model is nothing but a tree of boxes, just as the tree of HTML, each of these boxes is represented by a very used class called CssBox. The start node is represented by the class InitialContainer.

All the known CSS properties apply to each of these boxes. Each box may contain any number of child boxes and just one parent. The only box that has no parent at all is the so called Initial Container.

A typical use of an Initial Container to draw HTML would look like this:

//Create the InitialContainer
InitialContainer c = new InitialContainer("<html>");
//Give bounds to the container
//Measure bounds of each box on the tree

//Paint the HTML document


First a label, then a panel and at last a ToolTip, all of which support HTML rendering.

You may never use it, since I provided controls and methods that create this object for you.


A panel that is ready to accept HTML code via its Text property. Its full name is System.Windows.Forms.HtmlPanel.

The only properties you need to know are:

  • AutoScroll. Activates/Deactivates the auto-scroll capabilities as you know. It is set to true by default.
  • Text. Gets/Sets the HTML source.

The panel will update the bounds of the elements as you scroll or resize the control.


A label that is ready to accept HTML code via its Text property. Its full name is System.Windows.Forms.HtmlLabel.

The only properties you need to know are:

  • AutoScroll. Activates/Deactivates the auto-scroll capabilities as you know. It is set to true by default.
  • AutoSize. Sets the size of the label automatically if activated.
  • Text. Gets/Sets the HTML source.

Some interesting things:

  • The label will update the bounds of the elements as you scroll or resize the control.
  • The label can be transparent.
  • The panel has better performance than the label.


Works exactly like the ToolTip you already know, with the little difference that this tooltip will render HTML on it. It's full name is System.Windows.Forms.HtmlToolTip.

There are no properties here to learn. Use it just the way you use the ToolTip that comes with the framework. Internally, it just handles the OwnerDraw event.

Some Features of my Own

I took the liberty of adding a couple of features:

  • Background gradients
  • Rounded corners

These are achieved through the following CSS properties:

  • background-gradient: (color)
  • background-gradient-angle: (number)
  • corner-ne-radius: (length)
  • corner-nw-radius: (length)
  • corner-se-radius: (length)
  • corner-se-radius: (length)
  • corner-radius: (length){1,4} (shorthand for all corners)

What's Currently Supported by the Renderer?

  • Most border, padding and margin and positioning CSSproperties (except for the height property)
  • Text alignment horizontally and vertically, text indents too
  • Lists, ordered and unordered. Advanced numbering is not yet supported
  • Tables, almost all of it. Cell combinations work quite well as far as I tested them
  • Fonts (partially) and Colors
  • Backgrounds (just color)

Points of Interest

What can I say, this is one of the most fun projects I've ever been involved with. And so far, it runs beautifully and checks its original design goals.

I am planning to give it full rendering support, to the day that you may visualize a web page just as a good web browser would; and why not, make a WYSIWYG HTML editor to give amazing HTML editing power to your applications.

I'm also planning to make sure it runs perfectly well on Mono and on Mobile platforms.

In the next few days, I'll publish a list of supported HTML tags and CSS properties.


  • Jan 08 2009: First release
  • Jan 29 2009: Minor bugs fixed 
    • Text Encoding of samples
    • Large paragraphs and fonts because of Culture  
    • License 


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

Jose Menendez Póo
Team Leader
Mexico Mexico
I'm in game programming now:

Jose Manuel Menéndez Poó

- I've been programming Windows and Web apps since 1997.
- My greatest concern nowadays is user interface usability.

Questions and stuff by twitter: @menendezpoo


You may also be interested in...


Comments and Discussions

GeneralParagraph align does not work if Paragraph contains multiple Elements Pin
Andreas Stolle31-Jul-09 1:32
memberAndreas Stolle31-Jul-09 1:32 
GeneralAwesome Pin
David Roh28-Jul-09 22:28
memberDavid Roh28-Jul-09 22:28 
QuestionLicense? Pin
Cory Crooks7-Jul-09 9:48
memberCory Crooks7-Jul-09 9:48 
QuestionProblems with spaces after punctuation and "&amp;nbsp;" [modified] Pin
JPaula25-Jun-09 13:03
memberJPaula25-Jun-09 13:03 
GeneralFont Size not honoured - trying to fix it [modified] Pin
JPaula23-Jun-09 11:01
memberJPaula23-Jun-09 11:01 
GeneralBR was not working. Pin
dnaprice18-Jun-09 5:41
memberdnaprice18-Jun-09 5:41 
GeneralSame here Pin
JPaula23-Jun-09 14:43
memberJPaula23-Jun-09 14:43 
GeneralBR Fixed PinPopular
Andreas Stolle31-Jul-09 1:22
memberAndreas Stolle31-Jul-09 1:22 
First: Thanks for this great part of work.

I had the same problem: "br" does not break lines.
Fix is simple here:

Just change "CssDefaults.cs" line#26 to
dir, hr, menu, pre, br { display: block }

GeneralRe: BR Fixed Pin
Doncp14-Oct-09 14:32
memberDoncp14-Oct-09 14:32 
GeneralMissing Navigating Event Pin
Jan Prochazka alias jena15-May-09 23:59
memberJan Prochazka alias jena15-May-09 23:59 
GeneralRe: Missing Navigating Event Pin
Member 603819621-May-09 23:48
memberMember 603819621-May-09 23:48 
GeneralRe: Missing Navigating Event Pin
Jan Prochazka alias jena25-May-09 9:00
memberJan Prochazka alias jena25-May-09 9:00 
QuestionPossible browser? Pin
The Dogcow Farmer12-May-09 12:54
memberThe Dogcow Farmer12-May-09 12:54 
GeneralBase64 encoded images support Pin
Fco. Javier Marin6-May-09 4:44
memberFco. Javier Marin6-May-09 4:44 
GeneralChanges to support Transparency Pin
JoaoGouveia24-Apr-09 15:28
memberJoaoGouveia24-Apr-09 15:28 
GeneralRe: Changes to support Transparency Pin
Jose M. Menendez Poó27-Apr-09 15:15
memberJose M. Menendez Poó27-Apr-09 15:15 
General<br> Not Supported.</br> Pin
Abydosgater16-Apr-09 15:23
memberAbydosgater16-Apr-09 15:23 
GeneralSeems nice. &lt;table style="height: 100%"&gt; not being honored Pin
TK_dna8-Apr-09 7:38
memberTK_dna8-Apr-09 7:38 
GeneralRe: Seems nice. &lt;table style="height: 100%"&gt; not being honored Pin
Jose M. Menendez Poó8-Apr-09 10:04
memberJose M. Menendez Poó8-Apr-09 10:04 
GeneralScrollbars Pin
todna10-Mar-09 6:23
membertodna10-Mar-09 6:23 
GeneralOne feature is important Pin
Seraph_summer21-Feb-09 0:48
memberSeraph_summer21-Feb-09 0:48 
Generalother functions supported: search, copy and so on... Pin
Seraph_summer20-Feb-09 23:38
memberSeraph_summer20-Feb-09 23:38 
GeneralHTMLRender ignores the Location.Y Pin
OlafD19-Feb-09 5:43
memberOlafD19-Feb-09 5:43 
GeneralRe: HTMLRender ignores the Location.Y Pin
JPaula23-Jun-09 9:14
memberJPaula23-Jun-09 9:14 
GeneralRe: HTMLRender ignores the Location.Y Pin
XNE115-Jul-10 8:26
memberXNE115-Jul-10 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 30 Jan 2009
Article Copyright 2009 by Jose Menendez Póo
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid