Pregunta

I guess I don't know what search terms to use, this should be simple... I what to determine the width in inches of a string.

        Dim sSize As System.Drawing.SizeF
        Dim fFont As New Font("Arial", 12)
        sSize = Me.CreateGraphics().MeasureString(txtAddr.Text, fFont)

The units here are whatever the IDE is using. I'm not sure that is a constant value, if it is the conversion should be straightforward.

Any way, I want to convert the returned units to inches when the text is printed at 100% using the specified font. How do I do that?


Thanks Hans!

        Dim boxGraphics As Graphics = txtAddr.CreateGraphics()
        Dim sSize As System.Drawing.SizeF = boxGraphics.MeasureString(txtAddr.Text, New Font("Arial", 12))
        Dim iInches As Single = sSize.Width / boxGraphics.DpiX

Beginnings of DYMO LabelWriter 450 code for those that might be interested:

        ' project ref to DYMO.Label.Framework .net 3.5/4
        ' Imports DYMO.Label.Framework
        Dim olabel As Label
        olabel = Label.Open("H:\INFO\Forms\ADMIN\Dymo Labels\APCDAddress.label")
        olabel.SetObjectText("ADDRESS", txtAddr.Text) ' case sensitive
        olabel.Print("\\PC3090-117\DYMO LabelWriter 450 Twin Turbo")
        MsgBox("Printed")

I'll be adding code to deal with address text that maybe too large for the label, hence the OP. The DYMO layouts can be set to shrink to fit the text but that has practical limits when making address labels.

¿Fue útil?

Solución

From @HansPassant: Divide by Graphics.DpiX

   Dim boxGraphics As Graphics = txtAddr.CreateGraphics()
    Dim sSize As System.Drawing.SizeF = boxGraphics.MeasureString(txtAddr.Text, New Font("Arial", 12))
    Dim iInches As Single = sSize.Width / boxGraphics.DpiX
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top