在pictureBox中旋转图像

3

我正在制作一个模拟时钟,在其中需要旋转pictureBox中的图像...

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

我该怎么做呢?

谢谢...


@javed Akram,您仍需要解决问题的答案。 - ratty
@Javed Akram,你考虑过使用图像列表,并在每个时钟“滴答”时切换图像吗?这可能是一个相当合适的解决方案,不需要在每次更新时进行太多计算。 - Mikael
@Mikael,抱歉,但我正在制作一个每秒钟都会改变的时钟,所以根据您的说法,我需要43200张图片(12X60X60)。 - Javed Akram
@Javed Akram,好的,我有点不太走运,当时想象你只有一只手在时钟上转动。 - Mikael
@Mikael,如果我伤害了你,我非常抱歉... - Javed Akram
3个回答

4

我记得有一段时间我曾经编写过一个类似于时钟的用户控件。以下是实现您需求的基本代码。

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,以给人以自然的时钟感觉。


在我的代码的第8行看到名为**_time**的变量了吗?只需将其更改为您想要显示的时间即可。最好从类级变量加载它,以便您的计时器可以在更新时设置时间。 - Alex Essilfie

1

RotateFlipType有明确的90度、180度、270度的角度,但我想要旋转6度。 - Javed Akram
1
@Javed Akram:是的,代码示例只使用了框架的功能,但这两个链接应该可以给你想要的东西。 - Oliver
@javed Akram,请查看我发布的第一个链接,它包含了您想要的 C# 相关内容。 - ratty

0

试一下这个

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

或者在每1000毫秒内在一个线程中旋转它


不是他要求的...他需要能够将图像旋转6°,而不是90°。 - Bobby

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接