Saturday, February 7, 2009

height and width of text with specific font

When you draw a string on a C# form if you want to figure out how many pixel wide and high the text will be use:

TextRenderer Class

To measure the size you may use

TextRenderer.MeasureText :

TextRenderer.MeasureText (String, Font, Size)
TextRenderer.MeasureText (IDeviceContext, String, Font, Size)
TextRenderer.MeasureText (String, Font, Size, TextFormatFlags)
TextRenderer.MeasureText (IDeviceContext, String, Font, Size, TextFormatFlags)

It returns back System.Drawing.Size which has height and width of the text you wanted.


To draw text you may use:
private void RenderText1(PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
new Point(10, 10), SystemColors.ControlText);
}


for more details you may visit:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer(VS.80).aspx

No comments: