?
Solved

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

Posted on 2013-02-03
3
Medium Priority
?
312 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 2000 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

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

840 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