[02/03] Rotate Graphic in Increments of 90
Hey guys!
I have a button on my form, now everytime I click on it, I want a picture to rotate a further 90 degrees. This is what I have so far :
Code:
Dim RBMP As New Bitmap(Selrect.Width, Selrect.Height)
Dim RI As Graphics = Graphics.FromImage(rbmp)
Select Case RC 'counter
Case 90
RI.TranslateTransform(90.0F, 0.0F) 'needs a bit of work
RI.RotateTransform(90) 'needs a bit of work
RI.DrawImage(pic1.Image, New Rectangle(0, 0, Selrect.Width, Selrect.Height), rtSel, GraphicsUnit.Pixel)
Case 180
RI.TranslateTransform(180.0F, 0.0F) 'needs a bit of work
RI.RotateTransform(180) 'needs a bit of work
RI.DrawImage(pic1.Image, New Rectangle(0, 0, Selrect.Width, Selrect.Height), rtSel, GraphicsUnit.Pixel)
Case 270
RI.TranslateTransform(270.0F, 0.0F) 'needs a bit of work
RI.RotateTransform(270) 'needs a bit of work
RI.DrawImage(pic1.Image, New Rectangle(0, 0, Selrect.Width, Selrect.Height), rtSel, GraphicsUnit.Pixel)
Case 360
RI.TranslateTransform(360.0F, 0.0F) 'needs a bit of work
RI.RotateTransform(360) 'needs a bit of work
RI.DrawImage(pic1.Image, New Rectangle(0, 0, Selrect.Width, Selrect.Height), rtSel, GraphicsUnit.Pixel)
Case Is > 360
RC = 0
End Select
pic2.Image = RBMP
The problem is, eventhough my counter RC, becomes incremented, the graphic doesn't rotate to a value of more than 90 degrees. The Selrect object is a dynamic selection rectangle.
How can I achieve this?
Re: [02/03] Rotate Graphic in Increments of 90
Have you considered just displaying the Image in a PictureBox and calling its RotateFlip method? Is it necessary to draw the image using GDI+?
Re: [02/03] Rotate Graphic in Increments of 90
Yes, but the problem I'm sitting with is that those options are quite limited. I also have a button that decrements the counter in increments of 90.
Or should I just rotate the graphic / picturebox just 90 degreess everytime?
Re: [02/03] Rotate Graphic in Increments of 90
Give it a try:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Rotate clockwise.
Me.PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
Me.PictureBox1.Refresh()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Rotate anti-clockwise.
Me.PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
Me.PictureBox1.Refresh()
End Sub
Re: [02/03] Rotate Graphic in Increments of 90
Aah, thanx, that's logical!
OK, another problem ( my real problem) was that I never used DrawImage to draw the image.
2 more questions.
1) what option must I use when the degrees becomes 360 again, should I use RotateNoneFlipNone ?
2) what if I have a custom rotation value, IOW. 33 degrees?
Re: [02/03] Rotate Graphic in Increments of 90
1. You don't use anything other than what I posted. Rotate90FlipNone rotates clockwise 90 degrees from the current orientation. Execute that four times and you're back where you started. The same goes for Rotate270FlipNone. Execute it four times and you're back where you started after rotating anti-clockwise 90 degrees each time.
2. Rotating by anything other than a multiple of 90 degrees is much more complex because there is not a 1:1 correspondence between pixels. The Image class only supports simple rotation. For complex rotation you'd have to use the Graphics class. Try this to rotate clockwise and anti-clockwise in increments of 30 degrees:
vb.net Code:
Private img As Image = Image.FromFile("image file path here")
Private angle As Single = 0.0F
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim imgCentreX As Integer = 100
Dim imgCentreY As Integer = 100
e.Graphics.TranslateTransform(imgCentreX, imgCentreY)
e.Graphics.RotateTransform(Me.angle)
e.Graphics.DrawImage(Me.img, -Me.img.Width \ 2, -Me.img.Height \ 2)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.angle += 30.0F
Me.Refresh()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.angle -= 30.0F
Me.Refresh()
End Sub
Re: [02/03] Rotate Graphic in Increments of 90
Given that we're used to defining locations based on top and left rather than centre, this may be clearer:
vb.net Code:
Private img As Image = Image.FromFile("image file path here")
Private imgTop As Integer = 150
Private imgLeft As Integer = 75
Private xOffset As Integer = Me.img.Width \ 2
Private yOffset As Integer = Me.img.Height \ 2
Private angle As Single = 0.0F
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.TranslateTransform(Me.imgLeft + Me.xOffset, Me.imgTop + Me.yOffset)
e.Graphics.RotateTransform(Me.angle)
e.Graphics.DrawImage(Me.img, -Me.xOffset, -Me.yOffset)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.angle += 30
Me.Refresh()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.angle -= 30
Me.Refresh()
End Sub