Sign in to follow this  
jagguy2

frames from sprite sheet in vb.net

Recommended Posts

I have about 16 sprites on a sprite sheet (ione bmp file) and how do i load and display 1 image into a picture box and then later on display another image from the same sprite sheet. Do I need to cut the sprite sheet up into smaller files or is there a way to display part of an image from this sprites = Image.FromFile("images/caveman.bmp")

Share this post


Link to post
Share on other sites
Graphics.DrawImage has a number of different overloads, including ones that let you specify a source rectangle. Assuming your sprites are 32x32 pixels, you could do something like this:

Dim SpriteSheet As New Bitmap(Path.Combine(Application.StartupPath, Path.Combine("images", "caveman.bmp")))

Dim Frame As New Bitmap(32, 32)
Using FrameGraphics As Graphics = Graphics.FromImage(Frame)
FrameGraphics.PixelOffsetMode = PixelOffsetMode.Half
FrameGraphics.DrawImage(SpriteSheet, New Rectangle(0, 0, 32, 32), New Rectangle(32, 0, 32, 32), GraphicsUnit.Pixel)
End Using


This would copy the 32x32 sprite starting at (32,0) to "Frame", which could then be set as the image in a picture box or similar.

The neatest way of doing this in your instance would probably be to create your own control that inherits from PictureBox, and add in some extra properties to handle sprite sheets.

Edit: Whoops, VB.NET. Removed the C# source code. [rolleyes]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this