Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Measuring a string drawn with a specific font

Total Hit ( 2629)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


When you're dynamically drawing text (either on a Windows Form or Web Form application), you may need to calculate the width and height of a string printed with a particular font. This is easy with the Graphics class's Measure string method, that takes in input the string being measured, and the font it will be printed with. Follows an example:

Click here to copy the following block
' create a new bitmap
Dim bmp As New Bitmap(400, 200, Drawing.Imaging.PixelFormat.Format16bppRgb565)
' get the underlying Graphics object
Dim gr As Graphics = Graphics.FromImage(bmp)
' measure the string
Dim stringSize As SizeF = gr.MeasureString("Test string", _
  New Font("Courier New", 18, FontStyle.Bold, GraphicsUnit.Pixel))

Finally you can read the Height and Width properties of the returned SizeF variable (stringSize) in the example above.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.