Text blurring using Graphics.Drawstring
-
I'm writing strings at a pre-determined location to a bitmap. Though, it seems that if the location isn't "snapped to the grid", blurring occurs. Simple example:
public Bitmap WriteToBitmap(Bitmap bitmapToWriteTo, float X\_Pos, float Y\_Pos) { Graphics g = Graphics.FromImage(bitmapToWriteTo); g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = TextRenderingHint.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.DrawString(this.text, this.font, new SolidBrush(Color.Black), X\_Pos, Y\_Pos, this.stringFormat); return bitmapToWriteTo; }
Nothing else is being done to the bitmap object or any of the other objects involved. So you may consider the code above as being used as is. The problem is, however, that using it like this, causes blurring in the text when drawn. There is a solution to stop the blurring and that is by changing the TextRenderingHint:
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
Now, this causes the text being drawn to be "snapped to a grid". However, this messes up my positioning, making it look chaotic. My question is this, how can I get the result of TextRenderingHint.AntiAliasGridFit, without losing the positioning. And, to what "grid" is the text being snapped to and am I able to manipulate it ?
-
I'm writing strings at a pre-determined location to a bitmap. Though, it seems that if the location isn't "snapped to the grid", blurring occurs. Simple example:
public Bitmap WriteToBitmap(Bitmap bitmapToWriteTo, float X\_Pos, float Y\_Pos) { Graphics g = Graphics.FromImage(bitmapToWriteTo); g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = TextRenderingHint.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.DrawString(this.text, this.font, new SolidBrush(Color.Black), X\_Pos, Y\_Pos, this.stringFormat); return bitmapToWriteTo; }
Nothing else is being done to the bitmap object or any of the other objects involved. So you may consider the code above as being used as is. The problem is, however, that using it like this, causes blurring in the text when drawn. There is a solution to stop the blurring and that is by changing the TextRenderingHint:
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
Now, this causes the text being drawn to be "snapped to a grid". However, this messes up my positioning, making it look chaotic. My question is this, how can I get the result of TextRenderingHint.AntiAliasGridFit, without losing the positioning. And, to what "grid" is the text being snapped to and am I able to manipulate it ?
Here's the solution as I can tell it at this point. Dropping the following code solved the bluring for me:
g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = TextRenderingHint.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBilinear;
From what I understood by reading Microsoft supplied information and the experiences from other developers, it's the Glyph hinting that causes the problem. I increased the DPI of the originating Bitmap and dropped the hinting. Problem solved. Some positioning adjustments are required, but nothing I can't fix. Hope this will help others as well.
-
Here's the solution as I can tell it at this point. Dropping the following code solved the bluring for me:
g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = TextRenderingHint.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBilinear;
From what I understood by reading Microsoft supplied information and the experiences from other developers, it's the Glyph hinting that causes the problem. I increased the DPI of the originating Bitmap and dropped the hinting. Problem solved. Some positioning adjustments are required, but nothing I can't fix. Hope this will help others as well.
Sorry, its not to the problem itself, but I'm wondering. You say "it's the Glyph hinting that causes the problem". But on MSDN it says that TextRenderingHintAntiAlias does no hinting. From MSDN: "TextRenderingHintAntiAlias: Specifies that a character is drawn using its antialiased glyph bitmap and no hinting. Stem width differences may be noticeable because hinting is turned off. "
-
Sorry, its not to the problem itself, but I'm wondering. You say "it's the Glyph hinting that causes the problem". But on MSDN it says that TextRenderingHintAntiAlias does no hinting. From MSDN: "TextRenderingHintAntiAlias: Specifies that a character is drawn using its antialiased glyph bitmap and no hinting. Stem width differences may be noticeable because hinting is turned off. "
True, that's what they say. I suggest you try it out and see if there's Glyph manipulation. Set the TextRenderingHint property of your Graphics object:
g.TextRenderingHint = TextRenderingHint.AntiAlias;
See how it's displayed. Then remove it and compare the two. Even though it's already visibly apparent, I would like to urge you to print the two results and compare them. In my humble opinion, the AntiAlias touches the Glyph. But I could be mistaken. If I am mistaken and somehow I seem to have missed what the default does as opposed to what AntiAlias does, then please inform me. Don't forget to add an informative link, not just for myself, but for others coming across this problem. Thanks in advance. Edit: I indeed stand corrected. Here's a link explaining how it works: http://msdn.microsoft.com/en-us/library/ms534404%28VS.85%29.aspx By setting the TextRenderingHint to AntiAlias, you kill the default corrective Glyph hinting the Graphics object does by itself. Quote: "TextRenderingHintSystemDefault Specifies that a character is drawn using the currently selected system font smoothing mode (also called a rendering hint)." Quote: "TextRenderingHintAntiAlias Specifies that a character is drawn using its antialiased glyph bitmap and no hinting. Stem width differences may be noticeable because hinting is turned off." Thank you Covean for this insight
modified on Thursday, October 22, 2009 9:07 AM
-
True, that's what they say. I suggest you try it out and see if there's Glyph manipulation. Set the TextRenderingHint property of your Graphics object:
g.TextRenderingHint = TextRenderingHint.AntiAlias;
See how it's displayed. Then remove it and compare the two. Even though it's already visibly apparent, I would like to urge you to print the two results and compare them. In my humble opinion, the AntiAlias touches the Glyph. But I could be mistaken. If I am mistaken and somehow I seem to have missed what the default does as opposed to what AntiAlias does, then please inform me. Don't forget to add an informative link, not just for myself, but for others coming across this problem. Thanks in advance. Edit: I indeed stand corrected. Here's a link explaining how it works: http://msdn.microsoft.com/en-us/library/ms534404%28VS.85%29.aspx By setting the TextRenderingHint to AntiAlias, you kill the default corrective Glyph hinting the Graphics object does by itself. Quote: "TextRenderingHintSystemDefault Specifies that a character is drawn using the currently selected system font smoothing mode (also called a rendering hint)." Quote: "TextRenderingHintAntiAlias Specifies that a character is drawn using its antialiased glyph bitmap and no hinting. Stem width differences may be noticeable because hinting is turned off." Thank you Covean for this insight
modified on Thursday, October 22, 2009 9:07 AM
-
I can see the difference. And yes I also think it touches the Glyph, but there so no hinting. The Problem with the "no hinting" is "Stem width differences may be noticeable because hinting is turned off." Edit: Oh I read your edit too late.
Not a problem, your thoughts in this have been insightful. ;)
-
Here's the solution as I can tell it at this point. Dropping the following code solved the bluring for me:
g.SmoothingMode = SmoothingMode.AntiAlias; g.TextRenderingHint = TextRenderingHint.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBilinear;
From what I understood by reading Microsoft supplied information and the experiences from other developers, it's the Glyph hinting that causes the problem. I increased the DPI of the originating Bitmap and dropped the hinting. Problem solved. Some positioning adjustments are required, but nothing I can't fix. Hope this will help others as well.
The plot thickens... I was somewhat bored and still not satisfied with how the
Graphics.DrawString()
was working for me. So I went on a Google hunt for some answers. After looking around I came to a forum where a user had somewhat similar problems. There he had linked a convo he had with a Microsoft MVP. For more information please check this link: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/362ab21b-1dc4-4140-a39a-a366beea9e40[^] Quote: "They couldn't fix the bugz in Graphics.DrawString() because way too much .NET 1.x code depended on its quirks. That's what Application.SetCompatibleTextRenderingDefault() is all about too. If you really want to use GDI+, you'll have to find the workarounds for the bugs yourself. Right now, using Graphics.DrawString() in a Paint event is a bug." Quote: "Graphics.DrawString() is too broken to be usable. It got replaced by TextRenderer.DrawText()." To simplify things: If you care for precision work, in particular with text rendering, then use the old GDI. GDI+ is bugged and unusable for precision work. If you don't care for precision work and you don't use text rendering (or very little of it or the quality isn't important), then use the new GDI+. -
The plot thickens... I was somewhat bored and still not satisfied with how the
Graphics.DrawString()
was working for me. So I went on a Google hunt for some answers. After looking around I came to a forum where a user had somewhat similar problems. There he had linked a convo he had with a Microsoft MVP. For more information please check this link: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/362ab21b-1dc4-4140-a39a-a366beea9e40[^] Quote: "They couldn't fix the bugz in Graphics.DrawString() because way too much .NET 1.x code depended on its quirks. That's what Application.SetCompatibleTextRenderingDefault() is all about too. If you really want to use GDI+, you'll have to find the workarounds for the bugs yourself. Right now, using Graphics.DrawString() in a Paint event is a bug." Quote: "Graphics.DrawString() is too broken to be usable. It got replaced by TextRenderer.DrawText()." To simplify things: If you care for precision work, in particular with text rendering, then use the old GDI. GDI+ is bugged and unusable for precision work. If you don't care for precision work and you don't use text rendering (or very little of it or the quality isn't important), then use the new GDI+.