地图处理代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public class CustomComparer : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            string s1 = (string)x;
            string s2 = (string)y;
            int s11 = Convert.ToInt32(s1.Substring(0, s1.LastIndexOf(".")));
            int s22 = Convert.ToInt32(s2.Substring(0, s2.LastIndexOf(".")));
            return s11 < s22 ? -1 : 1;
        }
    }
 
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
            string first = "f:/ditu/";
            string[] pathMaps = { "0.map" , "3.map", "d515.map", "d717.map", "d2013.map", "d2052.map", "d2063.map", "d2079.map", "d5071.map", "dm002.map"};
            List<string[,,]> resultList = new List<string[,,]>();
            for (int i = 0; i < pathMaps.Length; ++i)
            {
                resultList.Add(this.dispose(first+pathMaps[i],first+ "object",first+ "tile"));
            }
            for (int r = 0; r < resultList.Count; ++r)
            {
                string[,,] dataArray = resultList[r];
                int mapHeight = dataArray.GetLength(0);
                int mapWidth = dataArray.GetLength(1);
 
                string result = "" + mapWidth + "," + mapHeight;
                for (int i = 0; i < mapHeight; ++i)
                {
                    for (int j = 0; j < mapWidth; ++j)
                    {
                        {
                            string[] temp = dataArray[i, j, 0].Split('/');
                            if (temp.Length<2)
                            {
                                result += ",0";
                            }
                            else
                            {
                                int fileIndex = this.getNameIndex(first + "tileag" + temp[0] + "/", temp[1] + ".png");
                                if (fileIndex == 0)
                                {
                                    result += "," + temp[0] + "/" + temp[1];
                                }
                                else
                                {
                                    result += "," + temp[0] + "_" + fileIndex + "/" + temp[1];
                                }
                            }
                        }
                        {
                            string[] temp = dataArray[i, j, 1].Split('/');
                            if (temp.Length<2)
                            {
                                result += ",0";
                            }
                            else
                            {
                                int fileIndex = this.getNameIndex(first + "objectag" + temp[0] + "/", temp[1] + ".png");
                                if (fileIndex == 0)
                                {
                                    result += "," + temp[0] + "/" + temp[1];
                                }
                                else
                                {
                                    result += "," + temp[0] + "_" + fileIndex + "/" + temp[1];
                                }
                            }
                        }
                        result += "," + dataArray[i, j, 2];
                    }
                }
                this.Write(first + pathMaps[r] + "ag", result);
            }
        }
 
 
        public string[,,] dispose(string pathMap, string pathPng, string pathTile)
        {
            FileStream readStream = new FileStream(pathMap, FileMode.Open);
            byte[] data = new byte[1024];
            readStream.Read(data, 0, 52);
            int mapWidth = (int)data[0];
            int mapHeight = (int)data[2];
            string[,,] dataArray = new string[mapHeight, mapWidth, 3];
            int curWidth = 0;
            int curHeight = 0;
            while (true)
            {
                int length = readStream.Read(data, 0, 14);
                if (length == 0)
                {
                    break;
                }
                else
                {
                    string peng = "0";
                    if (data[1] >= 128)
                    {
                        data[1] -= 128;
                        peng = "1";
                    }
                    if (data[5] >= 128)
                    {
                        data[5] -= 128;
                        peng = "1";
                    }
                    int back = data[1] * 256 + data[0] - 1;
                    string backString = "";
                    if (back != -1 && curWidth % 2 == 0 && curHeight % 2 == 0)
                    {
                        string name = "";
                        for (int i = ("" + back).Length; i < 6; ++i) name += "0";
                        name += back;
                        this.modifyToPng(pathTile + "/" + name + ".bmp", pathTile + "ag"+"0"+"/" + back + ".png");
                        backString = "0/" + back;
                    }
                    else
                    {
                        backString = "0";
                    }
 
 
 
                    string nn = "" + (data[5] * 256 + data[4] - 1);
                    string obj = "" + data[10] + "/" + nn;
                    if (nn == "-1" || data[10] > 100)//纠正data【10】的错误
                    {
                        obj = "0";
                    }
                    else
                    {
                        string name = "";
                        for (int i = nn.Length; i < 6; ++i) name += "0";
                        name += nn;
                        this.modifyToPng(pathPng + data[10] + "/" + name + ".bmp", pathPng + "ag" + data[10] + "/" + nn + ".png");
                    }
 
                    dataArray[mapHeight - 1 - curHeight, curWidth, 0] = backString;
                    dataArray[mapHeight - 1 - curHeight, curWidth, 1] = obj;
                    dataArray[mapHeight - 1 - curHeight, curWidth, 2] = peng;
                    ++curHeight;
                    if (curHeight >= mapHeight)
                    {
                        curHeight = 0;
                        ++curWidth;
                    }
                }
            }
 
 
            readStream.Close();
            return dataArray;
        }
 
 
        public void modifyToPng(string sf,string result)
        {
            Image srcImage = Image.FromFile(sf);
            int resW = srcImage.Width;
            int resH = srcImage.Height;
            Bitmap resultImage = new Bitmap(resW, resH);
            Graphics g = Graphics.FromImage(resultImage);
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(srcImage, new Rectangle(0, 0, resW, resH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
            for (int i = 0; i < resW; i++)
            {
                for (int j = 0; j < resH; j++)
                {
                    Color c = resultImage.GetPixel(i, j);
                    if (c.R == 0 && c.G == 0 && c.B == 0)
                    {
                        resultImage.SetPixel(i, j, Color.FromArgb(0, c));
                    }
                    /*else{
                        int t = c.R + c.G + c.B;
                        if (t > 255) t = 255;
                        resultImage.SetPixel(i, j, Color.FromArgb(t,c.R,c.G,c.B));
                    }*/
                }
            }
            resultImage.Save(result, System.Drawing.Imaging.ImageFormat.Png);
        }
 
 
        private void Write(string path, string str)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write(str);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }
 
 
 
 
        public int getNameIndex(string path,string name)
        {
            // 获取当前路径下全部文件名
            String[] files = Directory.GetFiles(path);
            string[] fileArray = new string[files.Length];
            int i = 0;
            foreach (String filename in files)
            {
                int lastpath = filename.LastIndexOf("/");
                String beginpart = filename.Substring(lastpath + 1);
                fileArray[i] = beginpart;
                ++i;
            }
            Array.Sort(fileArray, new CustomComparer());
 
 
            int cur = 0;
            foreach (String filename in fileArray)
            {
                int lastpath = filename.LastIndexOf("/");
                String beginpart = filename.Substring(lastpath + 1);
                if (beginpart == name)
                {
                    int ret = cur/ 100;
                    return ret;
                }
                ++cur;
            }
            return 0;
        }
    }
}

转载于:https://www.cnblogs.com/agchuanqi/p/7896649.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值