none
How to clockwise rotate a picture,

    Question

  • I have a question.

    How to rotate a picture?

    If there are pictureBox and a timer in Form1.

    When Form_Load, this pictureBox Clockwise rotatation of the center itself, speed is 1 degree per second.

    How to do it.

    I am raw hand, So lot of question in VB, Thanks.

    Tuesday, May 17, 2011 6:45 AM

Answers

  • If its just for displaying I would consider it a waste of both memory and CPU resources to create a new image every time instead of just render it directly.

    And yes, my original sample required an image in the PictureBox to work, Sorry for not pointing that out.

    /Calle


    - Still confused, but on a higher level -


    Hi Calle,

    I decided to add a bit to your code to prompt the user to select a picture if PictureBox1.Image Is Nothing

    :-)

    To everyone else, add one Timer and one PictureBox to a Form to try this version please.

     

     

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
      Dim rotateDegrees As Single
      Dim image As Image
    
      Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
       Handles PictureBox1.Paint
    
        Dim box As PictureBox = DirectCast(sender, PictureBox)
        Dim g As Graphics = e.Graphics
        Dim center As New Point(box.ClientSize.Width \ 2, box.ClientSize.Height \ 2)
        Dim pts(0) As Point
    
        pts(0) = center
    
        g.RotateTransform(Me.rotateDegrees)
        g.TransformPoints(Drawing2D.CoordinateSpace.World, Drawing2D.CoordinateSpace.Page, pts)
        g.TranslateTransform(-Me.image.Width \ 2, -Me.image.Height \ 2, Drawing2D.MatrixOrder.Prepend)
        g.DrawImage(image, pts(0))
        g.ResetTransform()
    
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
        Me.rotateDegrees = (Me.rotateDegrees + 1) Mod 360
        Me.PictureBox1.Invalidate()
    
      End Sub
    
      Public Sub New()
    
        InitializeComponent()
    
        If PictureBox1.Image Is Nothing Then
          Using ofd As New OpenFileDialog
            ofd.Title = "Please select a picture..."
            ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
            ofd.Filter = "Bmp files|*.bmp|Jpg files|.*.jpg|Jpeg files|*.jpeg|Png files|*.png"
            Dim result As DialogResult = ofd.ShowDialog
            If result = Windows.Forms.DialogResult.OK And ofd.FileName <> String.Empty Then
              PictureBox1.Image = image.FromFile(ofd.FileName)
            End If
          End Using
        End If
    
        ' Move image from picture box into a local variable so the
        ' image won't be rendered twice.
        Me.image = Me.PictureBox1.Image
        Me.PictureBox1.Image = Nothing
        Timer1.Interval = 50
        Timer1.Start()
    
      End Sub
    
    End Class
    

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net



    • Proposed as answer by Mike FengModerator Friday, May 20, 2011 12:44 AM
    • Marked as answer by xaping Monday, May 23, 2011 11:56 PM
    Thursday, May 19, 2011 10:22 PM

