-2

我目前正在制作精灵表制作器,但在拼接目录中的所有图像时遇到了麻烦。到目前为止,我的代码可以读取用户输入的目录中所有文件的名称,并将它们放在字符串列表中。但是我不确定如何将它们缝合在一起。
我现在添加了用于拼接图像的代码,并在程序保存拼接图像时出现异常

。我拉入的图像是 *.png,输出也是如此。图像需要水平缝合。
我当前的代码:

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();
                }
            }
        }
4

2 回答 2

0

真的没那么难 - 依次解锁每个图像的位并将其写入新图像(考虑到空/边界/缓冲区像素)。

您需要预先确定上下多少张图像、每个图像的大小以及最终图像的尺寸。还有图像格式(pixelFormat - 是否需要透明度等)您可以设置 Stride 或让 .Net 为您设置等。

通常,您希望尺寸为 2 的幂或 4 的幂 - 取决于最终精灵表的使用。您可能还需要将图像转换为 ny .Net 不直接支持的 DDS。

于 2012-08-23T13:17:36.970 回答
0

与其自己构建东西,为什么不使用像SpriteMe这样的众多工具之一呢?

于 2012-08-23T12:26:55.607 回答