C#条码生成及打印实例代码
程序员文章站
2022-03-26 08:35:25
本文实例为大家分享了c#条码生成及打印的方法,供大家参考,具体内容如下
string barcodestring = "13043404455";//条码...
本文实例为大家分享了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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。