Animation in VB.NET, Part 3

In this article we are going to look at Bitmap Animations in VB.NET. In VB6 we had to make extensive use of API's in order to load and initialize large quantities of smaller images. Now in .NET we have the GDI+ dynamic library that has a large selection of graphic classes and functions. This means that we no longer require the use of API's to manipulate images.

The GDI+ has the Bitmap class that allows us to create, load and manipulate images. This class takes place of all the API's previously required to allocate memory resources for images and bitmaps. The most important API was BitBlt, which allowed you to draw images just about anywhere and any size in your Application. This API is now replaced with the Bitmaps DrawImage function.

In Part 2 of the VB6 Animation article we did a Donkey Kong Clone game. In this relatively short article we are going to upgrade that game to .NET standards. With this, we will not be using any API's, but only the Functions within the GDI+ library. Just remember that trying to use the Project Upgrade Wizard will not work with the VB6 version of this project. Another Issue is the API's do not translate directly to GDI+ functions.
FloorDC = CreateCompatibleDC(0&)
SelectObject FloorDC, LoadPicture(App.Path & "\floor.bmp")
LadderDC = CreateCompatibleDC(0&)
SelectObject LadderDC, LoadPicture(App.Path & "\ladder.bmp")
OilDC = CreateCompatibleDC(0&)
SelectObject OilDC, LoadPicture(App.Path & "\oil.bmp")

Buffer.Bmp = CreateCompatibleBitmap(Screen.hdc, Screen.ScaleWidth, Screen.ScaleHeight)
Buffer.DC = CreateCompatibleDC(0&)
SelectObject Buffer.DC, Buffer.Bmp

Let's start with declaring our images and buffers. In VB6, apart from declaring all the API's, the memory allocations were like the above code.

In .Net we simply use the following:

        Floorimg = New Bitmap(My.Application.Info.DirectoryPath & "\floor.bmp")
        Ladderimg = New Bitmap(My.Application.Info.DirectoryPath & "\ladder.bmp")
        Oilimg = New Bitmap(My.Application.Info.DirectoryPath & "\oil.bmp")
        BufferBmp = New Bitmap(Screen.Width, Screen.Height)
        BufferObj = Graphics.FromImage(.Bmp)

As you can see. one command takes care of several API functions. In addition, we do not run the risk of Memory leaks if we do not dispose of the DC's properly.

If you remember, in the previous article we covered Masking, a method used to apply irregularly shaped images over each other. We used masking in VB6 because Transparency was not available but now in .NET, transparency is available, and you can set any single colour to be transparent. For the purpose of this article and to differentiate from Masking we will use WHITE for the transparent color in our project.

        Floorimg.MakeTransparent(Color.White)
        Ladderimg.MakeTransparent(Color.White)
Just remember that if any pixel is just off white (IE: RGB 254,254,254) it will not be made transparent.

Here is a sample of images used for the Donkey Kong game.


Sample of images used for the Donkey Kong game

Note: Black will work just as well for transparency, however I used white to differentiate the two methods.

Now that we have all of our images loaded into our small application, let's look at how to use the DrawImage function to create the animations. DrawImage has 30 overloads, however they almost all require only two things.

1: Source Image.
2: Target location.

Our target image is the image used to call the DrawImage function.

BufferObj.DrawImage(Heroimg, HeroX, HeroY)

An Additional feature of the DrawImage function is that you can specify the Source location as well, rendering only part of the source image onto the target. This works very similar to how the BitBlt API worked. To render only a small section of the source image to our buffer we could use the following:
BufferObj.DrawImage(Heroimg, HeroX, HeroY, FrameRectangle, GraphicsUnit.Pixel)
The Source location has to be a Rectangle Typed variable, with an X,Y cord for the top left corner within the source, and a width and length of the desired image. At this time you will also have to specify what unit these cords are, so that the GDI can correctly calculate the section of the image you're using. I prefer having all my images on the same scale and using the pixel unit.

Finally after having put all our animations into the buffer we can display it. This is quick and easy.

Screen.Image = BufferBmp

The attached download is the Upgraded version of the VB6 Donkey Kong starter game, and as before, it's only the base of the game to demo the methods and get you started with animation.



About the Author

Richard Newcombe

Richard Newcombe has been involved in computers since the time of the Commodore 64. Today, he has excelled in programming, and designs. Richard is in his mid 30's and, if or when you looking for him look no further than his computer. Always willing to help and give advice where he can in regard to computer related subjects. At present he is working as a .NET 2008 Software Developer for Syncrony Web Services, South Africa.

Related Articles

Downloads

Comments

  • Harmonograph

    Posted by Patch on 11/27/2012 10:56am

    VB.net 2008 Good evening Richard I have written a program that creats an array of X,Y points that describe the motion of a 2 pendulum harmonograph. I can Paint the whole curve to a PictureBox but I want the curve to display dynamically the way you would see it drawn if you had a physical harmonograph. Hope you can help Patch

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • On-demand Event Event Date: September 17, 2014 Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this webcast and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the future state of your servers, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds