Ejemplo n.º 1
0
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="inPath">文件路径</param>
        public static DstFile LoadFile(string inPath)
        {
            FileStream fileStream = File.OpenRead(inPath);// 读取文件到Byte

            byte[] header = new byte[123];
            fileStream.Read(header, 0, header.Length);                                                                         // 提取有信息的文件头
            byte[] tempByte = new byte[389];
            fileStream.Read(tempByte, 0, tempByte.Length);                                                                     // 清除没意义的文件头
            byte[] stitch = new byte[fileStream.Length - header.Length - tempByte.Length - 3 - (fileStream.Length - 512) % 3]; // 总长 - 512 - 3 - 总长除3余数
            fileStream.Read(stitch, 0, stitch.Length);                                                                         // 提取针迹
            byte[] end = new byte[fileStream.Length - header.Length - stitch.Length];
            fileStream.Read(end, 0, end.Length);                                                                               // 提取结束符
            fileStream.Close();
            ///
            DstFile dst = new DstFile();

            dst.FilePath = inPath;
            dst.FileName = Path.GetFileNameWithoutExtension(inPath);
            ///
            try
            {
                string json = "{" + Encoding.Default.GetString(header);// 文件头转换Json
                json  = json.Replace(" ", "").Replace("\r", "").Replace("LA:", "\"LA\": \"").Replace("ST:", "\",\"ST\": \"").Replace("CO:", "\",\"CO\": \"").Replace("+X:", "\",\"+X\": \"").Replace("-X:", "\",\"-X\": \"").Replace("+Y:", "\",\"+Y\": \"").Replace("-Y:", "\",\"-Y\": \"").Replace("AX:", "\",\"AX\": \"").Replace("AY:", "\",\"AY\": \"").Replace("MX:", "\",\"MX\": \"").Replace("MY:", "\",\"MY\": \"").Replace("PD:", "\",\"PD\": \"");
                json += "\"}";
                JObject messages = JObject.Parse(json);
                //name = (string)messages["LA"];
                dst.StitchCount = (int)messages["ST"];
                dst.ColorCount  = (int)messages["CO"] + 1;
                dst.ColouPlate  = RandomColor.Get(dst.ColorCount);
                dst.MaxX        = (int)(messages["+X"]);
                dst.MinX        = (int)(messages["-X"]);
                dst.MaxY        = (int)(messages["+Y"]);
                dst.MinY        = (int)(messages["-Y"]);
                dst.StartX      = (int)(messages["AX"]);
                dst.EndX        = (int)(messages["MX"]);
                dst.StartY      = (int)(messages["AY"]);
                dst.EndY        = (int)(messages["MY"]);
                dst.Size        = new Size(dst.MaxX + dst.MinX, dst.MaxY + dst.MinY);
                dst.MmSize      = new Size((dst.MaxX + dst.MinX) / 10, (dst.MaxY + dst.MinY) / 10);
                dst.OneSize     = new Size((int)(dst.Size.Width * Pixels.Get()), (int)(dst.Size.Height * Pixels.Get()));
            }
            catch
            {
                MessageBox.Show("文件格式错误");
                return(null);
            }
            ///
            byte byte1, byte2, byte3;// 每3个字节为一组坐标

            dst.MinX = dst.MinY = 0;
            Point point  = new Point(0, 0);// 初始坐标
            float pixels = Pixels.Get();

            for (int i = 0; i < stitch.Length; i += 3)
            {
                byte1    = stitch[i];
                byte2    = stitch[i + 1];
                byte3    = stitch[i + 2];
                point.X += Bit(byte1, 0) - Bit(byte1, 1) + Bit(byte1, 2) * 9 - Bit(byte1, 3) * 9 + Bit(byte2, 0) * 3 - Bit(byte2, 1) * 3 + Bit(byte2, 2) * 27 - Bit(byte2, 3) * 27 + Bit(byte3, 2) * 81 - Bit(byte3, 3) * 81;
                point.Y -= Bit(byte1, 7) - Bit(byte1, 6) + Bit(byte1, 5) * 9 - Bit(byte1, 4) * 9 + Bit(byte2, 7) * 3 - Bit(byte2, 6) * 3 + Bit(byte2, 5) * 27 - Bit(byte2, 4) * 27 + Bit(byte3, 5) * 81 - Bit(byte3, 4) * 81;
                if (point.X < dst.MinX)
                {
                    dst.MinX = point.X;                    // 坐标负值修正
                }
                if (point.Y < dst.MinY)
                {
                    dst.MinY = point.Y;                    // 坐标负值修正
                }
                dst.Locations.Add(point);                  // 坐标
                dst.ColorChange.Add((Bit(byte3, 6) == 1)); // 换色
                dst.StitchJump.Add((Bit(byte3, 7) == 1));  // 跳针
            }
            return(dst);
        }