Results 1 to 7 of 7

Thread: Rotating a Picturebox.

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Location
    Sheffield, UK
    Posts
    35

    Rotating a Picturebox.

    Hey, I am creating an Advanced menu DLL, But how can i Rotate a Picturebox so that the Top is Pointing towards the Mouse Cursor?
    Possible?

    - Cheers
    uhm, it wasn't me..

    Prove it..

    Prove it was me........

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

    Re: Rotating a Picturebox.

    You can't. If you want to display an image rotated then you'll need to draw it using GDI+. In the Paint event of your form, or whatever control you want to draw on, use the RotateTransform method to rotate the Graphics object's frame of reference before calling DrawImage.

    You might want to call TranslateTransform too, to make the centre of the image the origin. That makes positioning the Image easier.
    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
    Member
    Join Date
    Sep 2006
    Location
    Sheffield, UK
    Posts
    35

    Re: Rotating a Picturebox.

    Could you help me out there? I added that code..But im not Sure how to go about using it..Im new to GDI...
    My Control to use is the Form...

    - Cheers
    uhm, it wasn't me..

    Prove it..

    Prove it was me........

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

    Re: Rotating a Picturebox.

    Create a new WinForms project.
    Add a NumericUpDown and a Button.
    Set the NUD's Minimum and Maximum to 0 and 359 respectively.
    Add the following code:
    Code:
    Private img As Image = Image.FromFile("file path here")
    
    Private Sub Button1_Click(ByVal sender As Object, _
                              ByVal e As EventArgs) Handles Button1.Click
        Me.Refresh()
    End Sub
    
    Private Sub Form1_Paint(ByVal sender As Object, _
                            ByVal e As PaintEventArgs) Handles Me.Paint
        With e.Graphics
            'Shift the origin to the centre of the form.
            .TranslateTransform(Me.ClientSize.Width \ 2, Me.ClientSize.Height \ 2)
    
            'Rotate the frame of refence.
            .RotateTransform(CInt(Me.NumericUpDown1.Value))
    
            'Position the image so that it is concentric with the form.
            Dim bounds As Rectangle = New Rectangle(-Me.img.Width \ 2, _
                                                    -Me.img.Height \ 2, _
                                                    Me.img.Width, _
                                                    Me.img.Height)
    
            'Draw the image.
            .DrawImage(Me.img, bounds)
        End With
    End Sub
    Insert your own image path.
    Run the project and try changing the angle in the NUD and clicking the Button.

    That shows you how to draw an image using a specific rotation. When you choose to set that angle and redraw the Image is up to you. Note that it is generally preferable to call Invalidate and Update rather than Refresh, so as to keep the area that gets redrawn to a minimum.
    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
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Rotating a Picturebox.

    Quote Originally Posted by jmcilhinney View Post
    Create a new WinForms project.
    Add a NumericUpDown and a Button.
    Set the NUD's Minimum and Maximum to 0 and 359 respectively.
    Add the following code:[CODE]Private img As Image = Image.FromFile("file path here")

    Private Sub Button1_Click(ByVal sender As Object, _
    ByVal e As EventArgs) Handles Button1.Click
    Me.Refresh()
    End Sub

    .

    i think Jm has a bit confuse in here. the event should have be :

    Code:
    Private Sub Button1_Click(ByVal sender As Object, _
                                  ByVal e As EventArgs) Handles NumericUpDown1.ValueChanged
            Me.Refresh()
        End Sub
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

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

    Re: Rotating a Picturebox.

    Quote Originally Posted by manhit45 View Post
    i think Jm has a bit confuse in here. the event should have be :

    Code:
    Private Sub Button1_Click(ByVal sender As Object, _
                                  ByVal e As EventArgs) Handles NumericUpDown1.ValueChanged
            Me.Refresh()
        End Sub
    Um, no, I'm not confused at all. Did you read what I posted?
    Run the project and try changing the angle in the NUD and clicking the Button.
    If you handle the ValueChanged event of the NUD then every time you change the value in the NUD the form will get repainted. If the NUD currently shows 10 and you press and hold its UP button then its Value will be incremented repeatedly. You're hardly going to want the form to repaint on every one of those changes. You would wait until you have reached your intended final value, then explicitly force a repaint by clicking the Button.
    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
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: Rotating a Picturebox.

    oK , I am confuse myself. sorrrrrrrrrry

    (in my case when NUD change the angle change correlatively with value of NUD.)
    Last edited by manhit45; May 20th, 2009 at 01:22 AM.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

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