Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <summary>
        /// 缩放
        /// </summary>
        /// <param name="inSize">绘制尺寸</param>
        /// <param name="location_mode">针迹位置:保持不变 = "default";窗口左上 = "left_top";窗口中心 = "window_center";鼠标中心 = "mouse_center";</param>
        private void Zooz(Size inSize, string location_mode)
        {
            #region 缩放限定
            if (inSize.Width > 20480 || inSize.Height > 20480)
            {
                MessageBox.Show("分辨率超出最大支持范围:20480 x 20480");
                return;
            }
            else if (inSize.Width < 50 || inSize.Height < 50)
            {
                MessageBox.Show("分辨率超出最小支持范围:50 x 50");
                return;
            }
            #endregion 缩放限定

            drawTimer.Stop();                                                                                                                                                 // 延时开关
            oldSize          = pictureBox.Size;                                                                                                                               // 旧尺寸
            oldMouseLocation = new Point(Cursor.Position.X - Location.X - frameSize - picPanel.Location.X, Cursor.Position.Y - Location.Y - titleSize - picPanel.Location.Y); // 旧光标在窗口坐标
            oldLocation      = pictureBox.Location;                                                                                                                           // 旧坐标

            #region 新尺寸
            zoozX = (float)inSize.Width / (float)dst.Size.Width;
            zoozY = (float)inSize.Height / (float)dst.Size.Height;
            if (zoozX > zoozY)
            {
                zoozZ = zoozY;               // 宽的倍大时,以高的倍数放大
            }
            else if (zoozY > zoozX)
            {
                zoozZ = zoozX;                    // 高的倍大时,以宽的倍数放大
            }
            else if (zoozY == zoozX)
            {
                zoozZ = zoozY = zoozX;                                                                 // 前面已排除1:1模式,理论上不存在zoozY == zoozX情况,但数值太大进行Float转换时,会出现误差,有概率发生此情况
            }
            newSize         = new Size((int)(dst.Size.Width * zoozZ), (int)(dst.Size.Height * zoozZ)); // 得到绘制尺寸
            pictureBox.Size = newSize;
            #endregion 新尺寸

            #region 新坐标
            if (location_mode == "pic_center")
            {
                newLocation = new Point((picPanel.Width - pictureBox.Width) / 2, (picPanel.Height - pictureBox.Height) / 2); // 图片中心对齐窗口中心
            }
            else if (location_mode == "mouse_center")                                                                        // 鼠标中心缩放 zoozSize的值分正负
            {
                liftTopSize = new Size(oldMouseLocation.X - oldLocation.X, oldMouseLocation.Y - oldLocation.Y);              // 缩放前,鼠标左上区域所占尺寸
                leftZooz    = (float)liftTopSize.Width / (float)oldSize.Width;                                               // 缩放前,缩放点左边占总宽百分比
                topZooz     = (float)liftTopSize.Height / (float)oldSize.Height;                                             // 缩放前,缩放点上边占总高百分比
                zoozSize    = new Size(pictureBox.Width - oldSize.Width, pictureBox.Height - oldSize.Height);                // 增或减的尺寸,增为正,减为负
                newLocation = new Point(oldLocation.X - (int)(leftZooz * zoozSize.Width), oldLocation.Y - (int)(topZooz * zoozSize.Height));
            }
            else if (location_mode == "left_top")
            {
                newLocation = new Point(0, 0);                                                                   // 对齐窗口左上
            }
            else if (location_mode == "windows_center")                                                          // 窗口中心缩放 zoozSize的值分正负
            {
                liftTopSize = new Size(picPanel.Width / 2 - oldLocation.X, picPanel.Height / 2 - oldLocation.Y); // 缩放前,缩放点左上区域所占尺寸
                leftZooz    = (float)liftTopSize.Width / (float)oldSize.Width;                                   // 缩放前,缩放点左边占总宽百分比
                topZooz     = (float)liftTopSize.Height / (float)oldSize.Height;                                 // 缩放前,缩放点上边占总高百分比
                zoozSize    = new Size(pictureBox.Width - oldSize.Width, pictureBox.Height - oldSize.Height);    // 增或减的尺寸,增为正,减为负
                newLocation = new Point(oldLocation.X - (int)(leftZooz * zoozSize.Width), oldLocation.Y - (int)(topZooz * zoozSize.Height));
            }
            else if (location_mode == "default")
            {
            }                                       // 不变
            else
            {
                MessageBox.Show("绘图时位置信息传入错误,坐标保持不变");
            }
            pictureBox.Location = newLocation;
            #endregion 新坐标

            if (oldSize != newSize)
            {
                drawTimer.Start();                    // 绘制
            }
            /// 信息
            infoLabel.Text = "带号:" + dst.FileName + " | 针数:" + dst.StitchCount + " | 颜色数:" + dst.ColorCount + " | 宽:" + dst.MmSize.Width + "mm | 高:" + dst.MmSize.Height + "mm | 起针点:X=" + (dst.Locations[0].X - dst.MinX) / 10 + "mm Y=" + (dst.Locations[0].Y - dst.MinY) / 10 + "mm | 缩放倍数:" + (zoozZ / Pixels.Get()).ToString("F2");
        }
Beispiel #3
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);
        }