XML图集拆分
最近找了套老游戲的資源圖集,合圖生成的文件是xml的,沒有找到拆圖的工具,所以自己寫了個(gè)
可以通過(guò)修改拆分任何格式配置的圖集,代碼如下,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坐標(biāo)???
? ? ? ? ? ? int y = 0; ? //截取Y坐標(biāo)???
? ? ? ? ? ? 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;
? ? ? ? ? ? }
? ? ? ? ? ? //創(chuàng)建新圖位圖???
? ? ? ? ? ? Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
? ? ? ? ? ? //創(chuàng)建作圖區(qū)域???
? ? ? ? ? ? Graphics graphic = Graphics.FromImage(bitmap);
? ? ? ? ? ? //截取原圖相應(yīng)區(qū)域?qū)懭胱鲌D區(qū)???
? ? ? ? ? ? graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
? ? ? ? ? ? bitmap.Save(saveFilePath, bitmap.RawFormat);
? ? ? ? ? ? bitmap.Dispose();
? ? ? ? ? ? graphic.Dispose();
? ? ? ? }
? ? }
}
?
總結(jié)
- 上一篇: 2012系统服务器网络限制,Window
- 下一篇: asp.net ajax控件工具集 Au