旋转图片框中的图像 [英] Rotating an Image in a picture box

查看:98
本文介绍了旋转图片框中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一款游戏,该游戏使用需要旋转的箭头,以便告诉用户他们将抛出他们的方块。我已经编写了几乎所有的解决方案,但是我有箭头作为图片框和里面的图像。我需要能够旋转并记录它旋转的角度,我有一个计时器,我打算每次刻度时将图像旋转一度,并在角度变量上加一个。我唯一挣扎的是实际旋转图像。您需要我提供的任何进一步细节,



非常感谢任何帮助,
非常感谢,
Dan

 私有函数RotateImage(图像为图像,角度为单一)作为位图
'调用代码负责(并且必须)
'处理返回的位图

Dim retBMP As New Bitmap(image)
retBMP.SetResolution(image.Horizo​​ntalResolution,image.VerticalResolution)

使用g = Graphics。 FromImage(retBMP)
'旋转图像中心
g.TranslateTransform(image.Width \ 2,image.Height \ 2)

'rotation
g.RotateTransform(angle)

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

'将图像绘制到新的boitmap
g.DrawImage(retBMP,New PointF(0,0))
结束使用
返回retBMP
结束函数


私人Sub PowerTimer_ Tick(sender As Object,e As EventArgs)Handles PowerTimer.Tick

如果ArrowAngle> = 45那么
AngleIncreasing = False
ElseIf ArrowAngle< = -45然后
AngleIncreasing = True
结束如果
如果AngleIncreasing = True那么
Arrow.Image =新位图(Arrow.Image)
Arrow.Image = RotateImage(Arrow.Image,1 )
Me.Refresh()
ArrowAngle = ArrowAngle + 1
否则
Arrow.Image = RotateImage(Arrow.Image,1)
Arrow.Image = RotateImage( Arrow.Image,-1)
Me.Refresh()
ArrowAngle = ArrowAngle - 1
End if
End Sub


解决方案



由于您的代码不计算新的边界矩形大小,因此会修剪角点。如果旋转的东西在一个较大的图像内,它仍然可以工作。






使用Rotate方法重定向箭头计时器上的-90到90度:





Smoooth!很多将取决于要旋转的图像的性质,在这种情况下我们对图像一无所知。只需看一眼TaskManager就不会泄漏 - 请务必丢弃上一张图片。


I am currently writing a game which uses an arrow which needs to rotate in order to tell the user at what angle they will "throw" their boule. I have coded almost all of the solution however, I have the arrow as a picture box and an image inside of it. I need to be able to rotate in and record at what angle it has been rotated, I have a timer which I plan to rotate the image one degree each time it ticks and add one to the angle variable. The only thing I struggle with is actually rotating the image. Any further details you require I can give,

Any help is much appreciated, Many Thanks, Dan

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

解决方案

There are several issues with the original code and the VB translation. First, CreateGraphics is almost never the right thing to use. Right after that, it is replaced with one from Graphics.FromImage. This is the right way, since you will be drawing to a bitmap, but neither one is disposed, so the app will leak. This could be a problem if something is rotating on a timer.

Second, the c# version is flawed: the method expects an offset to be passed but that can be calculated ("How To Use" doest bother to pass an offset). Finally, the c# version is also leaking.

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

Usage:

' 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 

Results (before and after):

The corners are clipped because your code does not calculate a new bounding rectangle size. If the thing rotating is inside a larger image, it could still work.


Using the Rotate method to redirect an arrow back and forth -90 to 90 degrees on a timer:

Smoooth! A lot will depend on the nature of the image to be rotated, and we know nothing about the image in this case. A glance at TaskManager showed nothing leaking - be sure to dispose of the previous image.

这篇关于旋转图片框中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