In case you want to display HTML with highlighted/colored syntax the simplest way to go is use WinForms RichTextBox, all is needed is to add RTF color tags surrounding HTML elements.
This helper class goal is exactly that, given a string containing HTML code it will return an RTF formatted string with the HTML elements colored.
I have used Alun Evans code as base, improving its coloring support and performance.
See it's usage in HtmlRenderer project.
Category Archives: DevLife
Generate image from HTML using HTML Renderer
The requirement to generate image from HTML snippet appears to be quite popular, as seen in a few StackOverflow questions1, so I have decided to properly support it in HTML Renderer to simplify the process and improve the results.
The support for image generation in HTML Renderer existed since day one, but it required manual handling of image graphics object, size limitations and configuration toggles.
Additionally the change to use GDI text rendering in 1.4.6.0 broke2 the most common code used for generating image with HTML Renderer, as provided in several blogs and StackOverflow answers3.
Text rendering methods comparison or GDI vs. GDI+ revised
In this post, hopefully the last on this subject1, I'm going to compare the different text rending methods available for Windows .NET application for standard resolution devices2.
I decided to revise my original findings (see: The wonders of text rendering and GDI) and do a comprehensive comparison for three reasons:
- In response to my earlier post (see: GDI text rendering to image ) Kris commented he uses GraphicsPath for text rendering to image to achieve better visual quality than standard GDI+ text rendering.
- My latest "revelation" (see: Blurry/Fuzzy GDI+ Text Rendering using anti-alias and floating-point Y coördinates ) on improving GDI+ text rendering visual quality.
- GDI+ typographic StringFormat can offer good text measurement for custom text layout.
Blurry/Fuzzy GDI+ Text Rendering using AntiAlias and floating-point Y coordinates
That's a long title that perfectly captures another issue I have found with GDI+ text rendering.
While checking Kris's claim on text rendering using GraphicsPath1 I have noticed that GDI+ text rendering using TextRenderingHint.AntiAlias looks much better (less fuzzy) in simple tests than the result of using HTML Renderer to generate transparent image (using AntiAlias hint). It's strange as HTML Renderer uses the same GDI+ text rendering method.
GDI text rendering to image
The saga of GDI text rendering issues continues, after solving Transparent text rendering with GDI I got a complain that using HTML Renderer to rendering images (PNG, bitmap, etc.) results in pixelated text as discussed in this HTML Renderer discussion.
Transparent text rendering with GDI
After changing HTML Renderer to GDI text rendering (see previous post and this one) I have encountered another issue: GDI doesn't support alpha channel, which means that GDI is unable to render anything with transparent color. A problem as before the change HTML Renderer fully supported transparent text.
Continue reading
Using native GDI for text rendering in C#
To complete my previous post on text rendering in .NET I will present here the pitfalls I encountered migrating HTML Renderer to native GDI text rendering. The final result is ready to use NativeTextRenderer class with simple managed API that can be used for native GDI text rendering.
Continue reading