在执行 RotateTransform 时使用 bottom-sx 角作为轴心

Use bottom-sx corner as pivot while performing RotateTransform

我知道如何使用 top-sx 角作为轴旋转文本或矩形。例如:

Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
    Dim g As Graphics = Panel1.CreateGraphics()
    Dim font As Font = New Font("Arial", 42, FontStyle.Regular, GraphicsUnit.Pixel)
    Dim i As Single
    For i = 0 To 255 Step 30

        Dim myBrush As SolidBrush = New SolidBrush(Color.FromArgb(255, i, 255 - i, i)) 'Green to violet
        'Draw a string and a Rectangle of the same size
        Dim stringSize As SizeF = g.MeasureString("Hello", font)

        g.TranslateTransform(200, 200)
        g.RotateTransform(-i)
        g.DrawString("Hello", font, myBrush, 0, 0)

        g.DrawRectangle(New Pen(Color.FromArgb(50, 255, 0, 0), 1), 0, 0, stringSize.Width, stringSize.Height)
        g.ResetTransform()
        myBrush.Dispose()
    Next
    
    'Draw the center of the rotation
    g.DrawRectangle(Pens.Black, 200 - 5, 200 - 5, 10, 10)
    g.Dispose()
End Sub

使用这段代码我得到以下输出:

如何使用 bottom-sx 角作为轴旋转我的图形元素?

替换如下:

Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
    Dim g As Graphics = Panel1.CreateGraphics()
    Dim font As Font = New Font("Arial", 42, FontStyle.Regular, GraphicsUnit.Pixel)
    Dim i As Single
    For i = 0 To 255 Step 30

        Dim myBrush As SolidBrush = New SolidBrush(Color.FromArgb(255, CInt(i), CInt(255 - i), CInt(i))) 'Green to violet
        'Draw a string and a Rectangle of the same size
        Dim stringSize As SizeF = g.MeasureString("Hello", font)

        g.TranslateTransform(200, 200)
        g.RotateTransform(-i)

        Dim coorX As Single = 0
        Dim coorY As Single = -stringSize.Height

        g.DrawString("Hello", font, myBrush, coorX, coorY)
        g.DrawRectangle(New Pen(Color.FromArgb(50, 255, 0, 0), 1), coorX, coorY, stringSize.Width, stringSize.Height)

        g.ResetTransform()
        myBrush.Dispose()
    Next

    'Draw the center of the rotation
    g.DrawRectangle(Pens.Black, 200 - 5, 200 - 5, 10, 10)
    g.Dispose()
End Sub

这里是优化内存和代码友好的相同版本:

Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click

    Using g As Graphics = Panel1.CreateGraphics()

        Using font As Font = New Font("Arial", 42, FontStyle.Regular, GraphicsUnit.Pixel)

            'Draw a string and a Rectangle of the same size
            Dim stringSize As SizeF = g.MeasureString("Hello", font)
            Dim coorX As Single = 0
            Dim coorY As Single = -stringSize.Height

            For i As Integer = 0 To 255 Step 30
                Using myBrush As SolidBrush = New SolidBrush(Color.FromArgb(255, CInt(i), CInt(255 - i), CInt(i))) 'Green to violet
                    g.TranslateTransform(200, 200)
                    g.RotateTransform(-i)
                    g.DrawString("Hello", font, myBrush, coorX, coorY)
                    g.DrawRectangle(New Pen(Color.FromArgb(50, 255, 0, 0), 1), coorX, coorY, stringSize.Width, stringSize.Height)
                    g.ResetTransform()
                End Using
            Next

        End Using

        'Draw the center of the rotation
        g.DrawRectangle(Pens.Black, 200 - 5, 200 - 5, 10, 10)

    End Using

End Sub