最近找了套老游戏的资源图集,合图生成的文件是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();
}
}
}