千年ATZ打开源码,winform版

标题# winform打开千年ATZ文件

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 千年
{

    struct NodeData
    {
        public int atzVer;
        public MemoryStream msData;
        public Color[] colorIndex;
        public string sFile;
        public uint iHeight;
        public uint iWidth;
        public uint dwXoff;
        public uint dwYoff;
        public uint dwUnKnow;
    }

    class TMap
    {
        private static TMap instance = new TMap();
        public static TMap Instance
        {
            get { return instance; }
        }
        private TMap() { }

        public List<NodeData> nodeList = new List<NodeData>();
        public Color GetAtz1Color(ushort atz1Value)
        {

            if (atz1Value > 0)
            {
                ushort wRGB565_Hight = (ushort)(atz1Value & 0xFFE0);
                ushort wRGB565_Lower = (ushort)(atz1Value & 0x1F);
                wRGB565_Hight = (ushort)(wRGB565_Hight * 2);
                ushort wRGB565 = (ushort)(wRGB565_Hight + wRGB565_Lower);
                byte bR = (byte)((wRGB565 & 0xF800) >> 11);
                byte bG = (byte)((wRGB565 & 0x7E0) >> 5);
                byte bB = ((byte)(wRGB565 & 0x1F));

                byte bTemp = ((byte)((bR << 3) >> 5));
                bR = (byte)((bR << 3) | bTemp);

                bTemp = (byte)((bB << 3) >> 5);
                bB = (byte)((bB << 3) | bTemp);

                bTemp = (byte)((bG << 2) >> 6);
                bG = (byte)((bG << 2) | bTemp);

                return Color.FromArgb(bR, bG, bB);
            }
            return Color.Transparent;
        }
        public void AddFile(string path)
        {
            string fileName = Path.GetFileName(path);
            //Console.WriteLine(fileName);
            using (var fe = File.Open(path, FileMode.Open))
            {
                if (fe == null)
                {
                    Console.WriteLine("打不开文件");
                    return;
                }
                byte[] cAtz = new byte[3];
                fe.Read(cAtz, 0, 3);
                string atz = Encoding.Default.GetString(cAtz);
                //Console.WriteLine(atz);
                if (atz != "ATZ")
                {
                    Console.WriteLine("目标错误");
                    return;
                }
                int atzVar = fe.ReadByte();
                Console.WriteLine(atzVar);

                byte[] picCount = new byte[4];
                //Console.WriteLine(fe.Length);
                fe.Read(picCount, 0, 4);
                //Console.WriteLine(fe.Position);
                int picc = BitConverter.ToInt32(picCount, 0);
                //Console.WriteLine("picc = " + picc);

                fe.Position = 12;
                NodeData nodeData = new NodeData();
                nodeData.sFile = fileName;
                int iRValue, iGValue, iBValue, iAlphaValue;
                nodeData.colorIndex = new Color[256];
                //Console.WriteLine(fe.Position);
                for (int i = 0; i < 256; i++)
                {
                    iRValue = fe.ReadByte();
                    iGValue = fe.ReadByte();
                    iBValue = fe.ReadByte();
                    iAlphaValue = fe.ReadByte();
                    nodeData.colorIndex[i] = Color.FromArgb(iRValue, iGValue, iBValue);
                    //nodeData.colorIndex[i] = ColorIndex[i];


                }
                //Console.WriteLine(fe.Position);

                for (int i = 0; i < picc; i++)
                {
                    NodeData node = new NodeData();
                    //node.colorIndex = new Color[256];
                    node.sFile = "ATZ" + i;

                    byte[] byte1 = new byte[4];
                    fe.Read(byte1, 0, 4);
                    byte[] byte2 = new byte[4];
                    fe.Read(byte2, 0, 4);
                    byte[] byte3 = new byte[4];
                    fe.Read(byte3, 0, 4);
                    byte[] byte4 = new byte[4];
                    fe.Read(byte4, 0, 4);
                    byte[] byte5 = new byte[4];
                    fe.Read(byte5, 0, 4);
                    //Console.WriteLine(fe.Position);

                    node.iWidth = BitConverter.ToUInt32(byte1, 0);
                    node.iHeight = BitConverter.ToUInt32(byte2, 0);
                    node.dwXoff = BitConverter.ToUInt32(byte3, 0);
                    node.dwYoff = BitConverter.ToUInt32(byte4, 0);
                    node.dwUnKnow = BitConverter.ToUInt32(byte5, 0);
                    //int wi = BitConverter.ToInt32(byte1, 0);
                    //int hh = BitConverter.ToInt32(byte2, 0);

                    node.atzVer = atzVar;

                    int size = Convert.ToInt32(node.iWidth * node.iHeight * 3);
                    node.msData = new MemoryStream(size);
                    node.msData.Position = 0;

                    Color iColor;
                    for (int item = 0; item < Convert.ToInt32(node.iWidth * node.iHeight); item++)
                    {
                        if (atzVar == 49)
                        {
                            byte[] byte6 = new byte[2];
                            fe.Read(byte6, 0, 2);
                            iColor = GetAtz1Color(BitConverter.ToUInt16(byte6, 0));
                            //g = GetAtz1Color(BitConverter.ToUInt16(byte6, 0)).G;
                            //b = GetAtz1Color(BitConverter.ToUInt16(byte6, 0)).B;

                        }
                        else
                        {
                            //byte[] byte7 = new byte[1];
                            int index = fe.ReadByte();
                            iColor = nodeData.colorIndex[index];
                            //r = nodeData.colorIndex[byte7].R;
                            //g = nodeData.colorIndex[byte7].G;
                            //b = nodeData.colorIndex[byte7].B;
                        }
                        node.msData.WriteByte(iColor.R);
                        node.msData.WriteByte(iColor.G);
                        node.msData.WriteByte(iColor.B);
                        //Console.Write(iColor);
                    }
                    node.colorIndex = nodeData.colorIndex;
                    nodeList.Add(node);
                }


            }

        }

    }
}

