private void KopSoftPrint_Load(object sender, EventArgs e) { string sDefault = printDocument.PrinterSettings.PrinterName; //设置默认打印机名 foreach (string sPrint in PrinterSettings.InstalledPrinters) //获取所有打印机名称 { cbPrint.Items.Add(sPrint); if (sPrint == sDefault) { cbPrint.SelectedIndex = cbPrint.Items.IndexOf(sPrint); } } try { PageMargins = int.Parse(tbPageMargins.Text);//页面边距 PagerSetting.PagePadding = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageMargins, 1); PageWidth = int.Parse(tbPageWidth.Text); PageHeight = int.Parse(tbPageHeight.Text); if (PageWidth > 0 && PageHeight > 0)//进行尺码单位毫米(mm)转换成像素(Px) { PagerSetting.PageHeight = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageHeight, 2); PagerSetting.PageWidth = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageWidth, 1); } //重绘pager纸张 PageLayer pl = new PageLayer(PagerSetting.PageWidth, PagerSetting.PageHeight, PagerSetting.PagePadding); //先把旧纸张去掉 if (panel1.Controls.Count > 0) { for (int i = 0; i < panel1.Controls.Count; i++) { if (((ImageLayerBase)panel1.Controls[i]).layerType == LayerType.Paper) { panel1.Controls.RemoveAt(i); break; } } } panel1.Controls.Add(pl); PagerSetting.IsPagerLayerCreated = true; //计算控件起始坐标 int startX = (int)((panel1.Width - PagerSetting.PageWidth) / 2); int startY = (int)((panel1.Height - PagerSetting.PageHeight) / 2); pl.Left = startX; pl.Top = startY; m_startX = startX + PagerSetting.PagePadding; m_startY = startY + PagerSetting.PagePadding; lbMessage.Text = "设置成功,纸张尺寸" + PagerSetting.PageWidth + "*" + PagerSetting.PageHeight + "px 页面边距" + PagerSetting.PagePadding + "px"; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 构造函数 /// </summary> /// <param name="content">内容</param> /// <param name="width">长</param> /// <param name="height">高</param> /// <param name="codetype">条码类型 1:二维码 2:条形码</param> public BarCodeLayer(string content, int width, int height, int codetype, bool show) : base(LayerType.BarCode) { this.Width = width; this.Height = height; this.VarContent = content; this.CodeType = codetype; this.Content = PagerSetting.Translate(content); this.IsShowText = show; }
private void Tbox_Leave(object sender, EventArgs e) { base.OnLeave(e); Tbox.SendToBack(); this.Content = PagerSetting.Translate(Tbox.Text); this.VarContent = Tbox.Text; //这里要还原 TLabel.Text = this.Content; Tbox.Width = TLabel.Width; this.Width = TLabel.Width + 20; }
private void tbPageHeight_TextChanged(object sender, EventArgs e) { try { PageMargins = int.Parse(tbPageMargins.Text);//页面边距 PagerSetting.PagePadding = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageMargins, 1); PageWidth = int.Parse(tbPageWidth.Text); PageHeight = int.Parse(tbPageHeight.Text); if (PageWidth > 0 && PageHeight > 0)//进行尺码单位毫米(mm)转换成像素(Px) { PagerSetting.PageHeight = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageHeight, 2); PagerSetting.PageWidth = (int)PagerSetting.MillimeterToPixel(panel1.Handle, PageWidth, 1); } //重绘pager纸张 PageLayer pl = new PageLayer(PagerSetting.PageWidth, PagerSetting.PageHeight, PagerSetting.PagePadding); //先把旧纸张去掉 if (panel1.Controls.Count > 0) { for (int i = 0; i < panel1.Controls.Count; i++) { if (((ImageLayerBase)panel1.Controls[i]).layerType == LayerType.Paper) { panel1.Controls.RemoveAt(i); break; } } } panel1.Controls.Add(pl); PagerSetting.IsPagerLayerCreated = true; //计算控件起始坐标 int startX = (int)((panel1.Width - PagerSetting.PageWidth) / 2); int startY = (int)((panel1.Height - PagerSetting.PageHeight) / 2); pl.Left = startX; pl.Top = startY; m_startX = startX + PagerSetting.PagePadding; m_startY = startY + PagerSetting.PagePadding; lbMessage.Text = "设置成功,纸张尺寸" + PagerSetting.PageWidth + "*" + PagerSetting.PageHeight + "px 页面边距" + PagerSetting.PagePadding + "px"; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void init(string content, bool isSetFont, Font font, Color color) { this.VarContent = content; //重点属性:内容 this.BackColor = Color.White; this.Content = PagerSetting.Translate(content); //转换后的内容 if (isSetFont) { this.Font = font; //重点属性:字体 this.ForeColor = color; //重点属性:颜色 //另外 重点属性:坐标 实时计算 } TLabel = new Label(); TLabel.AutoSize = true; TLabel.Location = new Point(0, 0); TLabel.Text = this.Content; if (isSetFont) { TLabel.Font = font; TLabel.ForeColor = color; } TLabel.BackColor = Color.White; this.Controls.Add(TLabel); Tbox = new TextBox(); Tbox.Location = new Point(0, 0); Tbox.BorderStyle = BorderStyle.None; Tbox.Width = TLabel.Width; if (isSetFont) { Tbox.Font = font; Tbox.ForeColor = color; } Tbox.Text = this.VarContent; this.Controls.Add(Tbox); this.Height = TLabel.Height; this.Width = TLabel.Width + 20; TLabel.BringToFront(); TLabel.Click += TLabel_Click; Tbox.Leave += Tbox_Leave; Tbox.KeyDown += Tbox_KeyDown; }
public static Image Draw(PrintConfig cfg) { if (cfg == null || cfg.Layers == null || cfg.Layers.Count == 0) { return(null); } //根据配置还原图 Image img = new Bitmap(cfg.PageWidth, cfg.PageHeight); Graphics g = Graphics.FromImage(img); for (int i = cfg.Layers.Count - 1; i >= 0; i--) { string layertype = cfg.Layers[i].GetType().ToString(); switch (layertype.Substring(layertype.LastIndexOf(".") + 1)) { case "BarCodeLayerConfig": BarCodeLayerConfig bc = (BarCodeLayerConfig)cfg.Layers[i]; BarcodeFormat myBarcodeFormat; EncodingOptions myEncoding; if (bc.CodeType == 1) //二维码 { myBarcodeFormat = BarcodeFormat.QR_CODE; myEncoding = new QrCodeEncodingOptions() { Height = bc.Height, Width = bc.Width, Margin = 0, CharacterSet = "UTF-8", PureBarcode = !bc.IsShowText }; } else //条形码 { myBarcodeFormat = BarcodeFormat.CODE_128; myEncoding = new EncodingOptions() { Height = bc.Height, Width = bc.Width, Margin = 0, PureBarcode = !bc.IsShowText }; } BarcodeWriter writer = new BarcodeWriter { Format = myBarcodeFormat, Options = myEncoding, Renderer = (IBarcodeRenderer <Bitmap>)Activator.CreateInstance(typeof(BitmapRenderer)) }; Bitmap barImg = writer.Write(PagerSetting.Translate(bc.Content)); g.DrawImage(barImg, bc.X, bc.Y, bc.Width, bc.Height); break; case "ImageLayerConfig": ImageLayerConfig imglc = (ImageLayerConfig)cfg.Layers[i]; g.DrawImage(Image.FromFile(imglc.ImageFilePath), imglc.X, imglc.Y, imglc.Width, imglc.Height); break; case "TextLayerConfig": TextLayerConfig tlc = (TextLayerConfig)cfg.Layers[i]; //g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;//文本失真 g.DrawString(PagerSetting.Translate(tlc.Content), tlc.ContentFont, new SolidBrush(tlc.ContentColor), new PointF(tlc.X, tlc.Y)); break; case "LineLayerConfig": LineLayerConfig lc = (LineLayerConfig)cfg.Layers[i]; using (Pen p = new Pen(Color.Black, lc.lineWidth)) { if (lc.lineDirect == 1) //横 { g.DrawLine(p, lc.X, lc.Y, lc.X + lc.lineLength, lc.Y); } else //竖 { g.DrawLine(p, lc.X, lc.Y, lc.X, lc.Y + lc.lineLength); } } break; default: break; } } return(img); }