Results 1 to 3 of 3

Thread: rotating an image, by not using the defaulted upper left corner for rotation point

  1. #1

    Thread Starter
    Registered User jkw119's Avatar
    Join Date
    Oct 2001
    Location
    Pittsburgh
    Posts
    256

    rotating an image, by not using the defaulted upper left corner for rotation point

    If anyone can help me out here, i would really appreciate it. I am trying to do a relatively simple task -> Rotate a Picture. Which works fine. But the problem is, my code uses the upper left corner to rotate the image. Is it possible to change that to the middle, or the origin, so that i can rotate the picture from the center. If so, how is this done? Here is the sample code, basically it is a panel on a form, and this is it's paint event. It loads a picture from the programs bin folder. Thanks alot...

    Jeff

    VB Code:
    1. Private Sub TextArea_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TextArea.Paint
    2.  
    3.         Dim sa As System.Drawing.Size
    4.         sa = TextArea.Size
    5.  
    6.         Dim middle As New Point(sa.Width / 4, sa.Height / 3)
    7.         e.Graphics.TranslateTransform(middle.X, middle.Y)
    8.  
    9.         'Apply all transforms....
    10.         e.Graphics.RotateTransform(45) 'angle to rotate
    11.  
    12.         Dim bmp As New Bitmap("sample.jpg")
    13.         e.Graphics.DrawImage(bmp, 0, 0)
    14.  
    15.     End Sub

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474


    Your code rotates the text area not the image. Try it on a form and you will see some intersting results, for example from resize handler goes out of your form.

    Try this code I adopted from C# from here.
    Dont forget to import system.math and that your image should be 'Centered' in a PictureBox.
    VB Code:
    1. Public Function rotate(ByVal img As Bitmap, ByVal angle As Single) As Bitmap
    2.         Dim pi2 As Double = PI / 2.0
    3.         Dim oldWidth As Double = img.Width
    4.         Dim oldHeight As Double = img.Height
    5.         Dim theta As Double = angle * PI / 180.0
    6.         Dim locked_theta As Double = theta
    7.         Dim newWidth, newHeight As Double
    8.         Dim nWidth, nHeight As Integer
    9.         Dim adjacentTop, oppositeTop As Double
    10.         Dim adjacentBottom, oppositeBottom As Double
    11.  
    12.  
    13.         If (locked_theta >= 0.0 And locked_theta < pi2) Or (locked_theta >= Math.PI And locked_theta < (Math.PI + pi2)) Then
    14.  
    15.             adjacentTop = Math.Abs(Math.Cos(locked_theta)) * oldWidth
    16.             oppositeTop = Math.Abs(Math.Sin(locked_theta)) * oldWidth
    17.  
    18.             adjacentBottom = Math.Abs(Math.Cos(locked_theta)) * oldHeight
    19.             oppositeBottom = Math.Abs(Math.Sin(locked_theta)) * oldHeight
    20.  
    21.         Else
    22.  
    23.             adjacentTop = Math.Abs(Math.Sin(locked_theta)) * oldHeight
    24.             oppositeTop = Math.Abs(Math.Cos(locked_theta)) * oldHeight
    25.  
    26.             adjacentBottom = Math.Abs(Math.Sin(locked_theta)) * oldWidth
    27.             oppositeBottom = Math.Abs(Math.Cos(locked_theta)) * oldWidth
    28.         End If
    29.         newWidth = adjacentTop + oppositeBottom
    30.         newHeight = adjacentBottom + oppositeTop
    31.         nWidth = Math.Ceiling(newWidth)
    32.         nHeight = Math.Ceiling(newHeight)
    33.         Dim rotatedBmp As Bitmap = New Bitmap(nWidth, nHeight)
    34.         Dim g As Graphics = Graphics.FromImage(rotatedBmp)
    35.         Dim points(2) As Point
    36.         If locked_theta >= 0.0 And locked_theta < pi2 Then
    37.             points(0) = New Point(CInt(oppositeBottom), 0)
    38.             points(1) = New Point(nWidth, CInt(oppositeTop))
    39.             points(2) = New Point(0, CInt(adjacentBottom))
    40.  
    41.         ElseIf locked_theta >= pi2 And locked_theta < Math.PI Then
    42.             points(0) = New Point(nWidth, CInt(oppositeTop))
    43.             points(1) = New Point(CInt(adjacentTop), nHeight)
    44.             points(2) = New Point(CInt(oppositeBottom), 0)
    45.  
    46.         ElseIf locked_theta >= Math.PI And locked_theta < (Math.PI + pi2) Then
    47.             points(0) = New Point(CInt(adjacentTop), nHeight)
    48.             points(1) = New Point(0, CInt(adjacentBottom))
    49.             points(2) = New Point(nWidth, CInt(oppositeTop))
    50.         Else
    51.             points(0) = New Point(0, CInt(adjacentBottom))
    52.             points(1) = New Point(CInt(oppositeBottom), 0)
    53.             points(2) = New Point(CInt(adjacentTop), nHeight)
    54.  
    55.         End If
    56.         g.DrawImage(img, points)
    57.  
    58.         g.Dispose()
    59.         Return rotatedBmp
    60.  
    61.     End Function

  3. #3

    Thread Starter
    Registered User jkw119's Avatar
    Join Date
    Oct 2001
    Location
    Pittsburgh
    Posts
    256
    thanks so much, that was exactly what i wanted to do!

    Jeff

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