Kees C. Bakker
Kees C. Bakker

Reputation: 33381

Get System.Drawing.Font width?

I'm using .Net tools to do some 2D drawing. System.Drawing.Font uses a GetHeight() that returns the height in pixels. I'm missing a GetWidth() to retrieve the width! What should I use?

Upvotes: 5

Views: 13063

Answers (2)

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Use Graphics.MeasureString Method (String, Font):

Eg.

   // Set up string. string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);
    // Measure string.
    SizeF stringSize = new SizeF();
    Graphics gfx = Graphics.FromImage(new Bitmap(1, 1));
    stringSize = gfx.MeasureString(measureString, stringFont);
    // This will give you string width, from which you can calculate further 
    double width = stringSize.Width

Upvotes: 10

Rawling
Rawling

Reputation: 50114

What width? GetHeight returns the distance between the baselines of two lines of text, which is a property of the font itself. But the width depends on what you're going to write.

If you know what it is you want to write, try the Graphics.MeasureString methods.

Upvotes: 4

Related Questions