【问题标题】:Rotate image in pictureBox旋转图片框中的图像
【发布时间】:2011-05-07 20:15:48
【问题描述】:

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

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

我能做些什么呢?

谢谢....

【问题讨论】:

  • @javed Akram 你仍然需要回答你的问题
  • @Javed Akram 您是否考虑过使用带有图像的图像列表,并且每个时钟“滴答”地切换图像?这可能是一个相当合适的解决方案,每次更新都不需要那么多计算。
  • @Mikael,对不起,但我正在制作时钟,它每秒都会改变,所以根据你我需要 43200 张图像(12X60X60)
  • @Javed Akram,好吧,我想的时候有点不走运,以为你只有一只手全天候移动..
  • @Mikael,如果我伤害了你,我很抱歉.....

标签: c# .net image picturebox


【解决方案1】:

【讨论】:

  • RotateFlipType 有明确的角度 90,180,270 但我想旋转 6 度
  • @Javed Akram:是的,代码示例只是使用了框架功能,但是这两个链接应该可以满足您的需求。
  • @javed Akram 查看我发布的第一个链接,它包含你在 c# 中想要的内容
【解决方案2】:

我记得前段时间曾经写过一个类似时钟的 UserControl。这是完成您的请求的基本代码。

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 设置为时钟[这是一个图片框]。


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

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

【讨论】:

  • 在我的代码的第 8 行看到名为 _time 的变量吗?只需将其更改为您想要显示的时间即可。最好从类级变量中加载它,以便您的计时器可以在时钟更新之前设置时间。
【解决方案3】:

试试这个

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

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

【讨论】:

  • 不是他要求的……他需要能够将图像旋转 6°,而不是 90°。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多