views:

2050

answers:

5

I'm using GDI+ in C++. (This issue might exist in C# too).

I notice that whenever I call Graphics::MeasureString() or Graphics::DrawString(), the string is padded with blank space on the left and right.

For example, if I am using a Courier font, (not italic!) and I measure "P" I get 90, but "PP" gives me 150. I would expect a monospace font to give exactly double the width for "PP".

My question is: is this intended or documented behaviour, and how do I disable this?

RectF Rect(0,0,32767,32767);
RectF Bounds1, Bounds2;
graphics->MeasureString(L"PP", 1, font, Rect, &Bounds1);
graphics->MeasureString(L"PP", 2, font, Rect, &Bounds2);
margin = Bounds1.Width * 2 - Bounds2.Width;
+1  A: 

It's by design, that method doesn't use the actual glyphs to measure the width and so adds a little padding in the case of overhangs.

MSDN suggests using a different method if you need more accuracy. See that link for more details.

HitScan
+1  A: 

Sounds like it might also be connecting to hinting, based on this kb article, Why text appears different when drawn with GDIPlus versus GDI

Factor Mystic
+1  A: 

It's true that is by design, however the link on the accepted answer is actually not perfect. The issue is the use of floats in all those methods when what you really want to be using is pixels (ints).

The TextRenderer class is meant for this purpose and works with the true sizes. See this link from msdn for a walkthrough of using this.

Cory
A: 

TextRenderer was great for getting the size of the font. But in the drawing loop, using TextRenderer.DrawText was excruciatingly slow compared to graphics.DrawString().

Since the width of a string is the problem, your much better off using a combination of TextRenderer.MeasureText and graphics.DrawString..

+1  A: 

TextRenderer is in the System.Windows namespace. I want something in the System or Web namespace. I think System.Drawing's MeasureString will do just fine.

Rupak Ganguly