textmetrix in c#

jadhaol used Ask the Experts™
How to get TEXTMETRIX in C#, I mean how to get width and height of a string for a given font? In C++, we get the same using TEXTMETRIX. What is the equivalent for it in C#? code sample please.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Use Graphics.MeasureString()

I am working on some example code...

1. Create an empty form.

2. Subscribe to the form's Paint event.

this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

3. Add the following code to the Paint event handler.

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
     // Create a test string...
     string testString = "This is a test string";

     // Measure the size of the string (using the font)...
     SizeF stringSize = e.Graphics.MeasureString(testString, this.Font);

     e.Graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, (int)stringSize.Width, (int)stringSize.Height);

     e.Graphics.DrawString(testString, Font, new SolidBrush(Color.Black), 0, 0);

4. The result will be some black text drawn over a white rectangle, which represents the measured size of the text.


Thanks Jazzyfoot,


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial