Solved

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

Posted on 2013-02-03
3
289 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 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

18 Experts available now in Live!

Get 1:1 Help Now