Results 1 to 7 of 7

Thread: [02/03] Rotate Graphic in Increments of 90

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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+?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [02/03] Rotate Graphic in Increments of 90

    Give it a try:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     'Rotate clockwise.
    3.     Me.PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
    4.     Me.PictureBox1.Refresh()
    5. End Sub
    6.  
    7. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    8.     'Rotate anti-clockwise.
    9.     Me.PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
    10.     Me.PictureBox1.Refresh()
    11. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Private img As Image = Image.FromFile("image file path here")
    2. Private angle As Single = 0.0F
    3.  
    4. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    5.     Dim imgCentreX As Integer = 100
    6.     Dim imgCentreY As Integer = 100
    7.  
    8.     e.Graphics.TranslateTransform(imgCentreX, imgCentreY)
    9.     e.Graphics.RotateTransform(Me.angle)
    10.     e.Graphics.DrawImage(Me.img, -Me.img.Width \ 2, -Me.img.Height \ 2)
    11. End Sub
    12.  
    13. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.     Me.angle += 30.0F
    15.     Me.Refresh()
    16. End Sub
    17.  
    18. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    19.     Me.angle -= 30.0F
    20.     Me.Refresh()
    21. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Private img As Image = Image.FromFile("image file path here")
    2.  
    3. Private imgTop As Integer = 150
    4. Private imgLeft As Integer = 75
    5.  
    6. Private xOffset As Integer = Me.img.Width \ 2
    7. Private yOffset As Integer = Me.img.Height \ 2
    8.  
    9. Private angle As Single = 0.0F
    10.  
    11. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    12.     e.Graphics.TranslateTransform(Me.imgLeft + Me.xOffset, Me.imgTop + Me.yOffset)
    13.     e.Graphics.RotateTransform(Me.angle)
    14.     e.Graphics.DrawImage(Me.img, -Me.xOffset, -Me.yOffset)
    15. End Sub
    16.  
    17. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    18.     Me.angle += 30
    19.     Me.Refresh()
    20. End Sub
    21.  
    22. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    23.     Me.angle -= 30
    24.     Me.Refresh()
    25. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width