VB.NET 围绕中心旋转图形对象
VB.NET Rotate graphics object around center
如果这是一个愚蠢的问题,请提前致歉。
我有一个图形对象来处理我在带有背景的面板上绘制的图像。我想要做的是让图像围绕其中心旋转。
这是我目前拥有的代码:
全局声明:
Dim myBitmap As New Bitmap("C:\Users\restofthefilepath")
Dim g As Graphics
Form1_Load:
g = Panel1.CreateGraphics
Timer1_tick(设置为1s间隔):
Panel1.Refresh()
g.DrawImage(myBitmap, -60, 110)
g.RenderingOrigin = New Point(160, 68)
g.RotateTransform(10)
我明白了:左边是第一个刻度之后,右边是第二个刻度之后。
(占位符图形)
如您所见,我正在设置 RenderingOrigin(如 this answer): , but the rotation is still around 0,0. I've tried implementing RotateTransform(10, 160, 68) (with the centre of rotation specified) as this documentation 中所建议的那样应该可行,但我收到构建错误 "Overload resolution failed because no accessible 'RotateTransform' accepts this number of arguments"。
我哪里出错了,如何让图像围绕其中心旋转?
我开始了一个新的 VB.NET Windows Forms 项目。我添加了一个 200px x 200px 的面板和一个按钮来根据需要暂停动画。我给了 Panel1 一张背景图片:
制作了一张有点像你的图片:
并使用了以下代码:
Public Class Form1
Dim wiggle As Bitmap
Dim tim As Timer
Sub MoveWiggle(sender As Object, e As EventArgs)
Static rot As Integer = 0
Panel1.Refresh()
Using g = Panel1.CreateGraphics()
Using fnt As New Font("Consolas", 12), brsh As New SolidBrush(Color.Red)
' the text will not be rotated or translated
g.DrawString($"{rot}°", fnt, brsh, New Point(10, 10))
End Using
' the image will be rotated and translated
g.TranslateTransform(100, 100)
g.RotateTransform(CSng(rot))
g.DrawImage(wiggle, -80, 0)
End Using
rot = (rot + 10) Mod 360
End Sub
Private Sub bnPause_Click(sender As Object, e As EventArgs) Handles bnPause.Click
Static isPaused As Boolean = False
If isPaused Then
tim.Start()
bnPause.Text = "Pause"
Else
tim.Stop()
bnPause.Text = "Start"
End If
isPaused = Not isPaused
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
wiggle = New Bitmap("C:\temp\path3494.png")
wiggle.SetResolution(96, 96) ' my image had a strange resolution
tim = New Timer With {.Interval = 50}
AddHandler tim.Tick, AddressOf MoveWiggle
tim.Start()
End Sub
Private Sub Form1_Closing(sender As Object, e As EventArgs) Handles MyBase.Closing
RemoveHandler tim.Tick, AddressOf MoveWiggle
tim.Dispose()
wiggle.Dispose()
End Sub
End Class
并实现了这一目标:
注意 1:以正确的顺序设置转换很重要。
注2:我在MyBase.Closing
事件中对可支配资源调用了.Dispose()
。这可以确保内存保持干净并且没有任何泄漏。
毫无疑问,有更好的方法来创建动画,但以您想要的每秒一帧的速度,这可以达到您想要的效果。
如果这是一个愚蠢的问题,请提前致歉。
我有一个图形对象来处理我在带有背景的面板上绘制的图像。我想要做的是让图像围绕其中心旋转。 这是我目前拥有的代码:
全局声明:
Dim myBitmap As New Bitmap("C:\Users\restofthefilepath")
Dim g As Graphics
Form1_Load:
g = Panel1.CreateGraphics
Timer1_tick(设置为1s间隔):
Panel1.Refresh()
g.DrawImage(myBitmap, -60, 110)
g.RenderingOrigin = New Point(160, 68)
g.RotateTransform(10)
我明白了:左边是第一个刻度之后,右边是第二个刻度之后。
(占位符图形)
如您所见,我正在设置 RenderingOrigin(如 this answer): , but the rotation is still around 0,0. I've tried implementing RotateTransform(10, 160, 68) (with the centre of rotation specified) as this documentation 中所建议的那样应该可行,但我收到构建错误 "Overload resolution failed because no accessible 'RotateTransform' accepts this number of arguments"。
我哪里出错了,如何让图像围绕其中心旋转?
我开始了一个新的 VB.NET Windows Forms 项目。我添加了一个 200px x 200px 的面板和一个按钮来根据需要暂停动画。我给了 Panel1 一张背景图片:
制作了一张有点像你的图片:
并使用了以下代码:
Public Class Form1
Dim wiggle As Bitmap
Dim tim As Timer
Sub MoveWiggle(sender As Object, e As EventArgs)
Static rot As Integer = 0
Panel1.Refresh()
Using g = Panel1.CreateGraphics()
Using fnt As New Font("Consolas", 12), brsh As New SolidBrush(Color.Red)
' the text will not be rotated or translated
g.DrawString($"{rot}°", fnt, brsh, New Point(10, 10))
End Using
' the image will be rotated and translated
g.TranslateTransform(100, 100)
g.RotateTransform(CSng(rot))
g.DrawImage(wiggle, -80, 0)
End Using
rot = (rot + 10) Mod 360
End Sub
Private Sub bnPause_Click(sender As Object, e As EventArgs) Handles bnPause.Click
Static isPaused As Boolean = False
If isPaused Then
tim.Start()
bnPause.Text = "Pause"
Else
tim.Stop()
bnPause.Text = "Start"
End If
isPaused = Not isPaused
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
wiggle = New Bitmap("C:\temp\path3494.png")
wiggle.SetResolution(96, 96) ' my image had a strange resolution
tim = New Timer With {.Interval = 50}
AddHandler tim.Tick, AddressOf MoveWiggle
tim.Start()
End Sub
Private Sub Form1_Closing(sender As Object, e As EventArgs) Handles MyBase.Closing
RemoveHandler tim.Tick, AddressOf MoveWiggle
tim.Dispose()
wiggle.Dispose()
End Sub
End Class
并实现了这一目标:
注意 1:以正确的顺序设置转换很重要。
注2:我在MyBase.Closing
事件中对可支配资源调用了.Dispose()
。这可以确保内存保持干净并且没有任何泄漏。
毫无疑问,有更好的方法来创建动画,但以您想要的每秒一帧的速度,这可以达到您想要的效果。