2016-11-04 134 views
0

我目前正在编写一款游戏,该游戏使用需要旋转的箭头来告诉用户他们将“扔”他们的梨子的角度。我编写了几乎所有的解决方案,但是,我有箭头作为图片框和其中的图像。我需要能够旋转并记录它旋转的角度,我有一个计时器,我计划每次旋转图像旋转一个角度,并将角度变量添加一个角度。我唯一要做的就是旋转图像。你需要我可以给任何进一步的细节,在图片框中旋转图片

任何帮助是非常赞赏, 非常感谢, 丹

Private Function RotateImage(image As Image, angle As Single) As Bitmap 
    ' the calling code is responsible for (and must) 
    ' disposing of the bitmap returned 

    Dim retBMP As New Bitmap(image) 
    retBMP.SetResolution(image.HorizontalResolution, image.VerticalResolution) 

    Using g = Graphics.FromImage(retBMP) 
     ' rotate aroung the center of the image 
     g.TranslateTransform(image.Width \ 2, image.Height \ 2) 

     'rotate 
     g.RotateTransform(angle) 

     g.TranslateTransform(-image.Width \ 2, -image.Height \ 2) 

     'draw image to the new boitmap 
     g.DrawImage(retBMP, New PointF(0, 0)) 
    End Using 
    Return retBMP 
End Function 


Private Sub PowerTimer_Tick(sender As Object, e As EventArgs) Handles PowerTimer.Tick 

    If ArrowAngle >= 45 Then 
     AngleIncreasing = False 
    ElseIf ArrowAngle <= -45 Then 
     AngleIncreasing = True 
    End If 
    If AngleIncreasing = True Then 
     Arrow.Image = New Bitmap(Arrow.Image) 
     Arrow.Image = RotateImage(Arrow.Image, 1) 
     Me.Refresh() 
     ArrowAngle = ArrowAngle + 1 
    Else 
     Arrow.Image = RotateImage(Arrow.Image, 1) 
     Arrow.Image = RotateImage(Arrow.Image, -1) 
     Me.Refresh() 
     ArrowAngle = ArrowAngle - 1 
    End If 
End Sub 
+0

你有没有做过任何研究?第一次谷歌搜索返回MSDN写入'RotateFlip'方法。 https://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip(v=vs.110).aspx – TheValyreanGroup

+0

是的,我已经完成了研究,但你应该知道RotateFlip方法不能做事情的价值,如1度,它可以做90,180和所有其他主要价值,但我不相信它可以做一个学位。纠正我,如果我错了,并显示一些示例代码,但我不认为这种方法适用于我的实施。只是为了让你知道我也研究过图像(图画),但是为了旋转箭头而效率不高。 – Dandb79

+0

http://www.codeproject.com/Articles/58815/C-Image-PictureBox-Rotations – TheValyreanGroup

回答

7

没有与the original code和VB翻译的几个问题。首先,CreateGraphics几乎从不是正确的使用方式。之后,它被替换为Graphics.FromImage中的一个。这是正确的方法,因为您将绘制位图,但都没有放置,所以应用程序会泄漏。如果某个计时器正在旋转,这可能是一个问题。

其次,C#版本有缺陷:该方法需要传递一个偏移量,但可以计算(“如何使用”doest打扰传递偏移量)。最后,c#版本也在泄漏。

Private Function RotateImage(img As Image, angle As Single) As Bitmap 
    ' the calling code is responsible for (and must) 
    ' disposing of the bitmap returned 

    Dim retBMP As New Bitmap(img.Width, img.Height) 
    retBMP.SetResolution(img.HorizontalResolution, img.VerticalResolution) 

    Using g = Graphics.FromImage(retBMP) 
     ' rotate aroung the center of the image 
     g.TranslateTransform(img.Width \ 2, img.Height \ 2) 

     'rotate 
     g.RotateTransform(angle) 

     g.TranslateTransform(-img.Width \ 2, -img.Height \ 2) 

     'draw image to the bitmap 
     g.DrawImage(img, New PointF(0, 0)) 

     Return retBMP 
    End Using 
End Function 

用法:

' form level var if rotating over and over 
Private CatImg As Bitmap 

' elsewhere: 
CatImg = New Bitmap("C:\Temp\ceiling_cat.jpg") 

' to create a new rotated image: 
Dim bmp = RotateImage(CatImg, -65) 
pb1.Image = bmp 

'ToDo: dispose of the picbox image 

结果(前后):

enter image description here

角被截断,因为您的代码不计算新的边界矩形的大小。如果旋转的东西在更大的图像内,它仍然可以工作。


使用旋转方法箭头重定向回和定时器第四-90到90度:

enter image description here

Smoooth!很多将取决于要旋转的图像的性质,并且在这种情况下我们对图像一无所知。瞥一眼TaskManager显示没有任何泄漏 - 一定要处理先前的图像。

+0

使用此代码,并可以将其添加到我原来的帖子,如果需要的话,但它正在绘制到旧图像把两个都在屏幕上,因为我旋转了很多,这导致图片框只是填写红色。有任何想法吗? – Dandb79

+0

当然,我不知道你的形象是什么样子,或者有多大的红色因素。该方法返回从原始图像旋转的**新**图像,因此如果它正在绘制到前一个图像,则是您如何使用它。这应该是一个像东西的时钟(你提到了1度旋转),看起来很奢侈。 – Plutonix

+0

图像是红色的,它是一个红色的箭头。我将把我当前的代码放入原始文章 – Dandb79