XML图集拆分

最近找了套老游戏的资源图集,合图生成的文件是xml的,没有找到拆图的工具,所以自己写了个

可以通过修改拆分任何格式配置的图集,代码如下,vs粘上可以直接使用


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Xml;

namespace xmlpng
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("start!");
                //读取xml
                string xmlPath = Console.ReadLine();
                Console.WriteLine(xmlPath);

                string imagePath = xmlPath.Substring(0, xmlPath.LastIndexOf('.') + 1) + "png";
                Console.WriteLine(imagePath);

                string saveRootPath = xmlPath.Substring(0, xmlPath.LastIndexOf('.'));
                System.IO.Directory.CreateDirectory(saveRootPath);
                XmlTextReader reader = new XmlTextReader(xmlPath);
                while (reader.Read())
                {
                    if (reader.Name == "spr")
                    {
                        string name = reader.GetAttribute("name");
                        string path = saveRootPath + "\\" + name + ".png";
                        int x = int.Parse(reader.GetAttribute("x")) * 2;
                        int y = int.Parse(reader.GetAttribute("y")) * 2;
                        int w = int.Parse(reader.GetAttribute("w")) * 2;
                        int h = int.Parse(reader.GetAttribute("h")) * 2;

                        Console.WriteLine(name + "|" + x + "|" + y + "|" + w + "|" + h + "|" + path);
                        CaptureImage(imagePath, path, w, h, x, y);
                    }
                }
            }

        }
        public static void CaptureImage(string sFromFilePath, string saveFilePath, int width, int height, int spaceX, int spaceY)
        {
            //载入底图   
            Image fromImage = Image.FromFile(sFromFilePath);
            int x = 0;   //截取X坐标   
            int y = 0;   //截取Y坐标   
            int sX = fromImage.Width - width;
            int sY = fromImage.Height - height;
            if (sX > 0)
            {
                x = sX > spaceX ? spaceX : sX;
            }
            else
            {
                width = fromImage.Width;
            }
            if (sY > 0)
            {
                y = sY > spaceY ? spaceY : sY;
            }
            else
            {
                height = fromImage.Height;
            }

            //创建新图位图   
            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            //创建作图区域   
            Graphics graphic = Graphics.FromImage(bitmap);
            //截取原图相应区域写入作图区   
            graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
            bitmap.Save(saveFilePath, bitmap.RawFormat);

            bitmap.Dispose();
            graphic.Dispose();
        }

    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值