Solved

Any way to determine the width of text when it is drawn?  ASP.NET VB

Posted on 2013-02-03
3
292 Views
Last Modified: 2013-02-03
Here is my line of code which draws text to the screen:

Dim TopLine1 as string = "Hello World"

graphicImage.DrawString(TopLine1, New Font("Helvetica, sans-serif", 18, FontStyle.Regular), SystemBrushes.WindowText, New Point(GetLeftMargin(TopLine1), 20))


I need to create the function GetLeftMargin which will center the text within my area, which is 440 pixels wide.   Since most fonts do not have a "fixed" width for each character, I can't simply take the number of characters and multiply by a fixed pixel width.

My hope is to pre-calculate the width of the string I am drawing, and then use that to calculate my X margin.  If I knew how wide my text image was, then I could quickly calculate the correct margin for it.   How can I pre-calculate how wide my text image is going to be?
0
Comment
Question by:arthurh88
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
MajorBigDeal earned 500 total points
ID: 38849138
0
 

Author Comment

by:arthurh88
ID: 38849149
thanks a bunch!  this worked too, i discovered right as you answered:

 Dim Tlength As Integer = graphicImage.MeasureString(String.Format("{0:C2}", TopLine1), New Font("Helvetica, sans-serif", 18, FontStyle.Regular)).Width
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 38849514
Cool, thanks for sharing your solution.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question