469,644 Members | 1,749 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

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

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.
Nov 17 '05 #1
2 5062
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.

"Zanna" <zn*******@virgilio.it> wrote in message
news:uG**************@TK2MSFTNGP11.phx.gbl...
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.

Nov 17 '05 #2
> 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.
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Pohihihi | last post: by
2 posts views Thread by Alphonse Giambrone | last post: by
2 posts views Thread by user | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.