Results 1 to 4 of 4

Thread: Width of a string

  1. #1

    Thread Starter
    Registered User jackgreenfox's Avatar
    Join Date
    Oct 2002
    Location
    China
    Posts
    35

    Width of a string

    How can I measure the width of the string which I draw on a Graphics object?

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This way .
    Code:
    protected override void OnPaint(PaintEventArgs e)
    		{			
    			 SizeF stringSize=new SizeF();
    			SizeF=e.Graphics.MeasureString("Your text here",stringSize,Myfont);
    
    		}

  3. #3
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Pirate
    This way .
    Code:
    protected override void OnPaint(PaintEventArgs e)
    		{			
    			 SizeF stringSize=new SizeF();
    			SizeF=e.Graphics.MeasureString("Your text here",stringSize,Myfont);
    
    		}
    I dont know if it would be helpful but: just remember that SizeF is in decimal format, so if you are rounding it or converting it to integer I would suggest using Math.Ceiling. I was using this in VB and was a few pixels short depending on the system font size and couldnt figure it out, until I found out that it was a rounding problem
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Remember
    Code:
    StringAlignment
    can affect the result taken by MeasureString method .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width