-2

I'm currently making a sprite sheet maker but am having trouble stitching all of the images from a directory. So far I have the code that reads the names of all files in a directory which the user inputs and places them in a list of strings. However I am not sure how to stitch them together.
I have now added code for stitching the images and get an exception when the program goes to save the stitched image

The images I am pulling in are *.png and so will the output be. The images will need to be horizontal stitched.
My current code:

static void Main()
        {
            bool cont = false;
            bool skip = false;
            while (cont == false)
            {
                Console.WriteLine("Enter a Folder name(end to end):");
                string fold = Console.ReadLine();
                if (fold.Equals("end"))
                {
                    break;
                }
                try
                {
                    string[] files = Directory.GetFiles(@"sprites\" + fold, "*.PNG");
                    foreach (string file in files)
                    {
                        Console.WriteLine(file);
                    }
                    skip = false;
                }
                catch (Exception)
                {
                    Console.WriteLine("Folder not Found!");
                    Console.WriteLine("Try Again!");
                    cont = false;
                    skip = true;
                }
                if (skip != true)
                {
                    try
                    {
                        string[] files = Directory.GetFiles(@"sprites\" + fold, "*.PNG");
                        System.Drawing.Bitmap stitchedImage = Combine(files);
                        Console.WriteLine("save filename (no extention)");
                        string fil = Console.ReadLine();
                        stitchedImage.Save(@"/sheets/"+fil+".png", System.Drawing.Imaging.ImageFormat.Png);
                        cont = true;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error Creating Sprite Sheet");
                        Console.WriteLine(ex);
                        Console.WriteLine("Please Try Again!");
                    }
                }
            }
            Console.WriteLine("Done!");
            Console.WriteLine("Program will now exit(Enter to continue)");
            Console.ReadLine();
        }
        public static System.Drawing.Bitmap Combine(string[] files)
        {
            //read all images into memory
            List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
            System.Drawing.Bitmap finalImage = null;

            try
            {
                int width = 0;
                int height = 0;

                foreach (string image in files)
                {
                    //create a Bitmap from the file and add it to the list
                    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);

                    //update the size of the final bitmap
                    width += bitmap.Width;
                    height = bitmap.Height > height ? bitmap.Height : height;

                    images.Add(bitmap);
                }

                //create a bitmap to hold the combined image
                finalImage = new System.Drawing.Bitmap(width, height);

                //get a graphics object from the image so we can draw on it
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
                {
                    //set background color
                    g.Clear(System.Drawing.Color.Black);

                    //go through each image and draw it on the final image
                    int offset = 0;
                    foreach (System.Drawing.Bitmap image in images)
                    {
                        g.DrawImage(image,
                          new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                        offset += image.Width;
                    }
                }

                return finalImage;
            }
            catch (Exception ex)
            {
                if (finalImage != null)
                {
                    finalImage.Dispose();
                }
                Console.WriteLine(ex);
                throw ex;
            }
            finally
            {
                //clean up memory
                foreach (System.Drawing.Bitmap image in images)
                {
                    image.Dispose();
                }
            }
        }
ultrazoid
  • 461
  • 1
  • 6
  • 12

2 Answers2

0

Rather than build something yourself why not use one of the many tools avalible like SpriteMe?

Mark Broadhurst
  • 2,675
  • 23
  • 45
0

Not that difficult really - unlock the bits of each image in turn and write it out to a new image (taking into account empty/bordering/buffer pixels).

You need to pre-decided how many image across and down, size of each, and therefore the final image dimensions. Also image format (pixelFormat - whether you need transparency etc) You can set Stride or let .Net set it for you, etc.

Usually you want dimensions to be Power of 2 or Power of 4 - depeneding on the use of the final sprite sheet. You may also need to convert the image to DDS which is not directly supported ny .Net.

Wolf5370
  • 1,374
  • 11
  • 12