/// <summary> /// 打开 /// </summary> private void openButton_Click(object sender, EventArgs e) { OpenFileDialog openfiledialog = new OpenFileDialog(); openfiledialog.Filter = "刺绣文件(*.dst)|*.dst"; // 文件类型 openfiledialog.DefaultExt = "dst"; // 默认格式 if (openfiledialog.ShowDialog() != DialogResult.OK) { return; } dst = DstDecode.LoadFile(openfiledialog.FileName); Zooz(picPanel.Size, "pic_center"); /// DirectoryInfo directorys = new DirectoryInfo(Path.GetDirectoryName(openfiledialog.FileName)); files = directorys.GetFiles(@"*.dst", SearchOption.TopDirectoryOnly);// 扫描 if (files.Length > 1) { previousButton.Enabled = true; nextButton.Enabled = true; } else { previousButton.Enabled = false; nextButton.Enabled = false; } }
/// <summary> /// 生成EMF文件 /// </summary> /// <param name="dst">DST实例</param> /// <param name="displayJump">显示跳针</param> /// <returns></returns> public static string Save(DstFile dst, bool displayJump) { string emf = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), dst.FileName + ".emf"); Bitmap bmp = new Bitmap(dst.OneSize.Width, dst.OneSize.Height);// 创建图像 Graphics graphicsBmp = Graphics.FromImage(bmp); graphicsBmp.SmoothingMode = SmoothingMode.AntiAlias;// 绘图质量 Metafile metafile = new Metafile(emf, graphicsBmp.GetHdc()); Graphics raphicsMef = Graphics.FromImage(metafile); Point start, end; // 起点终点坐标 Pen pen = new Pen(dst.ColouPlate[0], 1); // 画笔 int corCount = 1;; // 换色次数 float pixels = Pixels.Get(); for (int i = 0; i < dst.Locations.Count - 1; i++) // 绘制 { if (dst.ColorChange[i]) // 换色 { pen.Color = dst.ColouPlate[corCount]; corCount++; } if (dst.StitchJump[i]) { if (displayJump) { continue; // 跳针 } } start = dst.Locations[i]; end = dst.Locations[i + 1]; raphicsMef.DrawLine(pen, (int)((start.X - dst.MinX) * pixels), (int)((start.Y - dst.MinY) * pixels), (int)((end.X - dst.MinX) * pixels), (int)((end.Y - dst.MinY)) * pixels); } raphicsMef.Save(); raphicsMef.Dispose(); metafile.Dispose(); graphicsBmp.Dispose(); return(emf); }
/// <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); }