I have some large sprite sheets (up to 2048 x 2048 pixels, png 32 bit) which I use in my programs.
Now, I have the following options:
1. Split the image in an image editor and add the individual sprites to my program as resources (I don't like this approach)
2. Load the image, split it, keep the parts and then dispose it. (I like this most, as the job is done only once)
3. Load the image, keep it and get the sprites out of it when needed.
As for the actual splitting, I thought of two options:
1. Split it first in rows and then the rows in individual cells using
ImageList
s.
private static ImageList Split(Bitmap image, int width, int height)
{
ImageList rows = new ImageList();
rows.ImageSize = new Size(image.Width, height);
rows.Images.AddStrip(image);
ImageList cells = new ImageList();
cells.ImageSize = new Size(width, height);
foreach (Image row in Rows.Images)
{
cells.Images.AddStrip(row);
}
return cells;
}
(rough code, just for purpose of exemplification)
2. Draw portions of it on a canvas using
Graphics
.
private static ImageList Split(Bitmap image, int width, int height)
{
ImageList cells = new ImageList();
cells.ImageSize = new Size(width, height);
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
for (int i = 0; i < image.Width / width; i++)
{
for (int j = 0; i < image.Height / height; j++)
{
g.DrawImageUnscaled(image, -i * width, -j * height);
cells.Images.Add((Bitmap)bmp.Clone());
}
}
return cells;
}
(rough code, just for purpose of exemplification)
However, both methods are quite slow... :(
There must be a better way!
Can someone give me a few ideas?
Thank you.
If you think this is a good question, please vote it.