下面是winform界面


```csharp
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 千年
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.listView1.View = View.List;
         
            this.listView1.SmallImageList = this.imageList1;
        }
        private string path = string.Empty;
        //private  TMap map;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog
            {
                InitialDirectory = ".",
                Filter = "所有文件(*.*)|*.*"
            };
            file.ShowDialog();
            if (file.FileName != string.Empty)
            {
                try
                {
                    this.listView1.Items.Clear();
                    TMap.Instance.nodeList.Clear();
                    path = file.FileName;
                    this.label1.Text = file.FileName;


                    TMap.Instance.AddFile(path);


                    for (int i = 0; i < TMap.Instance.nodeList.Count; i++)
                    {
                        this.listView1.Items.Add(TMap.Instance.nodeList[i].sFile);
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item = this.listView1.SelectedItems;
            if (item.Count == 1)
            {
                var it = listView1.SelectedItems[0].Index; //获取索引
                var node = TMap.Instance.nodeList[it];


                int isize = (int)(node.iWidth * node.iHeight);
                int iAtzVer = node.atzVer;
                Bitmap pic = new Bitmap((int)node.iWidth, (int)node.iHeight); //创建一个图片
                int ipox = 0;
                int ipoy = 0;
                node.msData.Position = 0;
                for (int i = 0; i < isize; i++)
                {
                    byte bR = (byte)node.msData.ReadByte();
                    byte bG = (byte)node.msData.ReadByte();
                    byte bB = (byte)node.msData.ReadByte();
                    //Console.Write("R:{0}, G: {1}, B {2}. ", bR, bG, bB);
                    if (ipox > pic.Width - 1)
                    {
                        ipoy += 1;
                        ipox = 0;
                    }
                    pic.SetPixel(ipox, ipoy, Color.FromArgb(bR, bG, bB)); //设置每个像素的颜色
                    ipox += 1;
                }

                //pictureBox1.Width = pic.Width;
                //pictureBox1.Height = pic.Height;
                pictureBox1.Image = pic;
                pictureBox1.Show();


                label2.Text = node.sFile;
                label3.Text = string.Format("宽: {0},高: {1}", node.iWidth, node.iHeight);
                if (iAtzVer == 49)
                {
                    label4.Text = Convert.ToString(isize * 2);
                }
                label4.Text = Convert.ToString(isize);

                if (iAtzVer == 49)
                {
                    label7.Text = "ATZ1";
                }
                else
                {
                    label7.Text = "ATZ0";
                }
            }
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
    }
}

#  下面是主函数

```csharp
    class Program
    {

        [STAThread]
        static void Main(string[] args)
        {

            Form1 f = new Form1();

            Application.Run(f);

        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

langliali

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值