C#条码生成及打印实例代码
本文实例为大家分享了C#条码生成及打印的方法,供大家参考,具体内容如下
stringBarcodeString="13043404455";//条码 intImgWidth=520; intImgHeight=120; //打印按钮 privatevoidbutton1_Click(objectsender,EventArgse) { //实例化打印对象 PrintDocumentprintDocument1=newPrintDocument(); //设置打印用的纸张,可以自定义纸张的大小(单位:mm).当打印高度不确定时也可以不设置 //printDocument1.DefaultPageSettings.PaperSize=newPaperSize("Custum",585,800); //注册PrintPage事件,打印每一页时会触发该事件 printDocument1.PrintPage+=newPrintPageEventHandler(this.printDocument1_PrintPage); //开始打印 printDocument1.Print(); //打印预览 //PrintPreviewDialogppd=newPrintPreviewDialog(); //ppd.Document=printDocument1; //ppd.ShowDialog(); } //打印事件 privatevoidprintDocument1_PrintPage(objectsender,PrintPageEventArgse) { StringBuildersb=newStringBuilder(); sb.Append("\r\n\r\n\r\n"); sb.Append("*******兴隆超市*******\r\n"); sb.Append("品名-----数量-----价格\r\n"); sb.Append("精品白沙18元\r\n"); sb.Append("张新发槟榔110元\r\n"); sb.Append("合计:218元\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> ///<paramname="e">PrintPageEventArgs</param> ///<paramname="PrintStr">需要打印的文本</param> ///<paramname="BarcodeStr">条码</param> publicvoidDrawPrint(PrintPageEventArgse,stringPrintStr,stringBarcodeStr,intBarcodeWidth,intBarcodeHeight) { try { //绘制打印字符串 e.Graphics.DrawString(PrintStr,newFont(newFontFamily("黑体"),10),System.Drawing.Brushes.Black,1,1); if(!string.IsNullOrEmpty(BarcodeStr)) { intPrintWidth=175; intPrintHeight=35; //绘制打印图片 e.Graphics.DrawImage(CreateBarcodePicture(BarcodeStr,BarcodeWidth,BarcodeHeight),0,0,PrintWidth,PrintHeight); } } catch(Exceptionex) { MessageBox.Show(ex.ToString()); } } ///<summary> ///根据字符串生成条码图片(需添加引用:BarcodeLib.dll) ///</summary> ///<paramname="BarcodeString">条码字符串</param> ///<paramname="ImgWidth">图片宽带</param> ///<paramname="ImgHeight">图片高度</param> ///<returns></returns> publicSystem.Drawing.ImageCreateBarcodePicture(stringBarcodeString,intImgWidth,intImgHeight) { BarcodeLib.Barcodeb=newBarcodeLib.Barcode();//实例化一个条码对象 BarcodeLib.TYPEtype=BarcodeLib.TYPE.CODE128;//编码类型 //获取条码图片 System.Drawing.ImageBarcodePicture=b.Encode(type,BarcodeString,System.Drawing.Color.Black,System.Drawing.Color.White,ImgWidth,ImgHeight); //BarcodePicture.Save(@"D:\Barcode.jpg"); b.Dispose(); returnBarcodePicture; } //预览条码 privatevoidbutton2_Click(objectsender,EventArgse) { pictureBox1.Image=CreateBarcodePicture(BarcodeString,ImgWidth,ImgHeight); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。