Rotate and Resize Bitmap

Unhnd_Exception 0 Tallied Votes 864 Views Share
''' <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
Member Avatar for Unhnd_Exception
Unhnd_Exception

Edit.

I made this for no reason. Turns out I needed it. After putting it to the test it has some issues. It doesn't work for angles 90 - 270.

This fixes it.

Private Function RotateAndResize(ByRef Original_Bitmap As Bitmap, ByRef Angle As Double) As Bitmap

        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
        M.RotateAt(Angle, New PointF(Original_Bitmap.Width / 2, Original_Bitmap.Height / 2))
        M.TransformPoints(BoxCorners)

        Dim left As Integer = Integer.MaxValue
        Dim right As Integer = Integer.MinValue
        Dim top As Integer = Integer.MaxValue
        Dim bottom As Integer = Integer.MinValue

        For i = 0 To UBound(BoxCorners)
            If BoxCorners(i).X < left Then left = BoxCorners(i).X
            If BoxCorners(i).X > right Then right = BoxCorners(i).X
            If BoxCorners(i).Y < top Then top = BoxCorners(i).Y
            If BoxCorners(i).Y > bottom Then bottom = BoxCorners(i).Y
        Next

        Dim RotatedBitmap = New Bitmap(right - left, bottom - top)
        Dim x As Integer = (RotatedBitmap.Width - Original_Bitmap.Width) / 2
        Dim y As Integer = (RotatedBitmap.Height - Original_Bitmap.Height) / 2
        Dim g As Graphics = Graphics.FromImage(RotatedBitmap)

        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()

        Return RotatedBitmap

    End Function
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.