Advertisement
Guest User

Windows Forms: Display image using Control Paint event

a guest
Jun 16th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Class Form1
  2.  
  3.     Dim WithEvents t As New Timer
  4.  
  5.     Dim stp As Integer = 2
  6.     Dim fps As Integer = 25
  7.  
  8.     Dim ax, ay, bx, by As Integer
  9.  
  10.     Dim imgpath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "elephant.png")
  11.     Dim img As Image = Image.FromFile(imgpath)
  12.  
  13.     Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  14.         Dim g As Graphics = e.Graphics
  15.         g.DrawImage(img, ax, ay)
  16.         g.DrawImage(img, bx, by)
  17.     End Sub
  18.  
  19.     Private Sub PaintNextFrame()
  20.         ax = ax + 2 * stp
  21.         bx = bx + stp
  22.         Me.Invalidate()
  23.     End Sub
  24.  
  25.     Private Sub tick(ByVal o As Object, ByVal e As EventArgs) Handles t.Tick
  26.         PaintNextFrame()
  27.     End Sub
  28.  
  29.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  30.         t.Interval = 1000 / fps
  31.     End Sub
  32.  
  33.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  34.         t.Start()
  35.     End Sub
  36.  
  37.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  38.         t.Stop()
  39.     End Sub
  40.  
  41.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  42.         t.Stop()
  43.         ax = 0
  44.         bx = 0
  45.         ay = 0
  46.         by = 0
  47.         Me.Invalidate()
  48.     End Sub
  49. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement