VB.NET 中圖形旋轉任意角度 [ZT]

來源:互聯網
上載者:User

最近CSDN有幾個人問這個問題,圖形旋轉任意角度方法演算法都很多,這裡主要用 Graphics.RotateTransform()方法實現。

Public Class Form1
    Dim img1 As Image
    Const PI = 3.14159265
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.PictureBox1.Image = Rotate(img1, 30)

    End Sub
    '
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        img1 = Image.FromFile("d:\9.jpg")
        Me.PictureBox1.Image = img1
    End Sub

    Public Function Rotate(ByVal imgSource As Image, ByVal degree As Integer) As Image
        degree = degree Mod 360
        If degree < 0 Then degree = 360 + degree
        If imgSource Is Nothing Then Return Nothing
        Dim ImgTarget As Image = Nothing
        Try
            Select Case degree
                Case 0 To 89
                    ImgTarget = Rotate0_90(imgSource, degree)
                Case 90 To 179
                    ImgTarget = Rotate90_180(imgSource, degree)
                Case 180 To 269
                    ImgTarget = Rotate180_270(imgSource, degree)
                Case 270 To 359
                    ImgTarget = Rotate270_360(imgSource, degree)
            End Select
        Catch
        End Try
        Return ImgTarget
    End Function
    Private Function Rotate0_90(ByVal img As Image, ByVal degree As Integer) As Image
        Dim ImgTarget As Bitmap
        Dim alpha As Double = (degree / 180) * PI

        Dim iWidth As Integer = img.Width * Math.Cos(alpha) + img.Height * Math.Sin(alpha)
        Dim iHeight As Integer = img.Width * Math.Sin(alpha) + img.Height * Math.Cos(alpha)

        ImgTarget = New Bitmap(iWidth, iHeight, Drawing.Imaging.PixelFormat.Format24bppRgb)
        Dim g As Graphics
        g = Graphics.FromImage(ImgTarget)

        g.TranslateTransform(img.Height * Math.Sin(alpha), 0)

        g.RotateTransform(degree)
        'ImgTarget.MakeTransparent(ImgTarget.GetPixel(1, 1))
        g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height))
        Return ImgTarget
    End Function
    Private Function Rotate90_180(ByVal img As Image, ByVal degree As Integer) As Image
        Dim ImgTarget As Bitmap
        Dim alpha As Double = ((degree - 90) / 180) * PI

        Dim iHeight As Integer = img.Width * Math.Cos(alpha) + img.Height * Math.Sin(alpha)
        Dim iWidth As Integer = img.Width * Math.Sin(alpha) + img.Height * Math.Cos(alpha)

        ImgTarget = New Bitmap(iWidth, iHeight, Drawing.Imaging.PixelFormat.Format24bppRgb)
        Dim g As Graphics
        g = Graphics.FromImage(ImgTarget)

        g.TranslateTransform(iWidth, img.Height * Math.Sin(alpha))

        g.RotateTransform(degree)
        'ImgTarget.MakeTransparent(ImgTarget.GetPixel(1, 1))
        g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height))
        Return ImgTarget
    End Function
    Private Function Rotate180_270(ByVal img As Image, ByVal degree As Integer) As Image
        Dim ImgTarget As Bitmap
        Dim alpha As Double = ((degree - 180) / 180) * PI

        Dim iWidth As Integer = img.Width * Math.Cos(alpha) + img.Height * Math.Sin(alpha)
        Dim iHeight As Integer = img.Width * Math.Sin(alpha) + img.Height * Math.Cos(alpha)

        ImgTarget = New Bitmap(iWidth, iHeight, Drawing.Imaging.PixelFormat.Format24bppRgb)
        Dim g As Graphics
        g = Graphics.FromImage(ImgTarget)

        g.TranslateTransform(img.Width * Math.Cos(alpha), iHeight)

        g.RotateTransform(degree)
        'ImgTarget.MakeTransparent(ImgTarget.GetPixel(1, 1))
        g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height))
        Return ImgTarget
    End Function
    Private Function Rotate270_360(ByVal img As Image, ByVal degree As Integer) As Image
        Dim ImgTarget As Bitmap
        Dim alpha As Double = ((degree - 270) / 180) * PI

        Dim iHeight As Integer = img.Width * Math.Cos(alpha) + img.Height * Math.Sin(alpha)
        Dim iWidth As Integer = img.Width * Math.Sin(alpha) + img.Height * Math.Cos(alpha)

        ImgTarget = New Bitmap(iWidth, iHeight, Drawing.Imaging.PixelFormat.Format24bppRgb)
        Dim g As Graphics
        g = Graphics.FromImage(ImgTarget)

        g.TranslateTransform(0, img.Width * Math.Cos(alpha))

        g.RotateTransform(degree)
        'ImgTarget.MakeTransparent(ImgTarget.GetPixel(1, 1))
        g.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height))
        Return ImgTarget
    End Function
End Class

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.