首页 > 代码库 > C#条码生成及打印实例代码
C#条码生成及打印实例代码
string BarcodeString = "13043404455";//条码 int ImgWidth = 520; int ImgHeight = 120; //打印按钮 private void button1_Click(object sender, EventArgs e) { //实例化打印对象 PrintDocument printDocument1 = new PrintDocument(); //设置打印用的纸张,可以自定义纸张的大小(单位:mm). 当打印高度不确定时也可以不设置 //printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 585, 800); //注册PrintPage事件,打印每一页时会触发该事件 printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage); //开始打印 printDocument1.Print(); //打印预览 //PrintPreviewDialog ppd = new PrintPreviewDialog(); //ppd.Document = printDocument1; //ppd.ShowDialog(); } //打印事件 private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { StringBuilder sb = new StringBuilder(); sb.Append("\r\n\r\n\r\n"); sb.Append("*******兴隆超市*******\r\n"); sb.Append("品名-----数量-----价格\r\n"); sb.Append("精品白沙 1 8元\r\n"); sb.Append("张新发槟榔 1 10元\r\n"); sb.Append("合计: 2 18元\r\n"); sb.Append("---收银员:张三---\r\n"); sb.Append("---技术支持:李四---\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"); DrawPrint(e, sb.ToString(), BarcodeString, ImgWidth, ImgHeight); } /// <summary> /// 绘制打印内容 /// </summary> /// <param name="e">PrintPageEventArgs</param> /// <param name="PrintStr">需要打印的文本</param> /// <param name="BarcodeStr">条码</param> public void DrawPrint(PrintPageEventArgs e, string PrintStr, string BarcodeStr, int BarcodeWidth, int BarcodeHeight) { try { //绘制打印字符串 e.Graphics.DrawString(PrintStr, new Font(new FontFamily("黑体"), 10), System.Drawing.Brushes.Black, 1, 1); if (!string.IsNullOrEmpty(BarcodeStr)) { int PrintWidth = 175; int PrintHeight = 35; //绘制打印图片 e.Graphics.DrawImage(CreateBarcodePicture(BarcodeStr, BarcodeWidth, BarcodeHeight), 0, 0, PrintWidth, PrintHeight); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } /// <summary> /// 根据字符串生成条码图片( 需添加引用:BarcodeLib.dll ) /// </summary> /// <param name="BarcodeString">条码字符串</param> /// <param name="ImgWidth">图片宽带</param> /// <param name="ImgHeight">图片高度</param> /// <returns></returns> public System.Drawing.Image CreateBarcodePicture(string BarcodeString, int ImgWidth, int ImgHeight) { BarcodeLib.Barcode b = new BarcodeLib.Barcode();//实例化一个条码对象 BarcodeLib.TYPE type = BarcodeLib.TYPE.CODE128;//编码类型 //获取条码图片 System.Drawing.Image BarcodePicture = b.Encode(type, BarcodeString, System.Drawing.Color.Black, System.Drawing.Color.White, ImgWidth, ImgHeight); //BarcodePicture.Save(@"D:\Barcode.jpg"); b.Dispose(); return BarcodePicture; } //预览条码 private void button2_Click(object sender, EventArgs e) { pictureBox1.Image = CreateBarcodePicture(BarcodeString, ImgWidth, ImgHeight); }
C#条码生成及打印实例代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。