private void outputBinaryFile(String fileName) { try { BinaryWriter bw; PicHead ph = creatPicHead(); if (ph.len > 0x300000 - 0x400 * 64) { MessageBox.Show("文件大小超出3M-64K!!"); return; } String filePath = fileName + ".pic"; eraseFile(filePath); FileInfo fi = new FileInfo(filePath); using (bw = new BinaryWriter(fi.OpenWrite())) { bw.Write(toBytes(ph)); bw.Flush(); bw.Close(); } } catch (Exception e) { MessageBox.Show(e.Message.ToString(), "Warning!"); } }
private byte[] toBytes(PicHead ph) { while (ph.len % 256 != 0) { ph.len++; } byte[] bt = new byte[ph.len]; int j = 0; for (int i = 0; i < ph.tag.Length; i++) { bt[j++] = ph.tag[i]; } /* * for (int i = 0; i < ph.crc.Length; i++) * { * bt[j++] = ph.crc[i]; * } * * for (int i = 0; i < ph.thisAddr.Length; i++) * { * bt[j++] = ph.thisAddr[i]; * } * for(int i = 0; i < 0x10; i++) * { * bt[j++] = 0xFF; * } */ for (int i = 0; i < ph.picAddr.Length; i++) { bt[j++] = ph.picAddr[i]; } for (int i = 0; i < ph.lenDatas; i++) { bt[j++] = ph.datas[i]; } for (; j < ph.len; j++) { bt[j] = 0xFF; } return(bt); }
private PicHead creatPicHead() { PicHead ph = new PicHead(); ph.tag = Encoding.ASCII.GetBytes("Reserved........"); ph.crc = Encoding.ASCII.GetBytes("**"); ph.thisAddr = toBytes(0x1000); //Encoding.ASCII.GetBytes("1234"); ph.picAddr = new byte[0x100 * 4]; ph.datas = new byte[0x800000 - 0x410]; UInt32 tmp = 0x410; pictureBoxCurrentPic.Image = Image.FromFile(openFileNames[0]); UInt16[] colorsHex; int j = 0; int tempDataLen = 0; for (int i = 0; i < fileSum; i++) { ph.picAddr[j++] = (byte)(tmp >> 24); ph.picAddr[j++] = (byte)(tmp >> 16); ph.picAddr[j++] = (byte)(tmp >> 8); ph.picAddr[j++] = (byte)(tmp); colorsHex = SavePixels(); tmp += (UInt32)colorsHex.Length * 2; for (int l = 0; l < colorsHex.Length; l++) { ph.datas[tempDataLen++] = (byte)(colorsHex[l] >> 8); ph.datas[tempDataLen++] = (byte)(colorsHex[l]); } if (i < fileSum - 1) { pictureBoxCurrentPic.Image = Image.FromFile(openFileNames[i + 1]); } } ph.lenDatas = (UInt32)(tempDataLen); ph.len = (UInt32)((UInt32)ph.tag.Length + (UInt32)ph.crc.Length + (UInt32)ph.thisAddr.Length + (UInt32)ph.picAddr.Length + ph.lenDatas); return(ph); }