PDA

Click to See Complete Forum and Search --> : rotating an image, by not using the defaulted upper left corner for rotation point


jkw119
Jan 7th, 2003, 11:25 AM
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


Private Sub TextArea_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles TextArea.Paint

Dim sa As System.Drawing.Size
sa = TextArea.Size

Dim middle As New Point(sa.Width / 4, sa.Height / 3)
e.Graphics.TranslateTransform(middle.X, middle.Y)

'Apply all transforms....
e.Graphics.RotateTransform(45) 'angle to rotate

Dim bmp As New Bitmap("sample.jpg")
e.Graphics.DrawImage(bmp, 0, 0)

End Sub

Lunatic3
Jan 7th, 2003, 12:27 PM
:)

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. (http://www.codeproject.com/csharp/rotateimage.asp)
Dont forget to import system.math and that your image should be 'Centered' in a PictureBox.

Public Function rotate(ByVal img As Bitmap, ByVal angle As Single) As Bitmap
Dim pi2 As Double = PI / 2.0
Dim oldWidth As Double = img.Width
Dim oldHeight As Double = img.Height
Dim theta As Double = angle * PI / 180.0
Dim locked_theta As Double = theta
Dim newWidth, newHeight As Double
Dim nWidth, nHeight As Integer
Dim adjacentTop, oppositeTop As Double
Dim adjacentBottom, oppositeBottom As Double


If (locked_theta >= 0.0 And locked_theta < pi2) Or (locked_theta >= Math.PI And locked_theta < (Math.PI + pi2)) Then

adjacentTop = Math.Abs(Math.Cos(locked_theta)) * oldWidth
oppositeTop = Math.Abs(Math.Sin(locked_theta)) * oldWidth

adjacentBottom = Math.Abs(Math.Cos(locked_theta)) * oldHeight
oppositeBottom = Math.Abs(Math.Sin(locked_theta)) * oldHeight

Else

adjacentTop = Math.Abs(Math.Sin(locked_theta)) * oldHeight
oppositeTop = Math.Abs(Math.Cos(locked_theta)) * oldHeight

adjacentBottom = Math.Abs(Math.Sin(locked_theta)) * oldWidth
oppositeBottom = Math.Abs(Math.Cos(locked_theta)) * oldWidth
End If
newWidth = adjacentTop + oppositeBottom
newHeight = adjacentBottom + oppositeTop
nWidth = Math.Ceiling(newWidth)
nHeight = Math.Ceiling(newHeight)
Dim rotatedBmp As Bitmap = New Bitmap(nWidth, nHeight)
Dim g As Graphics = Graphics.FromImage(rotatedBmp)
Dim points(2) As Point
If locked_theta >= 0.0 And locked_theta < pi2 Then
points(0) = New Point(CInt(oppositeBottom), 0)
points(1) = New Point(nWidth, CInt(oppositeTop))
points(2) = New Point(0, CInt(adjacentBottom))

ElseIf locked_theta >= pi2 And locked_theta < Math.PI Then
points(0) = New Point(nWidth, CInt(oppositeTop))
points(1) = New Point(CInt(adjacentTop), nHeight)
points(2) = New Point(CInt(oppositeBottom), 0)

ElseIf locked_theta >= Math.PI And locked_theta < (Math.PI + pi2) Then
points(0) = New Point(CInt(adjacentTop), nHeight)
points(1) = New Point(0, CInt(adjacentBottom))
points(2) = New Point(nWidth, CInt(oppositeTop))
Else
points(0) = New Point(0, CInt(adjacentBottom))
points(1) = New Point(CInt(oppositeBottom), 0)
points(2) = New Point(CInt(adjacentTop), nHeight)

End If
g.DrawImage(img, points)

g.Dispose()
Return rotatedBmp

End Function

jkw119
Jan 7th, 2003, 03:05 PM
thanks so much, that was exactly what i wanted to do!

Jeff