【问题标题】:Use bottom-sx corner as pivot while performing RotateTransform在执行 RotateTransform 时使用底部 sx 角作为枢轴
【发布时间】:2020-08-08 15:56:48
【问题描述】:

我知道如何使用 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

使用此代码,我有以下输出:

如何使用底部 sx 角作为枢轴旋转我的图形元素?

【问题讨论】:

    标签: vb.net graphics gdi+ rotatetransform


    【解决方案1】:

    如下替换:

    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
    

    【讨论】:

    • 非常感谢!不用担心内存使用...这只是一个演示代码!
    • @Calaf 是的,我想是的。更多的是为未来的用户展示正确的代码结构。 :)。不客气!
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多