String Size

  • Thread starter Thread starter Rodrigo Ferreira
  • Start date Start date
R

Rodrigo Ferreira

I want to calculate the size of a string in pixels, so i can use the
DrawString Method into the form to insert the string in the correct place.

Anyone knows how to do that?

Greetings

Rodrigo Ferreira
 
Hi Rodrigo
From a graphics object, call the MeasureString method. A common way to
get a graphics object is through the form's paint event. Here is an
example:

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Graphics myGraphics = e.Graphics;
Font myFont = new Font("Arial",12);
SizeF mySize = myGraphics.MeasureString("Example Text",myFont);
MessageBox.Show("Font Height is " + mySize.Height + ", Font Width is
" + mySize.Width);
}

There are 7 overloads for measure string, so you should be able to find
what u are after.

Cheers
Bill
 
Back
Top