0

我想将文本添加到图像。我制作了一个文本框来输入一些文本,然后这些文本将被绘制到图形对象。但是字体大小不正确。如果通过放大/缩小来缩放 Drawing.Graphics。所以我尝试使用 MeasureString() 函数来正确获取字体大小。

 Private Function GetCorrectFont(ByVal text As String, ByVal f As Font) As Integer
    Dim g As Graphics = _drawArea.CreateGraphics
    Dim widthBefore As Integer = g.MeasureString(text, f).Width
    Dim mx As Matrix = New Matrix()
    mx.Scale(2, 2, MatrixOrder.Append)
    g.Transform = mx
    Dim widthAfter As Integer = g.MeasureString(text, f).Width
    Dim fontSize As Integer = f.Size
    Do While widthAfter < widthBefore
        Dim newFont As New Font(f.FontFamily, fontSize)
        widthAfter = g.MeasureString(text, newFont).Width
        fontSize += 1
    Loop
    Return fontSize
End Function

但是widthBeforewidthAfter是相同的值。为什么?以及如何使字体大小与文本框中的显示相同?

4

0 回答 0