Text Width/Height problem.

  • Thread starter Thread starter mehdi_mousavi
  • Start date Start date
M

mehdi_mousavi

Hi folks,
Consider a text containing 200 characters (more or less) including
and/or excluding the CR/LF within. I would like to compute the height
of this text whenever it's width is limited by X pixels! i.e., what's
the height of the mentioned text when it's being automatically word
wrapped within a rectangle of WIDTH pixels? How am I supposed to
calculate the height required to show the message?

TIA,
Mehdi
 
Hello mehdi_mousavi,

I'd recomend you to ask this in microsoft.public.dotnet.framework.drawing
group
There were several problems, as i remember, with getting font/string height
and spaces between strings because standard font measuring based on the em
box, and some font glyphs are drawned ouside this embox

See there http://groups.google.com/group/micr...tnet.framework.drawing&q=string+height&qt_g=1

m> Hi folks,
m> Consider a text containing 200 characters (more or less) including
m> and/or excluding the CR/LF within. I would like to compute the height
m> of this text whenever it's width is limited by X pixels! i.e., what's
m> the height of the mentioned text when it's being automatically word
m> wrapped within a rectangle of WIDTH pixels? How am I supposed to
m> calculate the height required to show the message?
m> TIA,
m> Mehdi
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
I think you're looking for this :
this.CreateGraphics().MeasureString(string text, Font font, int width);
It has 7 overloads which may be of use, too.
 
Hi Mehdi,

Assuming that you are writing a WinForms app using the .NET 2.0 framework you can use the TextRenderer class to measure strings in
different formats and with different options.

TextRenderer.MeasureText(...)

In the 1.* frameworks you can measure strings using a method on the Graphics class provided by the Paint method of the target
Control.

Graphics.MeasureString(...)
 
Hi Dave,

I have been using TextRenderer.MeasureText but the width and height returned
are rounded down. This could lead to missing character on display. I used
g.MeasureString instead as it uses Float instead of integer. Is there
specific code to use to have MeasureText returning the right value ?

Regards,
Sylvain
 
Hi Sylvain,

Increase the size of the rectangle being measured. Maybe add a bit of padding to the right edge.

If you get better results from measuring strings with the Graphics object then I suggest you use that method.

The difference is that TextRenderer uses GDI and Graphics uses GDI+.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top