旋转图片框中的图像

发布于 2024-10-01 12:41:30 字数 93 浏览 1 评论 0原文

我正在制作一个模拟时钟,其中我必须旋转图片框中的图像...

例如我想每秒将图像旋转 6 度。

我能为此做什么?

谢谢....

I am making an Analogue Clock in which i have to rotate the image in my pictureBox...

e.g. i want to rotate my image by 6 degrees every second.

what can i do for this?

Thanks....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小ぇ时光︴ 2024-10-08 12:41:30

我记得不久前曾经写过一个类似时钟的用户控件。这是执行您的请求的基本代码。

Private Sub Paint_Clock(ByVal sender As Object, _
                        ByVal e As System.Windows.Forms.PaintEventArgs) _
                        Handles Clock.Paint

    Dim _independentHands as Boolean = False
    Dim g As Graphics = e.Graphics
    Dim centrePoint As Point = New Point(Clock.Width \ 2, Clock.Height \ 2)
         Dim _time As New TimeSpan(5, 2, 15)
    'pens'
    Dim hourPen As New Pen(Color.Black, 3)
    Dim minPen As New Pen(Color.Black, 2)
    Dim secPen As New Pen(Color.Red, 1)

    'clock hand lengths'
    Dim halfClockWidth As Integer = Clock.Width \ 2
    Dim hourLength As Integer = CInt(halfClockWidth * (4 / 6)) - 5
    Dim minLength As Integer = CInt(halfClockWidth * (5 / 6)) - 5
    Dim secLength As Integer = CInt(halfClockWidth * (5 / 6)) - 5
    Dim secLength2 As Integer = CInt(halfClockWidth * (1 / 6))

    'angles'
    Dim secAngle As Single = CSng(_time.Seconds / 60) * 360
    Dim secAngle2 As Single = secAngle - 180
    Dim minAngle As Single = CSng(_time.Minutes / 60) * 360
    Dim hrAngle As Single = CSng((_time.Hours - 12) / 12) * 360
    If Not _independentHands Then minAngle += (secAngle / 60)
    If Not _independentHands Then hrAngle += (minAngle / 12)

    'centre point'
    Dim pointPen As New Pen(Color.Black, 4)
    Dim pointRect As New Rectangle(centrePoint.X - 2, centrePoint.Y - 2, 4, 4)

    'antialias on'
    g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

    'draw the background'
    g.DrawImage(My.Resources.ClockBack, 0, 0, Clock.Width, Clock.Height)

    'draw the hands'
    g.DrawLine(hourPen, centrePoint, GetPoint2(centrePoint, hrAngle, hourLength))
    g.DrawLine(minPen, centrePoint, GetPoint2(centrePoint, minAngle, minLength))
    g.DrawLine(secPen, centrePoint, GetPoint2(centrePoint, secAngle, secLength))
    g.DrawLine(secPen, centrePoint, GetPoint2(centrePoint, secAngle2, secLength2))

    'draw the centre point'
    g.DrawEllipse(pointPen, pointRect)

    'draw the glass'
    g.DrawImage(My.Resources.ClockGlass, 0, 0, Clock.Width, Clock.Height)
End Sub



Private Function GetPoint2(ByVal startPoint As Point, _
                           ByVal angle As Single, _
                           ByVal length As Integer) As Point

    Dim x, y As Integer
    Dim sp As Point = startPoint

    'normalize'
    Do While angle - 360 > 0
        angle -= 360
    Loop
    Do While angle < 0
        angle += 360
    Loop
    If angle = 360 Then angle = 0

    Dim rad = angle * (Math.PI / 180)    'angle in radians'
    'calc the new point'
    x = CInt(length * Math.Sin(rad))
    y = CInt(length * Math.Cos(rad))

    Return New Point(sp.X + x, sp.Y - y)
End Function

注释
1. 将名为 Clock 的 PictureBox 添加到表单(或用户控件)
2. 为时钟背景(钟面)添加一个名为 ClockBack 的资源。
3. 为时钟的玻璃面添加一个名为 ClockGlass 的资源。
4. 将上面的代码放入您的表单中。
5. 将计时器的间隔设置为 1000,并使其 Tick 事件 RefreshInvalidate 时钟 [这是一个 PictureBox]。

请注意,变量 _independentHands 设置为 true 时,会使时钟的指针彼此独立。
例如,当设置为 False 时,4:30 时针将位于 4 和 5 之间的中间位置。当设置为 True 时,时针将位于 4 到 4 之间: 59:59 并将在 5:00:00“跳”至 5。

