[GDI+] how to know the height of a text line

  • Thread starter Thread starter Zanna
  • Start date Start date
Z

Zanna

Hi all!

I'm in difficulty with this: I need to know the height of a text line that
is written with Graphics.DrawString().

In theory I need just to get the Graphics.MeasureString() result.
But this works if the string is on a single line.

If the string goes on more lines (caused by a new line or word wrap), it
seems that I need to compute the space between the lines, since the height
of two lines does not correspond with the double of the height of a single
line.

Someone know how to compute the exact height of n lines of text?

Thanks.
 
MeasureString should still work if the line has line-breaks in it. To get
the best measurement possible ensure that the Graphics device is using an
antialiased text rendering hint and that you use a high quality string
format (GenericTypographic).

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
MeasureString should still work if the line has line-breaks in it. To get
the best measurement possible ensure that the Graphics device is using an
antialiased text rendering hint and that you use a high quality string
format (GenericTypographic).

No, my mistake.
It is a little more complex.
I need to "break" into more blocks of lines a text printed by DrawString().

For example, if I have the text

"
First Line\n
Second Line\n
This line goes on another line
due to word wrap on bounding rect
"

I need to break it, let's say at the second line, so I can have two separate
blocks to print:

"
First Line\n
Second Line\n
"

And

"
This line goes on another line
due to word wrap on bounding rect
"

But I cannot count the number of lines on the newline, because maybe I
havn't a \n, but I have a word wrap.

Some ideas?

Thanks.
 
Back
Top