利用ZXing生成条码图像的主要代码段
using ZXing; using ZXing.Common; private void button1_Click(object sender, EventArgs e) { string SN = textBox1.Text; if (string.IsNullOrEmpty(SN)) { return; } BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.DATA_MATRIX, Options =new EncodingOptions { Width = 70, Height = 70, Margin = 2 } }; Bitmap QRcode = writer.Write(SN); Bitmap bitmap = new Bitmap(360, 80); Graphics g = Graphics.FromImage(bitmap); Font font = new Font("Arial", 18); SolidBrush brush = new SolidBrush(Color.White); StringFormat format = new StringFormat(); g.DrawString(SN.Substring(0,17), font, brush, new Point(0, 0), format); g.DrawString(SN.Substring(17, 13), font, brush, new Point(0, 24), format); g.DrawString(SN.Substring(30), font, brush, new Point(0, 48), format); g.DrawImage(QRcode, new Point(280, 0)); font.Dispose(); brush.Dispose(); format.Dispose(); pictureBox1.Image = bitmap; string data = ZebraUnity.BitmapToHex(bitmap, out int to, out int row); g.Dispose(); string zpl = string.Format("~DGR:Temp0.GRF,{0},{1},{2}", to, row, data); StringBuilder cmd = new StringBuilder(); cmd.Append(zpl); cmd.Append("^XA^LH100,30"); cmd.Append(string.Format("^FO{0},{1}",top.Value,left.Value)); cmd.Append("^XGR:Temp0.GRF,1,1^FS"); cmd.Append(string.Format("^PQ{0}",numericUpDown1.Value)); cmd.Append("^XZ"); string cmdstr = cmd.ToString(); ZPLHelper.PrintData(cmdstr); }