今天在网上看有些人的文章,很是不怎么样。实在忍不住了。
简单的写了一下下。
鼠标拖拽旋转。任意角度。。
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