18

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;
svick
  • 236,525
  • 50
  • 385
  • 514
Tim Cooper
  • 10,023
  • 5
  • 61
  • 77

5 Answers5

13

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:

To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
HitScan
  • 8,651
  • 3
  • 22
  • 17
  • This removes the horizontal padding, but not the vertical padding. Ugh. – BlueRaja - Danny Pflughoeft Oct 06 '19 at 11:30
  • I did all this tuning and it improved the situation, but anyway `MeasureString` returns a little bit smaller value for width. I have to add 0.3333f for every character in string to compensate this and get the correct width. With this it works almost perfectly. Any thoughts what have I missed so that I have to make this manual adjustment? It doesn't depend on the letters used - narrow or wide; just have to add this value for every character in the string. – Damir Tenishev Aug 27 '23 at 00:50
6

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
  • 22,772
  • 19
  • 94
  • 91
5

Append StringFormat.GenericTypographic will fix your issue:

graphics->MeasureString(L"PP", 1, font, width, StringFormat.GenericTypographic);

Apply the same attribute to DrawString.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
vscpp
  • 131
  • 2
  • 2
1

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
  • 26,279
  • 16
  • 79
  • 95
0

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..