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 SkySide3gby_NetRlb
{
                               

    public partial class Form1 : Form
    {
        Byte[] rlbByte = new Byte[104857600 + 1];//100M
        Byte[,] resByte = new Byte[116, 10485760];//10M
        int[] resAddress = new int[116];//0-4000h
        Byte[] tmpBmpByte = new Byte[10485760 + 1];//10M
        Byte[,] resPicByte = new Byte[201,2000000];
        int[] resPicByteLen = new int[201];
        int tmpBmpByteLen = 0;
        RCHEAD[] reshead = new RCHEAD[116];
        int[] reswidth = new int[116];
        int[] resheight = new int[116];
        //int[] respicAddress = new int[300];//用于图片在res中相对的起始位置
        int resmax = 0;
        int resselected = 0;
        int respicmax = 0;
        int respicselected = 0;
        bool saved = false;
        int rlbByteLen = 0;
        //bool edited = false;
        bool isRlb = true;
        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 116; i++)
            {
                reshead[i] = new RCHEAD();
                reshead[i].ItmCnt = 0;
                reshead[i].ItmLen = 0;
                reshead[i].Reserved = 0;
                reshead[i].ResId = 0;
                reshead[i].ResKey = 0;
                reshead[i].ResLen = 0;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listBox1.SelectedIndex>0)label5.Text = resAddress[listBox1.SelectedIndex]+"";
            resselected = listBox1.SelectedIndex;

            listBox2.Items.Clear();
            respicmax = 0;

            int[] picAry = { 4, 5, 7, 8, 9, 15, 16, 26, 28, 29, 30, 31, 32, 33, 34, 44, 45, 46, 47, 48, 49, 50, 51, 54, 55, 56, 69, 75, 76 };
            int[] srsAry = { 3, 6, 19, 20, 21, 22, 23, 24, 25, 27, 35, 36, 37, 38, 39, 40, 41, 42, 43, 100, 101, 102, 103, 104, 105, 106, 107 };
            for (int i = 0; i < picAry.Length; i++)
            {
                if ((resselected + 1) == picAry[i])
                {
                    byte[] b1 = new byte[4];
                    b1[0] = resByte[resselected, 4];
                    b1[1] = 0;// resByte[resselected, 5];
                    b1[2] = 0;
                    b1[3] = 0;
                    int piccnt = BitConverter.ToInt32(b1, 0);
                    if (resByte[resselected, 5] > 0) piccnt *= 2;
                    respicmax = piccnt;
                    if (piccnt > 200) piccnt = 200;
                    for (int l = 0; l < piccnt; l++)
                    {
                        listBox2.Items.Add((l + 1) + "");
                        if (isRlb)
                        {
                            InitResPicColor(resselected, l, 0);
                        }
                        else
                        {
                            InitResPic(resselected, l, 0);
                        }
                    }



                    
                }
            }
            for (int i = 0; i < srsAry.Length; i++)
            {
                if ((resselected + 1) == srsAry[i])
                {
                    
                    int startpos = 6;
                    byte[] b1 = new byte[4];
                    b1[0] = resByte[resselected, 2];
                    b1[1] = 0;
                    b1[2] = 0;
                    b1[3] = 0;
                    int srscount = BitConverter.ToInt32(b1, 0);

                    byte[] b3 = new byte[4];
                    b3[0] = resByte[resselected, 3];
                    b3[1] = 0;
                    b3[2] = 0;
                    b3[3] = 0;
                    int piccount = BitConverter.ToInt32(b3, 0);

                    startpos = startpos + 5 * srscount;

                    int picLen = 0;
                    for (int k = 0; k < piccount; k++)
                    {
                        if (startpos >= 10485000) break;
                        listBox2.Items.Add((k + 1) + "");
                        if (k > 0) startpos += 6;
                        startpos += picLen;
                        if (startpos >= 10485000) break;
                        byte[] b2 = new byte[4];
                        b2[0] = resByte[resselected, startpos + 0];
                        b2[1] = resByte[resselected, startpos + 1];
                        b2[2] = 0;
                        b2[3] = 0;
                        int wid = BitConverter.ToInt32(b2, 0);

                        byte[] b4 = new byte[4];
                        b4[0] = resByte[resselected, startpos + 2];
                        b4[1] = resByte[resselected, startpos + 3];
                        b4[2] = 0;
                        b4[3] = 0;
                        int high = BitConverter.ToInt32(b4, 0);

                        byte[] b5 = new byte[4];
                        b5[0] = resByte[resselected, startpos + 5];
                        b5[1] = 0;
                        b5[2] = 0;
                        b5[3] = 0;
                        int mode = BitConverter.ToInt32(b5, 0);

                        if (isRlb)
                        {
                            int width2 = (wid * 24 + 31) / 32 * 4;//每行字节数
                            picLen = high * width2;
                        }
                        else
                        {

                            picLen = wid / 8;
                            if ((wid % 8) > 0)
                                picLen++;
                            picLen *= high;
                            picLen *= (mode + 1);
                        }
                        //picLen += 6;

                        resPicByteLen[k] = picLen;
                        
                        //try
                        {
                            for (int m = 0; m < picLen; m++)
                            {
                                if (m >= 2000000) break;
                                resPicByte[k, m] = resByte[resselected, startpos + 6 + m];
                            }
                        }
                        //catch { }
                    }



                    
                }
            }

            if(listBox2.Items.Count>0)listBox2.SelectedIndex = 0;

            
        }

        private void InitResPic(int resselected, int no,int type)//type=1 srs
        {
            if (type == 0)
            {
                byte[] b1 = new byte[4];
                b1[0] = resByte[resselected, 0];
                b1[1] = resByte[resselected, 1];
                b1[2] = 0;
                b1[3] = 0;
                int width = BitConverter.ToInt32(b1, 0);

                byte[] b2 = new byte[4];
                b2[0] = resByte[resselected, 2];
                b2[1] = resByte[resselected, 3];
                b2[2] = 0;
                b2[3] = 0;
                int height = BitConverter.ToInt32(b2, 0);

                int width1 = width + 8 - width % 8;
                int tmp = 0;
                if ((width1 - width) % 8 == 0)tmp = 1;

                int byteCount = ((width1 / 8 - tmp) * height);

                for (int i = 0; i < byteCount; i++)
                {
                    resPicByte[no, i] = resByte[resselected, i + 6 + no * byteCount];
                }
                resPicByteLen[no] = byteCount;
            }

        }

        private void InitResPicColor(int resselected, int no, int type)//type=1 srs
        {
            if (type == 0)
            {
                byte[] b1 = new byte[4];
                b1[0] = resByte[resselected, 0];
                b1[1] = resByte[resselected, 1];
                b1[2] = 0;
                b1[3] = 0;
                int width = BitConverter.ToInt32(b1, 0);

                byte[] b2 = new byte[4];
                b2[0] = resByte[resselected, 2];
                b2[1] = resByte[resselected, 3];
                b2[2] = 0;
                b2[3] = 0;
                int height = BitConverter.ToInt32(b2, 0);

                int width2 = (width * 24 + 31) / 32 * 4;//每行字节数
                int byteCount = height * width2;

                for (int i = 0; i < byteCount; i++)
                {
                    resPicByte[no, i] = resByte[resselected, i + 6 + no * byteCount];
                }
                resPicByteLen[no] = byteCount;
            }

        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void 资源ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string FileName = saveFileDialog1.FileName;

                MySave(FileName);
            } 
        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string FileName = saveFileDialog1.FileName;

                MySave(FileName);
            } 
        }

        private void MySave(string FileName)
        {
            for (int k1 = 0; k1 < 16384; k1++)
            {
                rlbByte[k1] = 0;
            }
            //resAddress - >rlbByte
            /*for(int i = 0 ; i < 108 ; i ++)
            {
                byte[] b2 = BitConverter.GetBytes(resAddress[i]);
                for (int q = 0; q < b2.Length; q++)
                {
                     if (q >= 4) break;
                     rlbByte[i*4+q] = b2[q];
                }
            }*/

            int pos = 16384;//4000h开始
            //resByte->rlbByte
            for (int j = 0; j < 116; j++)
            {
                //pos = resAddress[j];
                resAddress[j] = pos;
                byte[] b0 = BitConverter.GetBytes(resAddress[j]);
                for (int q = 0; q < b0.Length; q++)
                {
                    if (q >= 4) break;
                    rlbByte[j * 4 + q] = b0[q];
                }

                byte[] b1 = BitConverter.GetBytes(reshead[j].ResLen);
                for (int q = 0; q < b1.Length; q++)
                {
                    if (q >= 4) break;
                    rlbByte[pos + q] = b1[q];
                }
                pos += 4;
                byte[] b2 = BitConverter.GetBytes(reshead[j].ResId);
                for (int q = 0; q < b2.Length; q++)
                {
                    if (q >= 2) break;
                    rlbByte[pos + q] = b2[q];
                }
                pos += 2;
                byte[] b3 = BitConverter.GetBytes(reshead[j].ItmCnt);
                for (int q = 0; q < b3.Length; q++)
                {
                    if (q >= 2) break;
                    rlbByte[pos + q] = b3[q];
                }
                pos += 2;
                byte[] b4 = BitConverter.GetBytes(reshead[j].ItmLen);
                for (int q = 0; q < b4.Length; q++)
                {
                    if (q >= 2) break;
                    rlbByte[pos + q] = b4[q];
                }
                pos += 2;
                rlbByte[pos] = 0;
                rlbByte[pos+1] = 0;
                pos += 2;

                for (int k = 0; k < reshead[j].ResLen; k++)
                {
                    rlbByte[pos + k] = resByte[j, k];
                }
                pos += reshead[j].ResLen;
            }

            rlbByteLen = pos;




            FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            BinaryWriter bw = new BinaryWriter(fs);
            for (int i = 0; i < rlbByteLen; i++)
            {
                bw.Write(rlbByte[i]);
            }

            bw.Close();
            fs.Close();
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            reshead[resselected].ItmCnt = Convert.ToInt32(textBox4.Text);
            reshead[resselected].ResId = Convert.ToInt32(textBox1.Text);
            reswidth[resselected] = Convert.ToInt32(textBox2.Text);
            resheight[resselected] = Convert.ToInt32(textBox3.Text);
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            resmax = 0;
            resselected = -1;
            respicmax = 0;
            respicselected = -1;
            saved = false;
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void MyOpen(string filename)
        {
            FileStream fs;
            fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            BinaryReader br = new BinaryReader(fs);
            for (int i = 0; i < fs.Length; i++)
            {
                rlbByte[i] = br.ReadByte();
            }
            rlbByteLen = (int)fs.Length;

            br.Close();
            fs.Close();

            listBox1.Items.Clear();
            InitRlb();

        }

        private void InitRlb()
        {
            //head
            for (int i = 0; i < 116; i++)
            {
                byte[] tb = new byte[4];
                tb[0] = rlbByte[i * 4];
                tb[1] = rlbByte[i * 4 + 1];
                tb[2] = rlbByte[i * 4 + 2];
                tb[3] = rlbByte[i * 4 + 3];
                resAddress[i] = BitConverter.ToInt32(tb, 0);
            }
            //资源
            for (int j = 0; j < 116; j++)
            {
                int adr = resAddress[j];
                if (adr < 0)
                {
                    listBox1.Items.Add(reshead[j].ResId + "-NONE");
                    continue;
                }
                byte[] b1 = new byte[4];
                b1[0] = rlbByte[adr];
                b1[1] = rlbByte[adr + 1];
                b1[2] = rlbByte[adr + 2];
                b1[3] = rlbByte[adr + 3];
                reshead[j].ResLen = BitConverter.ToInt32(b1, 0);
                adr += 4;
                byte[] b2 = new byte[4];
                b2[0] = rlbByte[adr];
                b2[1] = rlbByte[adr + 1];
                b2[2] = 0;
                b2[3] = 0;
                reshead[j].ResId = BitConverter.ToInt32(b2, 0);
                adr += 2;
                byte[] b3 = new byte[4];
                b3[0] = rlbByte[adr];
                b3[1] = rlbByte[adr + 1];
                b3[2] = 0;
                b3[3] = 0;
                reshead[j].ItmCnt = BitConverter.ToInt32(b3, 0);
                adr += 2;
                byte[] b4 = new byte[4];
                b4[0] = rlbByte[adr];
                b4[1] = rlbByte[adr + 1];
                b4[2] = 0;
                b4[3] = 0;
                reshead[j].ItmLen = BitConverter.ToInt32(b4, 0);
                adr += 2;

                adr += 2;
                for (int k = adr; k < reshead[j].ResLen + adr; k++)
                {
                    resByte[j, k - adr] = rlbByte[k];
                }

                

                listBox1.Items.Add(reshead[j].ResId +"");

                

            }
        }

        private void 添加占位符ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            respicselected = listBox2.SelectedIndex;
            int []picAry = { 4 ,5 ,7 ,8 ,9 ,15 ,16 ,26 ,28 ,29 ,30 ,31 ,32 ,33 ,34 ,44 ,45 ,46 ,47 ,48, 49 ,50, 51 ,54 ,55, 56 ,69, 75, 76};
            int []srsAry = { 3, 6 ,19, 20, 21, 22, 23 ,24, 25 ,27, 35 ,36, 37 ,38, 39 ,40, 41, 42, 43, 100,101,102,103,104,105,106,107};
            for (int i = 0; i < picAry.Length; i++)
            {
                if ((resselected+1) == picAry[i])
                {
                    if (isRlb == true)
                    {
                        ShowInBoxColor(resselected, listBox2.SelectedIndex);
                    }
                    else
                    {

                        ShowInBox(resselected, listBox2.SelectedIndex);
                    }
                }
            }
            for (int i = 0; i < srsAry.Length; i++)
            {
                if ((resselected + 1) == srsAry[i])
                {
                    int sidx = listBox2.SelectedIndex;
                    int startpos = 6;
                    byte[] b1 = new byte[4];
                    b1[0] = resByte[resselected,2];
                    b1[1] = 0;
                    b1[2] = 0;
                    b1[3] = 0;
                    int srscount = BitConverter.ToInt32(b1, 0);

                    byte[] b3 = new byte[4];
                    b3[0] = resByte[resselected,3];
                    b3[1] = 0;
                    b3[2] = 0;
                    b3[3] = 0;
                    int piccount = BitConverter.ToInt32(b3, 0);

                    startpos = startpos + 5 * srscount;

                    int picLen = 0;
                    for (int k = 0; k < sidx+1; k++)
                    {
                        if(k>0)startpos += 6;
                        startpos += picLen;
                        byte[] b2 = new byte[4];
                        b2[0] = resByte[resselected, startpos + 0];
                        b2[1] = resByte[resselected, startpos + 1];
                        b2[2] = 0;
                        b2[3] = 0;
                        int wid = BitConverter.ToInt32(b2, 0);

                        byte[] b4 = new byte[4];
                        b4[0] = resByte[resselected, startpos + 2];
                        b4[1] = resByte[resselected, startpos + 3];
                        b4[2] = 0;
                        b4[3] = 0;
                        int high = BitConverter.ToInt32(b4, 0);

                        byte[] b5 = new byte[4];
                        b5[0] = resByte[resselected, startpos + 5];
                        b5[1] = 0;
                        b5[2] = 0;
                        b5[3] = 0;
                        int mode = BitConverter.ToInt32(b5, 0);

                        if (isRlb)
                        {
                           

                            picLen = (wid * 24 + 31) / 32 * 4;//每行字节数
                            picLen *= high;
                            
                        }
                        else
                        {
                            
                           picLen = wid / 8;
                           if ((wid % 8) > 0)
                               picLen++;
                           picLen *= high;
                           picLen *= (mode + 1);
                        }

                        //picLen += 6;

                        
                        
                    }

                    if (isRlb)
                    {
                        
                        ShowSrsInBoxColor(resselected, startpos, listBox2.SelectedIndex);//, picLen);
                    }
                    else
                    {
                        ShowSrsInBox(resselected, startpos, listBox2.SelectedIndex);//, picLen);
                    }
                }
            }
        }

        private void ShowInBox(int index,int picno)
        {
            MyBitMap myBitMap = new MyBitMap();
            int picLen = resPicByteLen[picno]+6;//reshead[index].ResLen;
            for (int i = 6; i < picLen; i++)
            {
                int tmp = 255 - Convert.ToInt32(resPicByte[picno,i - 6]);//resByte[index,i]);
                myBitMap.biImage[i - 6] = Convert.ToByte(tmp);
            }


            myBitMap.bfSize = BitConverter.GetBytes(62 + picLen - 6);
            myBitMap.biSizeImage = BitConverter.GetBytes(picLen - 6);
            myBitMap.biWidth = BitConverter.GetBytes(Convert.ToInt32(resByte[index, 0]));
            myBitMap.biHeight = BitConverter.GetBytes(Convert.ToInt32(resByte[index, 2]));

            Image im = myBitMap.BytesToImage(picno);
            if (im == null) return;
            Graphics g = this.pictureBox1.CreateGraphics();
            g.Clear(Color.White);
            if (im.Width > pictureBox1.Width)
            {
                Rectangle destRect = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Width * 3 / 5);
                g.DrawImage(im, destRect);
            }
            else
            {
                g.DrawImage(im, 0, 0);
            }
            g.Dispose();
        }

        private void ShowInBoxColor(int index, int picno)
        {
            MyBitMap myBitMap = new MyBitMap();
            int picLen = resPicByteLen[picno] + 6;//reshead[index].ResLen;
            for (int i = 6; i < picLen; i++)
            {
                int tmp = Convert.ToInt32(resPicByte[picno, i - 6]);//resByte[index,i]);
                myBitMap.biImage[i - 6] = Convert.ToByte(tmp);
            }


            myBitMap.bfSize = BitConverter.GetBytes(54 + picLen - 6);
            myBitMap.biSizeImage = BitConverter.GetBytes(picLen - 6);
            byte[] b1 = new byte[4];
            b1[0] = resByte[index, 0];
            b1[1] = resByte[index, 1];
            b1[2] = 0;
            b1[3] = 0;
            int wid1 = BitConverter.ToInt32(b1, 0);
            byte[] b2 = new byte[4];
            b2[0] = resByte[index, 2];
            b2[1] = resByte[index, 3];
            b2[2] = 0;
            b2[3] = 0;
            int hgt2 = BitConverter.ToInt32(b2, 0);


            myBitMap.biWidth = BitConverter.GetBytes(wid1);
            myBitMap.biHeight = BitConverter.GetBytes(hgt2);
           
            Image im = myBitMap.BytesToImageColor(picno);
            Graphics g = this.pictureBox1.CreateGraphics();
            g.Clear(Color.White);
            g.DrawImage(im, 0, 0);
            g.Dispose();
        }

        private void ShowSrsInBox(int index, int startpos, int picno)//, int picLen)
        {
            MyBitMap myBitMap = new MyBitMap();
            int picLen = resPicByteLen[picno] + 6;
            
            for (int i = 6; i < picLen; i++)
            {
                int tmp = 255 - Convert.ToInt32(resPicByte[picno, i - 6]);//resByte[index, startpos + i]);
                myBitMap.biImage[i - 6] = Convert.ToByte(tmp);
            }


            myBitMap.bfSize = BitConverter.GetBytes(62 + picLen - 6);
            myBitMap.biSizeImage = BitConverter.GetBytes(picLen - 6);
            myBitMap.biWidth = BitConverter.GetBytes(Convert.ToInt32(resByte[index, startpos + 0]));
            myBitMap.biHeight = BitConverter.GetBytes(Convert.ToInt32(resByte[index, startpos + 2]));

            Image im = myBitMap.BytesToImage(picno);
            Graphics g = this.pictureBox1.CreateGraphics();
            g.Clear(Color.White);
            g.DrawImage(im, 0, 0);
            g.Dispose();
        }

        private void ShowSrsInBoxColor(int index, int startpos, int picno)
        {
            MyBitMap myBitMap = new MyBitMap();
            int picLen = resPicByteLen[picno] + 6;

            if (picLen > 2000000) return;
            for (int i = 6; i < picLen; i++)
            {
                
                int tmp = Convert.ToInt32(resPicByte[picno, i - 6]);//resByte[index, startpos + i]);
                myBitMap.biImage[i - 6] = Convert.ToByte(tmp);
            }


            myBitMap.bfSize = BitConverter.GetBytes(54 + picLen - 6);
            myBitMap.biSizeImage = BitConverter.GetBytes(picLen - 6);
            byte[] b1 = new byte[4];
            b1[0] = resByte[index, startpos + 0];
            b1[1] = resByte[index, startpos + 1];
            b1[2] = 0;
            b1[3] = 0;
            int wid1 = BitConverter.ToInt32(b1, 0);
            byte[] b2 = new byte[4];
            b2[0] = resByte[index, startpos + 2];
            b2[1] = resByte[index, startpos + 3];
            b2[2] = 0;
            b2[3] = 0;
            int hgt2 = BitConverter.ToInt32(b2, 0);


            myBitMap.biWidth = BitConverter.GetBytes(wid1);
            myBitMap.biHeight = BitConverter.GetBytes(hgt2);

            Image im = myBitMap.BytesToImageColor(picno);
            if (im == null) return;
            Graphics g = this.pictureBox1.CreateGraphics();
            g.Clear(Color.White);

            if (im.Width > pictureBox1.Width)
            {
                Rectangle destRect = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Width * 3 / 5);
                g.DrawImage(im, destRect);
            }
            else
            {
                g.DrawImage(im, 0,0);
            }
            g.Dispose();

        }
        private void 占位符ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add((resmax + 1) + "");
            reshead[resmax].ItmCnt = 0;
            reshead[resmax].ResLen = 12;//头部12
            reshead[resmax].ResId = resmax;
            resmax++;
        }

        private void 资源项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //listBox1.Items.Add
            listBox1.Items.Add((resmax + 1) + "");
            reshead[resmax].ItmCnt = 0;
            reshead[resmax].ResLen = 12;//头部12
            reshead[resmax].ResId = resmax;
            resmax++;
            
        }

        private void 图片项目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //listBox1.Items.Add
            listBox2.Items.Add((respicmax + 1) + "");
            respicmax++;
            
        }

        private void ChangePicByte(int sid,int spicidx)
        {
            

            for (int i = 6; i < tmpBmpByteLen+6-54; i++)
            {
                resPicByte[spicidx,  i] = tmpBmpByte[i + 54];
            }
            //resPicByteLen[spicidx] = tmpBmpByteLen + 6 - 54;

            //width
            resByte[sid, 0] = tmpBmpByte[18];
            resByte[sid, 1] = tmpBmpByte[19];

            //height
            resByte[sid, 2] = tmpBmpByte[22];
            resByte[sid, 3] = tmpBmpByte[23];

            byte[] b0 = new byte[4];
            b0[0] = tmpBmpByte[18];
            b0[1] = tmpBmpByte[19];
            b0[2] = 0;
            b0[3] = 0;
            int width = BitConverter.ToInt32(b0, 0);

            byte[] b2 = new byte[4];
            b2[0] = tmpBmpByte[22];
            b2[1] = tmpBmpByte[23];
            b2[2] = 0;
            b2[3] = 0;
            int height = BitConverter.ToInt32(b2, 0);

            byte[] b1 = new byte[4];
            b1[0] = resByte[sid ,4];
            b1[1] = 0;
            b1[2] = 0;
            b1[3] = 0;
            int piccount = BitConverter.ToInt32(b1, 0);
            if (resByte[sid, 5] > 0) piccount *= 2;

            int width2 = (width * 24 + 31) / 32 * 4;//每行字节数
            int byteCount = height * width2;

            resPicByteLen[spicidx] = byteCount;

            reshead[sid].ResLen = 0;

            int pos = 6;
            for (int i = 0; i < piccount; i++)
            {
                for (int j = 0; j < resPicByteLen[i]; j++)
                {
                    resByte[sid, pos] = resPicByte[i, j];
                    pos++;
                }
                reshead[sid].ResLen += resPicByteLen[i];
            }
            reshead[sid].ResLen += 6;
            //edited = true;
        }

        private void ChangeSrsPicByte(int sid, int spicidx)
        {

            for (int i = 0; i < tmpBmpByteLen + 6 - 54; i++)
            {
                resPicByte[spicidx, i] = tmpBmpByte[i + 54];
            }
            //resPicByteLen[spicidx] = tmpBmpByteLen + 6 - 54;


            int startpos = 6;
            byte[] b1 = new byte[4];
            b1[0] = resByte[resselected, 2];
            b1[1] = 0;
            b1[2] = 0;
            b1[3] = 0;
            int srscount = BitConverter.ToInt32(b1, 0);

            byte[] b3 = new byte[4];
            b3[0] = resByte[resselected, 3];
            b3[1] = 0;
            b3[2] = 0;
            b3[3] = 0;
            int piccount = BitConverter.ToInt32(b3, 0);

            startpos = startpos + 5 * srscount;

            reshead[sid].ResLen = startpos;

            



            int pos = startpos;
            for (int i = 0; i < piccount; i++)
            {
                if (i == spicidx)
                {
                    //width
                    resByte[sid, pos + 0] = tmpBmpByte[18];
                    resByte[sid, pos + 1] = tmpBmpByte[19];

                    //height
                    resByte[sid, pos + 2] = tmpBmpByte[22];
                    resByte[sid, pos + 3] = tmpBmpByte[23];

                    byte[] b0 = new byte[4];
                    b0[0] = tmpBmpByte[18];
                    b0[1] = tmpBmpByte[19];
                    b0[2] = 0;
                    b0[3] = 0;
                    int width = BitConverter.ToInt32(b0, 0);

                    byte[] b2 = new byte[4];
                    b2[0] = tmpBmpByte[22];
                    b2[1] = tmpBmpByte[23];
                    b2[2] = 0;
                    b2[3] = 0;
                    int height = BitConverter.ToInt32(b2, 0);


                    int width2 = (width * 24 + 31) / 32 * 4;//每行字节数
                    int byteCount = height * width2;

                    resPicByteLen[spicidx] = byteCount;// +6;

                }
                pos += 6;
                for (int j = 0; j < resPicByteLen[i]; j++)
                {
                    if (j >= 2000000) break;
                    resByte[sid, pos] = resPicByte[i, j];
                    pos++;
                }
                reshead[sid].ResLen += resPicByteLen[i];
            }
            
            //edited = true;
        }

        private void 添加ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                    string filename = openFileDialog1.FileName;
                    //File fileOpen = new File(fName);
                    FileStream fs;
                    fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                    BinaryReader br = new BinaryReader(fs);

                    for (int i = 0; i < fs.Length; i++)
                    {
                        tmpBmpByte[i] = br.ReadByte();
                    }
                    tmpBmpByteLen = (int)fs.Length;

                    br.Close();
                    fs.Close();

                    respicselected = listBox2.SelectedIndex;
                    //listBox2.Items.Add(respicselected + "");
                    //respicmax++;

                    ChangePicByte(resselected,respicselected);

                    //int curpos = reshead[resselected].ResLen;
                    //reshead[resmax].ItmCnt++;
                    //listBox2.Items.Add((resmax + 1) + "");
                    //resmax++;
              

            }
        }

        private void 特效图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox2.Items.Add((respicmax + 1) + "");
            respicmax++;
            //reshead[resselected].ItmCnt++;
        }

        private void 添加特效图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {

                string filename = openFileDialog1.FileName;
                //File fileOpen = new File(fName);
                FileStream fs;
                fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                BinaryReader br = new BinaryReader(fs);

                for (int i = 0; i < fs.Length; i++)
                {
                    tmpBmpByte[i] = br.ReadByte();
                }
                tmpBmpByteLen = (int)fs.Length;

                br.Close();
                fs.Close();

                respicselected = listBox2.SelectedIndex;
                //listBox2.Items.Add(respicselected + "");
                //respicmax++;

                ChangeSrsPicByte(resselected, respicselected);

                //int curpos = reshead[resselected].ResLen;
                //reshead[resmax].ItmCnt++;
                //listBox2.Items.Add((resmax + 1) + "");
                //resmax++;


            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            添加ToolStripMenuItem1_Click(sender, e);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            添加特效图片ToolStripMenuItem_Click(sender, e);
        }

        private void 批量添加普通图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {

                string[] filenames = openFileDialog3.FileNames;
                int residt = 0;
                foreach (string filename in filenames)
                {
                    //File fileOpen = new File(fName);
                    FileStream fs;
                    fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                    BinaryReader br = new BinaryReader(fs);

                    for (int i = 0; i < fs.Length; i++)
                    {
                        tmpBmpByte[i] = br.ReadByte();
                    }
                    tmpBmpByteLen = (int)fs.Length;

                    br.Close();
                    fs.Close();

                    respicselected = residt;
                    residt ++;
                    //listBox2.Items.Add(respicselected + "");
                    //respicmax++;

                    ChangePicByte(resselected, respicselected);

                    //int curpos = reshead[resselected].ResLen;
                    //reshead[resmax].ItmCnt++;
                    //listBox2.Items.Add((resmax + 1) + "");
                    //resmax++;
                }


            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            批量添加普通图片ToolStripMenuItem_Click(sender, e);
        }

        private void rLBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog2.FileName;
                //File fileOpen = new File(fName);
                MyOpen(filename);
                isRlb = true;
            }
        }

        private void lIBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog2.FileName;
                //File fileOpen = new File(fName);
                MyOpen(filename);
                isRlb = false;
            }
        }

        private void 图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 批量添加特效图片ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog3.ShowDialog() == DialogResult.OK)
            {
                string[] filenames = openFileDialog3.FileNames;
                int residt = 0;
                foreach (string filename in filenames)
                {
                    //string filename = openFileDialog1.FileName;
                    //File fileOpen = new File(fName);
                    FileStream fs;
                    fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                    BinaryReader br = new BinaryReader(fs);

                    for (int i = 0; i < fs.Length; i++)
                    {
                        tmpBmpByte[i] = br.ReadByte();
                    }
                    tmpBmpByteLen = (int)fs.Length;

                    br.Close();
                    fs.Close();

                    respicselected = residt;
                    residt++;
                    //listBox2.Items.Add(respicselected + "");
                    //respicmax++;

                    ChangeSrsPicByte(resselected, respicselected);

                    //int curpos = reshead[resselected].ResLen;
                    //reshead[resmax].ItmCnt++;
                    //listBox2.Items.Add((resmax + 1) + "");
                    //resmax++;
                }


            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            批量添加特效图片ToolStripMenuItem_Click(sender, e);
        }

    }

    public class RCHEAD
    {
        public int ResLen;			/* 资源串总长度 */
        public int ResId;			/* 资源串id */
        public int ItmCnt;			/* 资源项个数 */
        public int ItmLen;			/* 资源项长度(0表示资源项不等长，后面会有资源项索引序列) */
        public int ResKey;			/* 资源串加密钥匙(加密算法统一使用+ResKey) */
        public int Reserved;		/* 保留 */
    };				/* 资源串头定义 */

    public class RIDX
    {

        public int offset;			/* 资源项相对资源起始地址的偏移地址 */
        public int rlen;			/* 资源项的数据长度 */
    }   

    public class PictureHead
    {
        public int wid;		/*图片宽*/
        public int hig;		/*图片高*/
        public int count;		/*图片数*/
        public int mask;		/*高4为bpp；(mask & 0x01)为1时为透明图；(mask & 0x02)为0时为横向图，为1时为纵向图*/
    };

    public class MyBitMap//黑白：文件头(14字节)+信息头(40字节)+2个调色板(共8字节)+Height(图像高度)*(Width+8-Width%8)/8
    {

        //BMP文件头14
        public Byte[] bfType = { 0x42, 0x4d };
        public Byte[] bfSize = new Byte[4];
        public Byte[] bfReserved1 = { 0, 0 };
        public Byte[] bfReserved2 = { 0, 0 };
        public Byte[] bfOffBits = { 0x3e, 0, 0, 0 };

        //信息头40
        public Byte[] biSize = { 0x28, 0, 0, 0 }; // 本结构所占用字节数
        public Byte[] biWidth = new Byte[4]; // 位图的宽度，以像素为单位
        public Byte[] biHeight = new Byte[4]; // 位图的高度，以像素为单位
        public Byte[] biPlanes = { 1, 0 }; // 目标设备的级别，必须为1
        public Byte[] biBitCount = { 1, 0 };// 每个像素所需的位数，必须是1(双色),4(16色)，8(256色)或24(真彩色)之一
        public Byte[] biCompression = { 0, 0, 0, 0 }; // 位图压缩类型，必须是 0(不压缩)
        public Byte[] biSizeImage = new Byte[4]; // 位图的大小，以字节为单位.必须为4倍数
        public Byte[] biXPelsPerMeter = { 0xc4, 0x0e, 0, 0 }; // 位图水平分辨率，每米像素数
        public Byte[] biYPelsPerMeter = { 0xc4, 0x0e, 0, 0 }; // 位图垂直分辨率，每米像素数
        public Byte[] biClrUsed = { 0, 0, 0, 0 };// 位图实际使用的颜色表中的颜色数
        public Byte[] biClrImportant = { 0, 0, 0, 0 };// 位图显示过程中重要的颜色数

        //调色板8
        public Byte[] bmiColors = { 0, 0, 0, 0, 0xff, 0xff, 0xff, 0 };

        //位图数据 0-黑 1-白 Height(图像高度)*(Width+8-Width%8)/8
        public Byte[] biImage = new Byte[10485760];//19200];

        public Image BytesToImage()
        {
            return BytesToImage(0);
        }

        public Image BytesToImage(int no)
        {
            int count = BitConverter.ToInt32(bfSize, 0);
            int width = BitConverter.ToInt32(biWidth, 0);
            int height = BitConverter.ToInt32(biHeight, 0);
            byte[] buffer = new byte[16384];


            buffer[0] = 0x42;
            buffer[1] = 0x4d;
            buffer[2] = bfSize[0];
            buffer[3] = bfSize[1];
            buffer[4] = bfSize[2];
            buffer[5] = bfSize[3];
            buffer[6] = 0;
            buffer[7] = 0;
            buffer[8] = 0;
            buffer[9] = 0;
            buffer[10] = 0x3e;
            buffer[11] = 0;
            buffer[12] = 0;
            buffer[13] = 0;
            buffer[14] = 0x28;
            buffer[15] = 0;
            buffer[16] = 0;
            buffer[17] = 0;
            buffer[18] = biWidth[0];
            buffer[19] = biWidth[1];
            buffer[20] = biWidth[2];
            buffer[21] = biWidth[3];
            buffer[22] = biHeight[0];
            buffer[23] = biHeight[1];
            buffer[24] = biHeight[2];
            buffer[25] = biHeight[3];
            buffer[26] = 1;
            buffer[27] = 0;
            buffer[28] = 1;
            buffer[29] = 0;
            buffer[30] = 0;
            buffer[31] = 0;
            buffer[32] = 0;
            buffer[33] = 0;
            buffer[34] = biSizeImage[0];
            buffer[35] = biSizeImage[1];
            buffer[36] = biSizeImage[2];
            buffer[37] = biSizeImage[3];
            buffer[38] = 0xc4;
            buffer[39] = 0x0e;
            buffer[40] = 0;
            buffer[41] = 0;
            buffer[42] = 0xc4;
            buffer[43] = 0x0e;
            buffer[44] = 0;
            buffer[45] = 0;
            buffer[46] = 0;
            buffer[47] = 0;
            buffer[48] = 0;
            buffer[49] = 0;
            buffer[50] = 0;
            buffer[51] = 0;
            buffer[52] = 0;
            buffer[53] = 0;
            buffer[54] = 0;
            buffer[55] = 0;
            buffer[56] = 0;
            buffer[57] = 0;
            buffer[58] = 0xff;
            buffer[59] = 0xff;
            buffer[60] = 0xff;
            buffer[61] = 0;

            //
            byte[] buffer1 = new byte[16384];

            int width1 = width + 8 - width % 8;
            int tmp = 0;
            if ((width1 - width) % 8 == 0)
            {
                tmp = 1;
            }


            width = (width + 8 - width % 8) / 8;


            int width2 = 0;
            if ((width - tmp) % 4 != 0)
            {
                width2 = (width - tmp) + 4 - (width - tmp) % 4;
            }
            else
            {
                width2 = width - tmp;
            }

            //width = (width + 8 - width % 8) / 8;

            //if (no == 0)
            {
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        buffer1[(height - i - 1) * width2 + j] = biImage[i * (width - tmp) + j];
                    }
                }

            
                
            }
            /*else
            {
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        buffer1[(height - i - 1) * width2 + j] = biImage[i * (width - tmp) + j + no * ((width1 / 8 - tmp) * height)];
                    }
                }
            }*/

            for (int i = 62; i < (height - 1) * width2 + width2 + 62; i++)
            {
                //buffer[count - i + 61 ] = biImage[i - 62];
                buffer[i] = buffer1[i - 62];
            }


            MemoryStream ms = new MemoryStream(buffer);
            FileStream fs = File.Create("D:\\1.qq");
            fs.Write(buffer, 0, buffer.Length);


            Image image = System.Drawing.Image.FromStream(ms);

            ms.Close();
            fs.Close();
            return image;
        }

        public Image BytesToImageColor(int no)
        {
            int count = BitConverter.ToInt32(bfSize, 0);
            int width = BitConverter.ToInt32(biWidth, 0);
            int height = BitConverter.ToInt32(biHeight, 0);

            int width2 = (width * 24 + 31) / 32 * 4;//每行字节数
            int byteCount = height * width2;

            byte[] buffer = new byte[byteCount + 54];


            buffer[0] = 0x42;
            buffer[1] = 0x4d;
            buffer[2] = bfSize[0];
            buffer[3] = bfSize[1];
            buffer[4] = bfSize[2];
            buffer[5] = bfSize[3];
            buffer[6] = 0;
            buffer[7] = 0;
            buffer[8] = 0;
            buffer[9] = 0;
            buffer[10] = 0x36;
            buffer[11] = 0;
            buffer[12] = 0;
            buffer[13] = 0;
            buffer[14] = 0x28;
            buffer[15] = 0;
            buffer[16] = 0;
            buffer[17] = 0;
            buffer[18] = biWidth[0];
            buffer[19] = biWidth[1];
            buffer[20] = biWidth[2];
            buffer[21] = biWidth[3];
            buffer[22] = biHeight[0];
            buffer[23] = biHeight[1];
            buffer[24] = biHeight[2];
            buffer[25] = biHeight[3];
            buffer[26] = 1;
            buffer[27] = 0;
            buffer[28] = 24;
            buffer[29] = 0;
            buffer[30] = 0;
            buffer[31] = 0;
            buffer[32] = 0;
            buffer[33] = 0;
            buffer[34] = biSizeImage[0];
            buffer[35] = biSizeImage[1];
            buffer[36] = biSizeImage[2];
            buffer[37] = biSizeImage[3];
            buffer[38] = 0xc4;
            buffer[39] = 0x0e;
            buffer[40] = 0;
            buffer[41] = 0;
            buffer[42] = 0xc4;
            buffer[43] = 0x0e;
            buffer[44] = 0;
            buffer[45] = 0;
            buffer[46] = 0;
            buffer[47] = 0;
            buffer[48] = 0;
            buffer[49] = 0;
            buffer[50] = 0;
            buffer[51] = 0;
            buffer[52] = 0;
            buffer[53] = 0;
            

            //
            byte[] buffer1 = new byte[byteCount];

            int width1 = width ;
            


            width = width * 3;



            no = 0;
            if (no == 0)
            {
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        buffer1[i * width2 + j] = biImage[i * width + j];
                    }
                }



            }
            else
            {
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        buffer1[i * width2 + j] = biImage[i * width + j + no * width * height];
                    }
                }
            }

            for (int i = 54; i < byteCount+54; i++)
            {
                //buffer[count - i + 61 ] = biImage[i - 62];
                buffer[i] = buffer1[i - 54];
            }

            try
            {

                MemoryStream ms = new MemoryStream(buffer);
                //FileStream fs = File.Create("D:\\1.qq");
                //fs.Write(buffer, 0, buffer.Length);


                Image image = System.Drawing.Image.FromStream(ms);

                ms.Close();
                //fs.Close();
                return image;
            }
            catch { }
            return null;
        }


    }
}
