Get Text Height & Width


In many cases we tend to work with dynamic data transaction & data formatting, then we maty need to calculate the width, height & align accordingly w.r.t specific font details etc.,,,,,

I got in to same scenarios, where i need to brush the text on a image & i was breaking my head how to calculated the starting point w.r.t text size, font  etc.,,,,,,,,,,
after a long of R & D ; got something which saved me i.e.,
Graphics class (which is a dotnet inbuilt class)

Usage :

Creating a instance
Graphics grpControl = CreateGraphics();

Creating a dummy string value
String minstr =”Minimumvalue”;

Font details I will be working with

Font textFont = new Font("Arial", 16, GraphicsUnit.Pixel);

Now the destination of calculation, graphics Class has a method named MeasureString this is return value of type  SizeF

SizeF: It stores the floated points
SizeF sizeDetails = grpControl.MeasureString(minStr,textFont);

Now the return value provide you the Height & width as shown below.
sizeDetails.Height // For height
sizeDetails.Width  // For Width

I have seen so many ways to find the string height but this is a best way to find height according to the text font.

Happy Coding :) :)

Comments

Popular posts from this blog

Maximize child MDI form without ControlBox

Adding text to label using javascript