2016-11-04 114 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