Solved

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

Posted on 2013-02-03
3
286 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
Comment Utility
0
 

Author Comment

by:arthurh88
Comment Utility
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
Comment Utility
Cool, thanks for sharing your solution.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now