VB.NET 中图形旋转任意角度 [ZT]

最近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

转载于:https://www.cnblogs.com/RobotTech/archive/2008/04/23/1167344.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值