private void ExcutPrintLabel(object sender, PrintPageEventArgs e) { string sBarcode = tbItemBarcode.Text.Trim(); string sProductNo = tbProductNo.Text.Trim(); string sItemNo = tbItemNo.Text.Trim(); string sItemDesc = tbItemDesc.Text.Trim(); string sSnNo = tbSnNo.Text.Trim(); //商品号ProductNo Dm二维码 DataMatrix.net.DmtxImageEncoder dmtxEncoder = new DataMatrix.net.DmtxImageEncoder(); Bitmap bmpDmtxCode = dmtxEncoder.EncodeImage(sProductNo); //商品号的条形码(不用打印条形码) Common.Code128 code128 = new Common.Code128(); Bitmap bmpProductNo = code128.GetCodeImage(sProductNo, Common.Code128.Encode.Code128C); Regex r = new Regex(@"^\d+$"); Common.Code128.Encode encode128 = Common.Code128.Encode.Code128A; //商品条码 Bitmap bmpBarcode; if (r.Match(sBarcode).Success) { if ((sBarcode.Length % 2) == 0) { encode128 = Common.Code128.Encode.Code128C; } } bmpBarcode = code128.GetCodeImage(sBarcode, encode128); //产品号(不用打印条码) encode128 = Common.Code128.Encode.Code128A; if (r.Match(sItemNo).Success) { if ((sItemNo.Length % 2) == 0) { encode128 = Common.Code128.Encode.Code128C; } } Bitmap bmpItemNo = code128.GetCodeImage(sItemNo.ToUpper(), encode128); //sn号的条码 Bitmap bmpSnNo = code128.GetCodeImage(sProductNo, Common.Code128.Encode.Code128C); if (cbxIsGenSn.Checked) { bmpSnNo = code128.GetCodeImage(sSnNo, Common.Code128.Encode.Code128C); } sSnNo = "SN : " + tbSnNo.Text.Trim(); //sn描述 //位置偏移量 const float PAGE_START_OFFSET_X = 2; const float PAGE_START_OFFSET_Y = 2; PointF pOffset = new PointF(PAGE_START_OFFSET_X, PAGE_START_OFFSET_Y); PointF pointBarcode = new PointF(pOffset.X + pbBarcode.Location.X, pOffset.Y + pbBarcode.Location.Y); PointF pointBarcodeDesc = new PointF(pOffset.X + pntBarcode.Location.X, pOffset.Y + pntBarcode.Location.Y); PointF pointItemNoDesc = new PointF(pOffset.X + pntItemNo.Location.X, pOffset.Y + pntItemNo.Location.Y); PointF pointItemDesc = new PointF(pOffset.X + pntItemDesc.Location.X, pOffset.Y + pntItemDesc.Location.Y); PointF pointDmtxCode = new PointF(pOffset.X + pbDmProductNo.Location.X, pOffset.Y + pbDmProductNo.Location.Y); PointF pointProductNoDesc = new PointF(pOffset.X + pntProductNo.Location.X, pOffset.Y + pntProductNo.Location.Y); PointF pointSnNoCode = new PointF(pOffset.X + pbSnNo.Location.X, pOffset.Y + pbSnNo.Location.Y); PointF pointSnNoDesc = new PointF(pOffset.X + pntSnNo.Location.X, pOffset.Y + pntSnNo.Location.Y); //return; //-----------打印货位文字---------- Brush brushDefaultFont = new SolidBrush(Color.Black); Font ArialFontSmall = new Font("Arial Bold", 9); Font ArialFontSn = new Font("Arial", 6); Font ArialFontBig = new Font("Arial Bold", 9); Font ArialFontDesc = new Font("Arial,宋体", 9); Graphics g = e.Graphics; //-----------打印条码-------------- g.DrawImage(bmpBarcode, pointBarcode.X, pointBarcode.Y, bmpBarcode.Width, pbBarcode.Height); g.DrawString(sBarcode, ArialFontSmall, brushDefaultFont, pointBarcodeDesc); //条码文字 //ItemNo描述 g.DrawString(sItemNo, ArialFontSn, brushDefaultFont, pointItemNoDesc); //文字 //ItemDesc const int LINE_CHARS = 8; string sTemp = ""; if (sItemDesc.Length > LINE_CHARS) { sTemp = sItemDesc.Substring(0, LINE_CHARS); } string sTemp2 = ""; g.DrawString(sTemp, ArialFontDesc, brushDefaultFont, pointItemDesc); //条码文字 if (sItemDesc.Length > LINE_CHARS) { sTemp2 = sItemDesc.Substring(LINE_CHARS, (sItemDesc.Length - LINE_CHARS)); pointItemDesc.Y = pointItemDesc.Y + 12; g.DrawString(sTemp2, ArialFontDesc, brushDefaultFont, pointItemDesc); //条码文字 } //datamatrix二维码 g.DrawImage(bmpDmtxCode, pointDmtxCode.X, pointDmtxCode.Y, bmpDmtxCode.Width, bmpDmtxCode.Height); g.DrawString(sProductNo, ArialFontSmall, brushDefaultFont, pointProductNoDesc); //条码文字 //--------------------------------- if (cbxIsGenSn.Checked) { //-----------打印sn条码-------------- g.DrawImage(bmpSnNo, pointSnNoCode.X, pointSnNoCode.Y, bmpSnNo.Width, pbSnNo.Height); g.DrawString(sSnNo, ArialFontSn, brushDefaultFont, pointSnNoDesc); //条码文字 } //--------------------------------- }
private void btnPrint_Click(object sender, EventArgs e) { string sBarcode = tbItemBarcode.Text.Trim(); string sProductNo = tbProductNo.Text.Trim(); string sItemNo = tbItemNo.Text.Trim(); string sItemDesc = tbItemDesc.Text.Trim(); string sSnNo = tbSnNo.Text.Trim(); pntProductNo.Text = sProductNo; pntBarcode.Text = sBarcode; pntSnNo.Text = "SN : " + sSnNo; pntItemDesc.Text = tbItemDesc.Text; pntItemNo.Text = sItemNo; DataMatrix.net.DmtxImageEncoder dmtxEncoder = new DataMatrix.net.DmtxImageEncoder(); Bitmap bmpDmtxCode = dmtxEncoder.EncodeImage(sProductNo); Common.Code128 code128 = new Common.Code128(); Bitmap bmpProductNo = code128.GetCodeImage(sProductNo, Common.Code128.Encode.Code128C); Bitmap bmpSnNo = code128.GetCodeImage(sProductNo, Common.Code128.Encode.Code128C); if (cbxIsGenSn.Checked) { bmpSnNo = code128.GetCodeImage(sSnNo, Common.Code128.Encode.Code128C); } Regex r = new Regex(@"^\d+$"); Common.Code128.Encode encode128 = Common.Code128.Encode.Code128A; Bitmap bmpBarcode; //if ((r.Match(sBarcode).Success) && (sBarcode.Length == 13)) //{ // Common.Ean13 ean13 = new Common.Ean13(); // Graphics g = new .CreateGraphics(); // var a = panelContent.CreateGraphics(); // var rectangleObj = new Rectangle(20, 20, 200, 50); // core.Paint_EAN13("211812100001", a, rectangleObj); //} //else { if (r.Match(sBarcode).Success) { if ((sBarcode.Length % 2) == 0) { encode128 = Common.Code128.Encode.Code128C; } } bmpBarcode = code128.GetCodeImage(sBarcode, encode128); } encode128 = Common.Code128.Encode.Code128A; if (r.Match(sItemNo).Success) { if ((sItemNo.Length % 2) == 0) { encode128 = Common.Code128.Encode.Code128C; } } Bitmap bmpItemNo = code128.GetCodeImage(sItemNo.ToUpper(), encode128); //bmpProductNo = Common.BitmapScaleHelper.ScaleToSize(bmpProductNo,2); //缩放图片 //bmpProductNo = Common.BitmapScaleHelper.ScaleToSize(bmpProductNo, bmpProductNo.Width * 2, bmpProductNo.Height); pbDmProductNo.Image = bmpDmtxCode; //pbProductNo.Image = bmpProductNo; pbBarcode.Image = bmpBarcode; //if (sItemNo != sBarcode) // pbItemNo.Image = bmpItemNo; if (cbxIsGenSn.Checked) { pbSnNo.Image = bmpSnNo; } string sLog = ""; sLog += bmpDmtxCode.Width + " x " + bmpDmtxCode.Height + "\r\n"; sLog += bmpProductNo.Width + " x " + bmpProductNo.Height + "\r\n"; sLog += bmpBarcode.Width + " x " + bmpBarcode.Height + "\r\n"; sLog += bmpItemNo.Width + " x " + bmpItemNo.Height + "\r\n"; sLog += bmpSnNo.Width + " x " + bmpSnNo.Height + "\r\n"; tbLogs.Text = sLog; //打印产品标签 if (DialogResult.Yes == MessageBox.Show("打印确认", "是否确认打印产品标签?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) { PrintItemLabel(); } }