troubleshooting Question

how do I get the correct font width for print formatting vb.net

Avatar of fwlrichard
fwlrichard asked on
.NET Programming
7 Comments1 Solution448 ViewsLast Modified:
Hi,
I have two lines both are the same other than the font color. The second line prints with extra spaces between the letters. I need a way  to get the correct font width for calculating the start/x position for printing. The attached code uses MeasureString and prints wrong.

Thanks
Richard
Dim pic_font As New Font("Lucida Console", 10, FontStyle.Regular, GraphicsUnit.Pixel)
        Dim c As Long
        Dim ps As String
        Dim h As Integer
        Dim w As Double

        w = 0
        h = 0

        e.Graphics.DrawString("Rich", pic_font, Brushes.Black, w, 10)

        ps = "R"
        e.Graphics.DrawString("R", pic_font, Brushes.Black, w, 20)
        c = e.Graphics.MeasureString(ps, pic_font).Width
        w = w + c

        ps = ps + "i"
        e.Graphics.DrawString("i", pic_font, Brushes.Red, w, 20)
        c = e.Graphics.MeasureString(ps, pic_font).Width
        w = w + c

        ps = ps + "c"
        e.Graphics.DrawString("c", pic_font, Brushes.Blue, w, 20)
        c = e.Graphics.MeasureString(ps, pic_font).Width
        w = w + c

        ps = ps + "h"
        e.Graphics.DrawString("h", pic_font, Brushes.Green, w, 20)

        e.HasMorePages = False
Bad-Output.bmp
Good-Output.bmp
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros