Ohad's Blog

Lets talk about .net !

Mirror at:
blogs.microsoft.co.il

News

         Ohad Israeli's Facebook profile
      

C# Code Snippts

Favorite Blogs

Israeli .Net Bloggers

ClearType & Graphics.DrawString

I was making some fine tuning to my system prior to my TechEd Beat Box presentation … one of the tuning was to change the vs ide to use Consolas the cool ClearType font.

After applying the ClearType setting I've noticed something very strange…

The fonts of my Pattern – User Control seem to become very ugly…. (see the Bass Drum, Snare Drum & rim font) 

I’ve also seen that the buttons on the right seems fine… and they are actually also drawn the same way…i.e. using DrawString

I’ve started investigating it and it seems that if you are using Graphics.DrawString on transparent background you get the ugly font while if the background is not transparent it looks fine.

After some more deep investigation I found that one line can solve the problem:

Add a rendering hint just prior to the DrawString statement

g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;   <— This line solved it !
g.DrawString(text, font, Brushes.Black, new Point(x, 0));

Now everything is back to normal :-)

Comments

foobar said:

Interesting.

Can you provide a screenshot with the corrected text?
# May 6, 2006 10:31 PM

Ohad Israeli said:

The screen will look like my previous post over here : http://weblogs.asp.net/israelio/archive/2006/05/03/444931.aspx
# May 7, 2006 2:13 AM

Hans Meiser said:

Great! I had exactly the same problem and I didn'e even think of cleartypes. Funny was that on approximately half of the computers I tested it on it looked fine which might have been caused by the local font settings.

Thx A LOT!

# January 21, 2007 4:37 AM

Brian said:

I was getting some really ugly text in a dynamically generated system tray icon on cleartype systems. I had resorted to painting a background that matched the taskbar color, but that looked bad on systems where the taskbar is skinned or has the gradient from the default XP theme. This fixed it, except I ended up using AntiAliasGridFit instead. Thanks for posting this!

# May 1, 2007 5:53 PM

blaze said:

I've saved with your tip a lot of time - THANX!!!!

# November 8, 2007 6:35 AM

Fred said:

Very Good! But I used AntiAliasGrid as well. Thank you Ohad.

# December 18, 2007 1:11 PM

Christophe said:

Thanks!  You helped me out a ton as well :)

# March 19, 2008 1:00 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)