Any way to determine the width of text when it is drawn? ASP.NET VB
Posted on 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?