frames from sprite sheet in vb.net

Started by
1 comment, last by jagguy2 15 years, 8 months ago
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")
Advertisement
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]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

i now need to cut up the sprite sheet but save each frame into a file. So for the attached file i need 16 files of sprites all cut up to the same size.

This topic is closed to new replies.

Advertisement