Imports System.Drawing.Drawing2D
Public Class Form1
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim RotateAngle = 1
Dim offset As Integer = -10
Dim tickcnt As Long = Now.Ticks
Dim BackBuffer As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
Dim DrawingArea As Graphics = Graphics.FromImage(BackBuffer)
Dim Viewable As Graphics = Me.CreateGraphics()
Dim TopRight As New PointF(800, 50)
Dim i As System.Drawing.Image = My.Resources.MyPic
Dim myPath As New GraphicsPath
Dim OrigPoints() As PointF = {TopRight, New Point(TopRight.X - i.Width, TopRight.Y), New Point(TopRight.X, TopRight.Y + i.Height)}
Dim translateMatrix As New Matrix()
Do While Not Me.BackgroundWorker1.CancellationPending
If Now.Ticks - tickcnt > 500000 Then
RotateAngle += 10
offset -= 5
DrawingArea.Clear(Me.BackColor)
myPath.Reset()
myPath.AddPolygon(OrigPoints)
' Do the transforamtion on the Image
translateMatrix.Reset()
translateMatrix.RotateAt(RotateAngle, New Point((TopRight.X - i.Width / 2) + offset, (TopRight.Y + i.Height / 2)))
translateMatrix.Translate(offset, 0)
'Apply it to the path
myPath.Transform(translateMatrix)
'DrawingArea.DrawString(myPath.PathPoints(0).X, New Font("Arial", 10, FontStyle.Regular), Brushes.Aqua, TopRight.X, TopRight.Y)
' Draw the image with the new points
DrawingArea.DrawImage(i, myPath.PathPoints)
Viewable.DrawImageUnscaled(BackBuffer, 0, 0)
tickcnt = Now.Ticks
End If
Loop
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
BackgroundWorker1.CancelAsync()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
End Sub
End Class
Friday, June 22, 2007
Rotating and moving Graphics/images in VB.NET with Style
I saw a post recently that asked if you could rotate graphics in VB.NET. Now rotating a shape is one thing but an image provided more of a challenge. I knew that matrices are the way to go and not the mathematical calculations using pi, and cos and sin etc. But I also knew that matices cannot be applied directly to the image object. So I had to get my pic into a graphicspath. Well it appears that this is not too bad either. But as soon as I put all four points and decided to rotate, RotateAt gave me a not implemented error??!! So I put only three points and it worked!!! Well now my images is rotating, but flickering like mad. So I looked into double buffering, and finally I have a rotating/sliding image:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment