''' <summary>
''' Returns a new copy of the bitmap passed in. The new copy is rotated
''' to the specified degree and resized to fit its new bounding box.
''' </summary>
''' <remarks>
''' The Original Bitmap should be the bitmap that has never been rotated.
''' If you keep rotating the same image the image will become distorted.
''' </remarks>
Private Function RotateAndResize(ByRef Original_Bitmap As Bitmap, ByRef Angle As Double) As Bitmap
'First store our original points into an array so we can pass
'them to the matrix to get our new rotated points.
Dim BoxCorners As Point() = {New Point(0, 0), _
New Point(Original_Bitmap.Width, 0), _
New Point(Original_Bitmap.Width, Original_Bitmap.Height), _
New Point(0, Original_Bitmap.Height)}
Dim M As New Matrix
'Apply a rotation transform and rotate our original corners.
M.RotateAt(Angle, New PointF(Original_Bitmap.Width / 2, Original_Bitmap.Height / 2))
M.TransformPoints(BoxCorners)
'Now get the size of the new box.
Dim left, right, top, bottom As Integer
For i = 0 To UBound(BoxCorners)
If BoxCorners(i).X < left Then
left = BoxCorners(i).X
ElseIf BoxCorners(i).X > right Then
right = BoxCorners(i).X
End If
If BoxCorners(i).Y < top Then
top = BoxCorners(i).Y
ElseIf BoxCorners(i).Y > bottom Then
bottom = BoxCorners(i).Y
End If
Next
'Initialize a new bitmap, get the new x and y start cooridinates which
'is half the of new box size - the old box size, set the
'graphics object to the new image.
Dim RotatedBitmap = New Bitmap(right - left, bottom - top)
Dim x As Integer = Math.Abs(RotatedBitmap.Width - Original_Bitmap.Width) / 2
Dim y As Integer = Math.Abs(RotatedBitmap.Height - Original_Bitmap.Height) / 2
Dim g As Graphics = Graphics.FromImage(RotatedBitmap)
'reset the matrix, rotate it to our new box: set the graphics tranform, and
'draw the image.
M.Reset()
M.RotateAt(Angle, New PointF(RotatedBitmap.Width / 2, RotatedBitmap.Height / 2))
g.Transform = M
g.DrawImage(Original_Bitmap, New Rectangle(x, y, Original_Bitmap.Width, Original_Bitmap.Height))
M.Dispose()
g.Dispose()
'Our new rotated and resized bitmap.
Return RotatedBitmap
End Function
Unhnd_Exception
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.