Rotate Image

dannyres

Well-known member
Joined
Aug 29, 2003
Messages
67
Hi, im trying to work out how i can Rotate an image from the center of it. From what ive found out so far i need to use RotateTransform and TranslateTransform.

Ive tried all sorts of things to get this to work but so far no luck. :(

So just to sum up, i have an image which i need to spin around at an interval. (for example a circle with a smily face.. the outside of the circle would always be in the same place, just the :) would move around).



Thanks, Dan
 
You can use the RotateFlip method of the Bitmap object to rotate it:
Code:
Dim b As New Bitmap("some path")
rotate the image by 90 degrees
b.RotateFlip(RotateFlipType.Rotate90FlipNone)
 
Last edited by a moderator:
Ok ive had a look around the net and in the .net sdk and i found out that i need to create a matrix object, and use its rotateat method and then apply it to my graphics object.

Code:
        Dim fg As New Bitmap("C:\Images\Foreground.png")
        Dim g As Graphics = Me.CreateGraphics
        Dim TestMatrix As New Matrix
        Dim RotatePoint As New PointF(fg.Width / 2, fg.Height / 2)

        TestMatrix.RotateAt(90, RotatePoint)
        g.Transform = TestMatrix

        g.DrawImage(fg, 0, 0)

        g.Dispose()

I tried that and from what i can see it should be working? But my image is square and when i rotate it 90 degrees it doesnt end up where it began. Can anyone spot the problem?



Dan
 
Ok ive working it out :)


Code:
        Dim g As Graphics = Me.CreateGraphics
        Dim TestMatrix As New Matrix
        Dim RotatePoint As New PointF(fg.Width / 2, fg.Height / 2)

        TestMatrix.RotateAt(90, RotatePoint)
        g.Transform = TestMatrix

        g.DrawImage(fg, 0, 0, fg.Width, fg.Height)

        g.Dispose()

Apparently using DrawImage doesnt draw the image the right size unless you specify the width and height. I tried using DrawImageUnscaled but that had the same problem.



Dan :)
 
Just out of mindless curiosity could you use this approach to animate a sprite, thus using a single image as opposed to say 8?

Would this method involve too much overhead?
 
Back
Top