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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With