我更喜欢将其保留为 false,以便给人自然时钟的感觉。

I remember ever writing a Clock-like UserControl some time ago. Here's the fundamental code to do your request.

Private Sub Paint_Clock(ByVal sender As Object, _
                        ByVal e As System.Windows.Forms.PaintEventArgs) _
                        Handles Clock.Paint

    Dim _independentHands as Boolean = False
    Dim g As Graphics = e.Graphics
    Dim centrePoint As Point = New Point(Clock.Width \ 2, Clock.Height \ 2)
         Dim _time As New TimeSpan(5, 2, 15)
    'pens'
    Dim hourPen As New Pen(Color.Black, 3)
    Dim minPen As New Pen(Color.Black, 2)
    Dim secPen As New Pen(Color.Red, 1)

    'clock hand lengths'
    Dim halfClockWidth As Integer = Clock.Width \ 2
    Dim hourLength As Integer = CInt(halfClockWidth * (4 / 6)) - 5
    Dim minLength As Integer = CInt(halfClockWidth * (5 / 6)) - 5
    Dim secLength As Integer = CInt(halfClockWidth * (5 / 6)) - 5
    Dim secLength2 As Integer = CInt(halfClockWidth * (1 / 6))

    'angles'
    Dim secAngle As Single = CSng(_time.Seconds / 60) * 360
    Dim secAngle2 As Single = secAngle - 180
    Dim minAngle As Single = CSng(_time.Minutes / 60) * 360
    Dim hrAngle As Single = CSng((_time.Hours - 12) / 12) * 360
    If Not _independentHands Then minAngle += (secAngle / 60)
    If Not _independentHands Then hrAngle += (minAngle / 12)

    'centre point'
    Dim pointPen As New Pen(Color.Black, 4)
    Dim pointRect As New Rectangle(centrePoint.X - 2, centrePoint.Y - 2, 4, 4)

    'antialias on'
    g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

    'draw the background'
    g.DrawImage(My.Resources.ClockBack, 0, 0, Clock.Width, Clock.Height)

    'draw the hands'
    g.DrawLine(hourPen, centrePoint, GetPoint2(centrePoint, hrAngle, hourLength))
    g.DrawLine(minPen, centrePoint, GetPoint2(centrePoint, minAngle, minLength))
    g.DrawLine(secPen, centrePoint, GetPoint2(centrePoint, secAngle, secLength))
    g.DrawLine(secPen, centrePoint, GetPoint2(centrePoint, secAngle2, secLength2))

    'draw the centre point'
    g.DrawEllipse(pointPen, pointRect)

    'draw the glass'
    g.DrawImage(My.Resources.ClockGlass, 0, 0, Clock.Width, Clock.Height)
End Sub



Private Function GetPoint2(ByVal startPoint As Point, _
                           ByVal angle As Single, _
                           ByVal length As Integer) As Point

    Dim x, y As Integer
    Dim sp As Point = startPoint

    'normalize'
    Do While angle - 360 > 0
        angle -= 360
    Loop
    Do While angle < 0
        angle += 360
    Loop
    If angle = 360 Then angle = 0

    Dim rad = angle * (Math.PI / 180)    'angle in radians'
    'calc the new point'
    x = CInt(length * Math.Sin(rad))
    y = CInt(length * Math.Cos(rad))

    Return New Point(sp.X + x, sp.Y - y)
End Function

Notes
1. Add a PictureBox with the name Clock to your form (or usercontrol)
2. Add a resource called ClockBack for the background of your clock (the clock's face).
3. Add a resource called ClockGlass for the clock's glassy face.
4. Drop the code above in your form.
5. Set a timer's interval to 1000 and have its Tick event Refresh or Invalidate the Clock [which is a PictureBox].

Please note that the variable _independentHands makes the clock's hands independent of each other when set to true.
For example, when set to False, 4:30 will have the hour hand halfway between 4 and 5. When True, the hour hand will be on 4 up till 4:59:59 and will 'jump' to 5 at 5:00:00.

I prefer to leave it to false so as to give the natural clock feel.

深居我梦 2024-10-08 12:41:30

试试这个

pictureBox1.Image.RotateFlip((RotateFlipType.Rotate90FlipX));

或者每 1000 毫秒在线程中轮换一次

try this

pictureBox1.Image.RotateFlip((RotateFlipType.Rotate90FlipX));

or rotate this in a thread per 1000 ms

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文