Good Old GDI+ (or: Unblur Thy Text)

It's been a while since I've done anything with GDI+ (i.e. System.Drawing). System.Windows (i.e. WPF) is so much more powerful.

However, there's one area where it seems the good old GDI+ can still surpass it's shiny new successor: text.

There have been many complaints about text rendering in WPF. I wrote about some of them myself. You can find a lot of questions on the WPF forum (to name a few), but not a lot of answers.

The main issue is that small text is blurry and unreadable. When asked how to render aliased text in WPF, the answer was: it's impossible. However, I recently encountered a whitepaper on windowsclient.net called "Text Clarity in WPF", which suggests a way to do just that. It's worth a read, even though the aliased text solution provided there is not usable (they use FormattedText, convert it to a Geometry and render the Geometry aliased using RenderOptions.SetEdgeMode() - which indeed renders aliased text, but it's not quite legible.)

As you may have already guessed, my solution - GdiTextBlock - relies on GDI+. We render a bitmap that contains the text and display it. As you may know, rendering bitmaps in WPF also has it's problems, which is why I use the Bitmap control in order to prevent bitmaps from becoming blurry as well.

Also, in order to maintain compatibility with WPF's TextBlock, and to avoid forcing the consumers of the control to add a reference to the System.Drawing assembly, the properties and their types are all the same as TextBlock's (in fact, the dependency properties are registered using DependencyProperty.AddOwner(), which also ensures that value inheritance down the visual tree works.) CoerceValueCallback is used in many places since GDI+ doesn't support all the options available in WPF (e.g. FontStyles.Oblique, non-solid Foreground, TextAlignment.Justify.) While I've included a property called TextQuality, which allows you to set the TextRenderingHint, you'll see that only the default (SingleBitPerPixelGridFit) provides legible results.

One last note about performance - this solution is not quite optimal. It creates a bitmap every measure pass. I haven't done any real performance tests, so I can't tell you what the real penalty is. Use it at your own discretion.

Update: Fixed a small bug and added size and color selectors to demo app.

Another Update: Fixed a leak when using InteropBitmap. See this post.

Attachment: GdiTextBlock.zip
Published 08-18-2008 11:35 AM by aelij
Filed under: ,

Comments

# Improper use of InteropBitmap can cause a memory leak

Wednesday, October 22, 2008 5:29 AM by TrackBack

I've been using InteropBitmap in my GdiTextBlock . The control creates a new GDI+ Bitmap every measure

# re: Good Old GDI+ (or: Unblur Thy Text)

Wednesday, April 21, 2010 1:00 AM by hu

Welcome to www.manoloblahnikcvs.com, from here you will hunt for any designer shoes which you desire!all style Manolo Blahnik Boots sale Ever since 2005, when we became the best online dealer of authentic Giuseppe Zanotti , Gucci,Cheap Manolo Blahnik Boots,Cheap Ugg Boots , Yves Saint Lauret , Christian Louboutin , and were upgraded to be a retailer for these brands,we have been devoted to provide customers with a large selection with finest quality at lowest price.We strive to build long-term relationship with each and every one of our customers, we can only accomplish our mission if you’re happy with the products you buy.So we’ve created a guarantee that takes all the risk out of shopping!All goods shipped are insured, free and no customs taxes!1-2days to pick up and 5-6days to your doors!you can also have a good view in our ed hardy clothing store.

# jordan18

Thursday, June 24, 2010 1:29 AM by jordan18

good

# re: Good Old GDI+ (or: Unblur Thy Text)

Friday, July 23, 2010 1:56 AM by zoro

Certainly,Manolo enjoys fashion, classic,art and comfort,  ladies cant help madding with her.

<a href="http://www.manoloblahnikselling.com"><strong>manolo blahnik</strong></a>

<a href="http://www.manoloblahnikselling.com"><strong>manolo blahnik shoes</strong></a>

<a href="http://www.manoloblahnikselling.com"><strong>manolo blahniks</strong></a>

<a href="http://www.manoloblahnikselling.com"><strong>manolo blahnik heels</strong></a>

<a href="www.manoloblahnikselling.com/jimmy-choo"><strong>jimmy choo</strong></a>

<a href="www.manoloblahnikselling.com/manolo-blahnik-boots"><strong>manolo blahnik boots</strong></a>

<a href="www.manoloblahnikselling.com/manolo-blahnik-pumps"><strong>manolo blahnik pumps</strong></a>

<a href="www.manoloblahnikselling.com/manolo-blahnik-sandals"><strong>manolo blahnik sandals</strong></a>

<a href="www.manoloblahnikselling.com/christian-louboutin"><strong>christian louboutin</strong></a>

<a href="www.manoloblahnikselling.com/christian-louboutin-sandals"><strong>christian louboutin sandals</strong></a>

<a href="www.manoloblahnikselling.com/christian-louboutin-pumps"><strong>christian louboutin pumps</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-annees-folles-140-black-sandals"><strong>Christian Annees Folles 140 black Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-peep-toe-glitter-slingbacks"><strong>Christian Louboutin Peep-Toe Glitter Slingbacks</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-peep-toe-pink-sandals"><strong>Christian Louboutin Peep-toe Pink sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-platform-cage-prink-sandals"><strong>Christian Louboutin Platform Cage Prink Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-tulip-thong-pink-sandals"><strong>Christian Louboutin Tulip Thong Pink Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-123-double-platform-gold-sandals"><strong>Christian Louboutin 123 Double Platform Gold Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-black-sandals"><strong>Christian Louboutin Black Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-black-sandals-rodita-zip"><strong>Christian Louboutin Black Sandals Rodita Zip</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-blue-satinorsays-sandals"><strong>Christian Louboutin Blue Satin d'Orsays Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-claudia-ankle-strap-black-sandals"><strong>Christian Louboutin Claudia Ankle-Strap Black Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-claudia-ankle-strap-coffee-sandals"><strong>Christian Louboutin Claudia Ankle-Strap Coffee Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-claudia-ankle-strap-red-sandals"><strong>Christian Louboutin Claudia Ankle-Strap Red Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-cutout-booties-white"><strong>Christian Louboutin Cutout Booties White</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-cutout-black-sandals"><strong>christian louboutin cutout black sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-cutout-booties-black"><strong>Christian Louboutin Cutout Booties black</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-cutout-booties-brown"><strong>Christian Louboutin Cutout Booties Brown</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-cutout-sandals-diamond"><strong>Christian Louboutin Cutout Sandals Diamond</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-double-platform-gold-sandals"><strong>Christian Louboutin Double-Platform Gold Sandals</strong></a>

<a href="www.manoloblahnikselling.com/.../christian-louboutin-double-platform-blue-sandals"><strong>Christian Louboutin Double-Platform Blue Sandals</strong></a>

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 5 and 6 and type the answer here:
Powered by Community Server (Non-Commercial Edition), by Telligent Systems