How to rotate an Image preserving its original size?

后端 未结 1 1954
Happy的楠姐
Happy的楠姐 2021-01-20 05:24

I have been attempting to rotate an Image and I have had some trouble with it. The first block of code was working, but it was causing an undesirable side effect: after the

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-20 06:03

    Not quite clear to me how would you like to rotate the image. Is the problem the rotation axis, the new size of the rotated image, or something else?

    Anyways, to the best of my guessing, the following code snippet rotates the image either around the edges of the canvas or around it's center point.

    Private Function RotateImage(ByVal src As Bitmap,
                                ByVal width As Integer,
                                ByVal height As Integer,
                                ByVal angle As Single,
                                Optional ByVal CenterRotation As Boolean = False) As Bitmap
    
       Dim cornors As Point() = {New Point(0, 0),
                            New Point(width, 0),
                            New Point(width, height),
                            New Point(0, height)}
       Using m As New Matrix
        If Not CenterRotation Then
            m.RotateAt(angle, New PointF(width / 2, height / 2))
            m.TransformPoints(cornors)
        End If
    
        Dim left = Integer.MaxValue
        Dim right = Integer.MinValue
        Dim top = Integer.MaxValue
        Dim bottom = Integer.MinValue
    
        For i = 0 To UBound(cornors)
            If cornors(i).X < left Then left = cornors(i).X
            If cornors(i).X > right Then right = cornors(i).X
            If cornors(i).Y < top Then top = cornors(i).Y
            If cornors(i).Y > bottom Then bottom = cornors(i).Y
        Next
    
        Dim b As New Bitmap(right - left, bottom - top)
        Dim x = (b.Width - width) / 2
        Dim y = (b.Height - height) / 2
    
        Using g As Graphics = Graphics.FromImage(b)
            m.Reset()
            m.RotateAt(angle, New PointF(b.Width / 2, b.Height / 2))
            g.Transform = m
            g.InterpolationMode = InterpolationMode.HighQualityBicubic
            g.SmoothingMode = SmoothingMode.HighQuality
            g.CompositingQuality = CompositingQuality.HighQuality
            g.Clear(Color.Transparent)
            g.DrawImage(src, New Rectangle(x, y, width, height))
        End Using
        Return b
       End Using
    End Function
    

    If you also need to resize the rotated image to fit into the canvas, then you also need the following code AFTER THE FIRST ONE:

    Private Function CreateThumbnail(ByVal bmp As Bitmap,
                                        ByVal canvasWidth As Integer,
                                        ByVal canvasHeight As Integer,
                                        Optional Stretch As Boolean = False) As Bitmap
    
        Dim bmpOut As Bitmap = Nothing
    
        If Stretch Then
            bmpOut = bmp.GetThumbnailImage(canvasWidth, canvasHeight, Nothing, IntPtr.Zero)
        Else
            Dim newWidth As Integer = 0
                Dim newHeight As Integer = 0
    
                bmpOut = New Bitmap(canvasWidth, canvasHeight)
    
                Dim ratioX As Double = CDbl(canvasWidth) / CDbl(bmp.Width)
                Dim ratioY As Double = CDbl(canvasHeight) / CDbl(bmp.Height)
                Dim ratio = If(ratioX < ratioY, ratioX, ratioY)
    
                newWidth = Convert.ToInt32(bmp.Width * ratio)
                newHeight = Convert.ToInt32(bmp.Height * ratio)
    
                If newWidth > bmp.Width Then
                    newWidth = bmp.Width
                End If
                If newHeight > bmp.Height Then
                    newHeight = bmp.Height
                End If
    
                Dim posX = Convert.ToInt32((canvasWidth - newWidth) / 2)
                Dim posY = Convert.ToInt32((canvasHeight - newHeight) / 2)
    
                Using g As Graphics = Graphics.FromImage(bmpOut)
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic
                    g.SmoothingMode = SmoothingMode.HighQuality
                    g.CompositingQuality = CompositingQuality.HighQuality
                    g.Clear(Color.Transparent)
                    g.DrawImage(bmp, posX, posY, newWidth, newHeight)
                End Using
            End If
    
        Return bmpOut
    End Function
    

    Here is a quick demo:

    Good luck.

    0 讨论(0)
提交回复
热议问题