VB.NET中心旋轉(zhuǎn)圖像實(shí)現(xiàn)技巧分享
作者:佚名
VB.NET中心旋轉(zhuǎn)圖像的實(shí)現(xiàn),完全可以按照本文給出的這段代碼進(jìn)行操作。初學(xué)者們可以以此為參考,進(jìn)行實(shí)際編寫(xiě),以加深對(duì)此的了解。
我們?cè)趯W(xué)習(xí)一門(mén)編程語(yǔ)言的時(shí)候,需要通過(guò)不斷的實(shí)踐去積累經(jīng)驗(yàn),來(lái)加深我們對(duì)這門(mén)語(yǔ)言的理解程度。對(duì)于VB.NET的學(xué)習(xí)同樣也是如此。在這里我們先通過(guò)一段VB.NET中心旋轉(zhuǎn)圖像的實(shí)現(xiàn)代碼來(lái)初步的了解一下這門(mén)語(yǔ)言的編寫(xiě)方式和應(yīng)用方法。
鼠標(biāo)拖拽旋轉(zhuǎn)。實(shí)現(xiàn)任意角度的VB.NET中心旋轉(zhuǎn)圖像。
- 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)- '可以用叉乘求面積,正負(fù)號(hào)代表旋轉(zhuǎn)方向,而后正弦定理求角度,
- Dim O As Double = Windows.Vector.AngleBetween(vOB, vOE)
- '若角度為有效值
- gr.TranslateTransform(bmpsize / 2, bmpsize / 2)
'移動(dòng)坐標(biāo)至圖像中心- gr.RotateTransform(O) '按角度旋轉(zhuǎn)
- 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中心旋轉(zhuǎn)圖像的具體操作方法就為大家介紹到這里,希望對(duì)大家有所幫助。
責(zé)任編輯:曹凱
來(lái)源:
CSDN