也谈VB.NET中心旋转图像

今天在网上看有些人的文章,很是不怎么样。实在忍不住了。

 

简单的写了一下下。

 

 

 

鼠标拖拽旋转。任意角度。。

 

 

Public Class Form1
    Dim bmp As Bitmap
    Dim bmpsize As Single
    Dim gr As Graphics
    Dim pb As Point
    Dim po As PointF

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bmpsize = Math.Sqrt(Me.Icon.Width ^ 2 + Me.Icon.Height ^ 2)
        bmp = New Bitmap(CInt(bmpsize), CInt(bmpsize))
        gr = Graphics.FromImage(bmp)
        po = New PointF((bmpsize - Me.Icon.Width) / 2, (bmpsize - Me.Icon.Height) / 2)
        gr.DrawIcon(Me.Icon, po.X, po.Y)
        PictureBox1.Image = bmp
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        pb = e.Location
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If Not pb = Point.Empty Then
            'O/-----------B
            '  /
            '   /
            '    /
            '    E
            Dim vOB, vOE As Windows.Vector
            vOB = New Windows.Vector(bmpsize / 2, bmpsize / 2) - New Windows.Vector(pb.X, pb.Y)
            vOE = New Windows.Vector(bmpsize / 2, bmpsize / 2) - New Windows.Vector(e.X, e.Y)
            '可以用叉乘求面积,正负号代表旋转方向,而后正弦定理求角度,
            Dim O As Double = Windows.Vector.AngleBetween(vOB, vOE)
            '若角度为有效值
            gr.TranslateTransform(bmpsize / 2, bmpsize / 2)         '移动坐标至图像中心
            gr.RotateTransform(O)                                   '按角度旋转
            gr.TranslateTransform(-bmpsize / 2, -bmpsize / 2)       '移回
            gr.Clear(Color.Transparent)                             '清除原有图像
            gr.DrawIcon(Me.Icon, po.X, po.Y)                        '绘制新图像
            PictureBox1.Image = bmp
            pb = e.Location
        End If

    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        pb = Point.Empty
    End Sub

End Class

VB.NET中,可以使用PictureBox的RotateTransform方法来旋转图片。 要旋转PictureBox中的图片,首先需要确保PictureBox的SizeMode属性设置为PictureBoxSizeMode.StretchImage,这样可以确保图片能够正确地填充PictureBox控件。 然后,可以通过以下步骤来实现旋转: 1. 创建一个PictureBox控件,并将其添加到窗体上。 2. 通过调用PictureBox的Image.FromFile方法,加载要显示的图片。 3. 在PictureBox的Paint事件中,获取PictureBox的Graphics对象,并创建一个Matrix对象。 4. 使用Matrix对象的Rotate方法来旋转图片,参数为旋转角度。 5. 调用Graphics对象的DrawImage方法来绘制旋转后的图像。 下面是一个简单的示例代码: Private Sub pictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles pictureBox1.Paint Dim img As Image = Image.FromFile("C:\path\to\image.jpg") Dim g As Graphics = e.Graphics Dim angle As Single = 45 '旋转角度 '创建一个旋转矩阵 Dim matrix As New Drawing2D.Matrix() matrix.RotateAt(angle, New Point(pictureBox1.Width / 2, pictureBox1.Height / 2)) '应用旋转矩阵并绘制图像 g.Transform = matrix g.DrawImage(img, 0, 0, pictureBox1.Width, pictureBox1.Height) End Sub 在上述代码中,我们使用了45度的角度来旋转图片。可以根据需求修改旋转角度。注意,旋转角度是以度为单位的,正值表示顺时针旋转,负值表示逆时针旋转。 通过上述步骤,我们可以在VB.NET中使用PictureBox控件来旋转图像
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清晨曦月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值