All replies

  • I have a question.

    How to rotate a picture?

    If there are pictureBox and a timer in Form1.

    When Form_Load, this pictureBox Clockwise rotatation of the center itself, speed is 1 degree per second.

    How to do it.

    I am raw hand, So lot of question in VB, Thanks.


    Hello xaping,

    A possible solution is to use the method "RotateFlip" class "Image"

    Hello

     


    Carmelo La Monica  http://community.visual-basic.it/carmelolamonica/
    Tuesday, May 17, 2011 7:24 AM
  • Hello

    But in help document, I only find rotate picturebox 90, 180, and 270 degree.

    I want rotate 1 degree per second, and so on continue.

    Thanks.

    Tuesday, May 17, 2011 7:38 AM
  • Hello

    But in help document, I only find rotate picturebox 90, 180, and 270 degree.

    I want rotate 1 degree per second, and so on continue.

    Thanks.


    Hello xaping,

    follow these pages

    http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-rotate

    http://www.codeproject.com/KB/graphics/rotateimage.aspx

     

    Hello


    Carmelo La Monica  http://community.visual-basic.it/carmelolamonica/
    Tuesday, May 17, 2011 8:19 AM
  • Try this example:

    Public Class Form1
    
      Dim rotateDegrees As Integer
      Dim image As Image
    
      Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
        Handles PictureBox1.Paint
    
        Dim box As PictureBox = DirectCast(sender, PictureBox)
        Dim g As Graphics = e.Graphics
        Dim center As New PointF(box.ClientSize.Width / 2, box.ClientSize.Height / 2)
        Dim pts(0) As PointF
    
        pts(0) = center
    
        g.RotateTransform(Me.rotateDegrees)
        g.TransformPoints(Drawing2D.CoordinateSpace.World, Drawing2D.CoordinateSpace.Page, pts)
        g.TranslateTransform(-Me.image.Width / 2, -Me.image.Height / 2, Drawing2D.MatrixOrder.Prepend)
        g.DrawImage(image, pts(0))
        g.ResetTransform()
    
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
        Me.rotateDegrees = (Me.rotateDegrees + 1) Mod 360
        Me.PictureBox1.Invalidate()
      End Sub
    
      Public Sub New()
    
        InitializeComponent()
        ' Move image from picture box into a local variable so the
        ' image won't be rendered twice.
        Me.image = Me.PictureBox1.Image
        Me.PictureBox1.Image = Nothing
      End Sub
    End Class
    

    /Calle


    - Still confused, but on a higher level -
    Tuesday, May 17, 2011 8:30 AM
  • Try this example:

    Public Class Form1
    
    
    
     Dim rotateDegrees As Integer
    
     Dim image As Image
    
    
    
     Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
    
      Handles PictureBox1.Paint
    
    
    
      Dim box As PictureBox = DirectCast(sender, PictureBox)
    
      Dim g As Graphics = e.Graphics
    
      Dim center As New PointF(box.ClientSize.Width / 2, box.ClientSize.Height / 2)
    
      Dim pts(0) As PointF
    
    
    
      pts(0) = center
    
    
    
      g.RotateTransform(Me.rotateDegrees)
    
      g.TransformPoints(Drawing2D.CoordinateSpace.World, Drawing2D.CoordinateSpace.Page, pts)
    
      g.TranslateTransform(-Me.image.Width / 2, -Me.image.Height / 2, Drawing2D.MatrixOrder.Prepend)
    
      g.DrawImage(image, pts(0))
    
      g.ResetTransform()
    
    
    
     End Sub
    
    
    
     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
    
    
      Me.rotateDegrees = (Me.rotateDegrees + 1) Mod 360
    
      Me.PictureBox1.Invalidate()
    
     End Sub
    
    
    
     Public Sub New()
    
    
    
      InitializeComponent()
    
      ' Move image from picture box into a local variable so the
    
      ' image won't be rendered twice.
    
      Me.image = Me.PictureBox1.Image
    
      Me.PictureBox1.Image = Nothing
    
     End Sub
    
    End Class
    
    

    /Calle


    - Still confused, but on a higher level -


    I create a new project, and create a picturebox1 and a timer1

    Then copy your code in this project, but running nothing happend.

    Maybe I lost some step. How to Load you code???

    Thank you

    Tuesday, May 17, 2011 9:17 AM
  • Based on the code from Calle I made a quick and dirty sample (but it runs and rotates, but you will have to adjust it that it becomes usable)

    Public Class Form1
      Dim bm As Bitmap
      Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Timer1.Enabled = True
        Timer1.Interval = 1000
        bm = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Using g As Graphics = Graphics.FromImage(bm)
          g.CopyFromScreen(New Point(0, 0), New Point(0, 0), New Size(200, 200))
        End Using
        PictureBox1.Image = bm
      End Sub
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim g As Graphics = Graphics.FromImage(bm)
        Dim center As New PointF(PictureBox1.ClientSize.Width \ 2, PictureBox1.ClientSize.Height \ 2)
        Dim pts(0) As PointF
        pts(0) = center
        g.RotateTransform(30)
        g.TransformPoints(Drawing2D.CoordinateSpace.World, Drawing2D.CoordinateSpace.Page, pts)
        g.TranslateTransform(-bm.Width \ 2, -bm.Height \ 2, Drawing2D.MatrixOrder.Prepend)
        g.DrawImage(bm, pts(0))
        g.ResetTransform()
        PictureBox1.Image = Nothing
        PictureBox1.Image = bm
      End Sub
    End Class
    


    Success
    Cor
    • Proposed as answer by XI Samuel IX Thursday, May 19, 2011 11:04 AM
    Tuesday, May 17, 2011 9:51 AM
  • I create a new project, and create a picturebox1 and a timer1

     


    Is there an image in the PictureBox?
    Tuesday, May 17, 2011 9:53 AM
  •  Yes I get it from screen, but the code from Calle does not run as it is. I also changed the Double divide in integer divides.

    And it shows now a nice rotating piece of the screen.

     


    Success
    Cor

    Tuesday, May 17, 2011 10:16 AM
  • Yes, right, Cor Ligthert's program, the parts of screen was turn when program running.

    And after I will find solution erase old picture.

    Thank you, all of give my solution

    Tuesday, May 17, 2011 10:53 AM
  • Hi Xaping,

    Thank you for posting here.

    Here is the whole code in the form1.vb file, please take a look at it:

    Public Class Form1
      Dim bm As Bitmap
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Timer1.Interval = 1000
        bm = New Bitmap(400, 400)
        Using g As Graphics = Graphics.FromImage(bm)
          g.CopyFromScreen(New Point(0, 0), New Point(0, 0), New Size(400, 400))
        End Using
        PictureBox1.Size = New Size(400, 400)
        'PictureBox1.Image = bm ' This line must be commented out
      End Sub
      Dim angle As Integer = 10
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim bmCopy As Image = bm.Clone
        Dim center As New PointF(bmCopy.Width \ 2, bmCopy.Height \ 2)
        Dim oldImage As Image = PictureBox1.Image
    
        PictureBox1.Image = RotateImage(bmCopy, center, angle)
    
        If oldImage IsNot Nothing Then
          oldImage.Dispose()
        End If
        angle = (angle + 10) Mod 360
      End Sub
    
      ''' <summary>
      ''' Creates a new Image containing the same image only rotated
      ''' </summary>
      ''' <param name="image">The <see cref="System.Drawing.Image"/> to rotate</param>
      ''' <param name="offset">The position to rotate from.</param>
      ''' <param name="angle">The amount to rotate the image, clockwise, in degrees</param>
      ''' <returns>A new <see cref="System.Drawing.Bitmap"/> of the same size rotated.</returns>
      ''' <exception cref="System.ArgumentNullException">Thrown if <see cref="image"/> is null.</exception>
      Public Shared Function RotateImage(ByVal image As Image, ByVal offset As PointF, ByVal angle As Single) As Bitmap
        If image Is Nothing Then
          Throw New ArgumentNullException("image")
        End If
    
        'create a new empty bitmap to hold rotated image
        Dim rotatedBmp As New Bitmap(image.Width, image.Height)
        rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution)
    
        'make a graphics object from the empty bitmap
        Dim g As Graphics = Graphics.FromImage(rotatedBmp)
    
        'Put the rotation point in the center of the image
        g.TranslateTransform(offset.X, offset.Y)
    
        'rotate the image
        g.RotateTransform(angle)
    
        'move the image back
        g.TranslateTransform(-offset.X, -offset.Y)
    
        'draw passed in image onto graphics object
        g.DrawImage(image, New PointF(0, 0))
    
        Return rotatedBmp
      End Function
    End Class
    

    I hope this will be helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 19, 2011 8:55 AM
    Moderator
  • If its just for displaying I would consider it a waste of both memory and CPU resources to create a new image every time instead of just render it directly.

    And yes, my original sample required an image in the PictureBox to work, Sorry for not pointing that out.

    /Calle


    - Still confused, but on a higher level -
    Thursday, May 19, 2011 9:33 AM
  • If its just for displaying I would consider it a waste of both memory and CPU resources to create a new image every time instead of just render it directly.

    And yes, my original sample required an image in the PictureBox to work, Sorry for not pointing that out.

    /Calle


    - Still confused, but on a higher level -


    Hi Calle,

    I decided to add a bit to your code to prompt the user to select a picture if PictureBox1.Image Is Nothing

    :-)

    To everyone else, add one Timer and one PictureBox to a Form to try this version please.

     

     

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
      Dim rotateDegrees As Single
      Dim image As Image
    
      Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
       Handles PictureBox1.Paint
    
        Dim box As PictureBox = DirectCast(sender, PictureBox)
        Dim g As Graphics = e.Graphics
        Dim center As New Point(box.ClientSize.Width \ 2, box.ClientSize.Height \ 2)
        Dim pts(0) As Point
    
        pts(0) = center
    
        g.RotateTransform(Me.rotateDegrees)
        g.TransformPoints(Drawing2D.CoordinateSpace.World, Drawing2D.CoordinateSpace.Page, pts)
        g.TranslateTransform(-Me.image.Width \ 2, -Me.image.Height \ 2, Drawing2D.MatrixOrder.Prepend)
        g.DrawImage(image, pts(0))
        g.ResetTransform()
    
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
        Me.rotateDegrees = (Me.rotateDegrees + 1) Mod 360
        Me.PictureBox1.Invalidate()
    
      End Sub
    
      Public Sub New()
    
        InitializeComponent()
    
        If PictureBox1.Image Is Nothing Then
          Using ofd As New OpenFileDialog
            ofd.Title = "Please select a picture..."
            ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
            ofd.Filter = "Bmp files|*.bmp|Jpg files|.*.jpg|Jpeg files|*.jpeg|Png files|*.png"
            Dim result As DialogResult = ofd.ShowDialog
            If result = Windows.Forms.DialogResult.OK And ofd.FileName <> String.Empty Then
              PictureBox1.Image = image.FromFile(ofd.FileName)
            End If
          End Using
        End If
    
        ' Move image from picture box into a local variable so the
        ' image won't be rendered twice.
        Me.image = Me.PictureBox1.Image
        Me.PictureBox1.Image = Nothing
        Timer1.Interval = 50
        Timer1.Start()
    
      End Sub
    
    End Class
    

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net



    • Proposed as answer by Mike FengModerator Friday, May 20, 2011 12:44 AM
    • Marked as answer by xaping Monday, May 23, 2011 11:56 PM
    Thursday, May 19, 2011 10:22 PM
  • The following link will be useful to you

    Image Processing Lab in C#

    http://www.codeproject.com/KB/GDI-plus/Image_Processing_Lab.aspx

    Regards,

    Narendran Ponpandiyan

    Friday, May 20, 2011 4:37 AM