Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleAnimate a moving picture in VB .NET
DescriptionThis example shows how to animate a moving picture in VB .NET. When a Timer fires, it moves the image slightly. This example shows how to make parts of a picture transparent.
Keywordsanimate, animation, multimedia
CategoriesMultimedia, Graphics, VB.NET
 
Thanks to Graeme Summers.

When the tmrFly Timer fires, the program redraws the part of the background image that is currently covered by the image it is animating. It then updates the object's position and calls MakeTransparent to draw the object in its new location.

 
Private Sub tmrFly_Tick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles tmrFly.Tick
    ' Rerdaw area covered in the last frame.
    ' section of background to copy
    Dim RectCopy As New Rectangle(X, Y, 64, 35)
    ' draw this section over the butterfly
    objCanvas.DrawImage(FormBackground, X, Y, RectCopy, _
        GraphicsUnit.Pixel)

    ' Wrap around.
    If X > Me.Width Then X = -picShip.Width

    X += 10 + Rnd() * 10
    Y = 100 + Rnd() * 50 - 25
    MakeTransparent(objCanvas, picShip.Image, X, Y)
End Sub
 
Subroutine MakeTransparent copies the object's image and calls the Bitmap object's MakeTransparent method to make the background pixels transparent. It then draws the image at the correct position on the form's background.
 
Public Sub MakeTransparent(ByVal Canvas As Graphics, ByVal _
    ImageSent As Image, ByVal X As Integer, ByVal Y As _
    Integer)
    ' Create a bitmap from the image sent
    Dim myBitmap As New Bitmap(ImageSent)
    ' Get the color of a background pixel.
    Dim backColor As Color = myBitmap.GetPixel(1, 1)
    ' Make backColor transparent for myBitmap.
    myBitmap.MakeTransparent(backColor)
    ' Draw the transparent bitmap to the canvas
    Canvas.DrawImage(myBitmap, X, Y, myBitmap.Width, _
        myBitmap.Height)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated