Click here to Skip to main content
Click here to Skip to main content

Height of Text

By , 23 Jun 2004
 

Introduction

This is a handy little function for getting the height of text. 

Overview

I have read many articles and asked lots of questions before I found the functions in the dot net library that would help me to achieve this task.

This is also my very first Code Project article. This site has helped me solve MANY problems and I hope that this piece of code helps someone out.

The Code

<BR>  /// <summary><BR>  /// This method will return the height of the string passed in.<BR>  /// </summary><BR>  /// <param name="strMeasureString">The string to measure</param><BR>  /// <param name="stringFont">The font to measure by</param><BR>  /// <param name="nWidth">The max width the string can be [Width of the text box usually ;)]</param><BR>  /// <param name="hwnd">Handle to your form so I can get the graphics object</param>  <BR>  /// <returns>The height of the string as a int</returns> <FONT color=#0000ff size=2><P> </P><P>public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> GetStringHeight(</FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> strMeasureString, System.Drawing.Font stringFont, </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> nWidth,System.IntPtr hwnd) </P><P>{</P><P></FONT><FONT color=#0000ff size=2>	using</FONT><FONT size=2> (System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(hwnd))</P><P>	{</P><P></FONT><FONT color=#0000ff size=2>		return</FONT><FONT size=2> (</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>)g.MeasureString(strMeasureString, stringFont, nWidth).Height; </P><P>	}</P><P>}</P></FONT>

(Thanks for the advice Micheal)

Its pretty straightforward and it creates some pretty cool effects if you use it with the 'Text Changed' event of a text box. Tie that to the 'Resize' event and you can have a form the grows and shrinks as the user is typing in text.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ACanadian
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalo dearmemberleppie24 Jun '04 - 7:55 
Ever looked at Font.Height?
Ever looked at Font.GetHeight()?
Ever looked at FontFamily?
D'Oh! | :doh: Sigh | :sigh:
 

 
top secret xacc-ide 0.0.1
GeneralRe: o dearmemberACanadian24 Jun '04 - 8:17 

huh, I've never looked at those... If they can... D'Oh! | :doh:
GeneralRe: o dearmemberlshzhou5 Jul '04 - 15:56 
o,if you use the function MeasureString of Graphics to get text height,you'll get a whole lineheight,this value includes the vertical space.when you set LineAlignment of StringFormat to StringAlignment.center,you'll find the text is at the exact middle,the space can be got in the floow way:
SizeF TextSize=g.MeasureString(str,font);
float space=TextSize.Height-font.size;
GeneralRe: o dearmemberleppie5 Jul '04 - 16:40 
or you can use font.Height - font,GetHeight()
 
top secret xacc-ide 0.0.1
GeneralRe: o dearmemberlshzhou5 Jul '04 - 15:59 
Smile | :) o,if you use the function MeasureString of Graphics to get text height,you'll get a whole lineheight,this value includes the vertical space.when you set LineAlignment of StringFormat to StringAlignment.center,you'll find the text is not at the exact middle,the space can be got in the follow way:
SizeF TextSize=g.MeasureString(str,font);
float space=TextSize.Height-font.size;
GeneralA couple of thoughtsmemberMichael Potter24 Jun '04 - 7:39 
1) If you create a Graphics object via FromHwnd(), you had better dispose of it before exiting the method. It is a system resource that must be returned.
 
2) No need to create a new SizeF if you are going to replace it in the next few lines;
 
Ignoring errors it might look something like this:
public int GetStringHeight(string strMeasureString, System.Drawing.Font stringFont, int nWidth,System.IntPtr hwnd)
{
     using (Graphics g = Graphics.FromHwnd(hwnd))
     {
          return (int)g.MeasureString(strMeasureString, stringFont, nWidth).Height;          
     }
}

GeneralRe: A couple of thoughtsmemberACanadian24 Jun '04 - 8:16 
Thanks for the little re-write.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 24 Jun 2004
Article Copyright 2004 by ACanadian
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid