标题# 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);
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);
if (atz != "ATZ")
{
Console.WriteLine("目标错误");
return;
}
int atzVar = fe.ReadByte();
Console.WriteLine(atzVar);
byte[] picCount = new byte[4];
fe.Read(picCount, 0, 4);
int picc = BitConverter.ToInt32(picCount, 0);
fe.Position = 12;
NodeData nodeData = new NodeData();
nodeData.sFile = fileName;
int iRValue, iGValue, iBValue, iAlphaValue;
nodeData.colorIndex = new Color[256];
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);
}
for (int i = 0; i < picc; i++)
{
NodeData node = new NodeData();
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);
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);
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));
}
else
{
int index = fe.ReadByte();
iColor = nodeData.colorIndex[index];
}
node.msData.WriteByte(iColor.R);
node.msData.WriteByte(iColor.G);
node.msData.WriteByte(iColor.B);
}
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 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();
if (ipox > pic.Width - 1)
{
ipoy += 1;
ipox = 0;
}
pic.SetPixel(ipox, ipoy, Color.FromArgb(bR, bG, bB));
ipox += 1;
}
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);
}
}