C#條碼生成及打印實(shí)例代碼
本文實(shí)例為大家分享了C#條碼生成及打印的方法,供大家參考,具體內(nèi)容如下
string BarcodeString = "13043404455";//條碼 int ImgWidth = 520; int ImgHeight = 120; //打印按鈕 private void button1_Click(object sender, EventArgs e) { //實(shí)例化打印對(duì)象 PrintDocument printDocument1 = new PrintDocument(); //設(shè)置打印用的紙張,可以自定義紙張的大小(單位:mm). 當(dāng)打印高度不確定時(shí)也可以不設(shè)置 //printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 585, 800); //注冊(cè)PrintPage事件,打印每一頁(yè)時(shí)會(huì)觸發(fā)該事件 printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage); //開(kāi)始打印 printDocument1.Print(); //打印預(yù)覽 //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("品名-----數(shù)量-----價(jià)格\r\n"); sb.Append("精品白沙 1 8元\r\n"); sb.Append("張新發(fā)檳榔 1 10元\r\n"); sb.Append("合計(jì): 2 18元\r\n"); sb.Append("---收銀員:張三---\r\n"); sb.Append("---技術(shù)支持:李四---\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> /// 繪制打印內(nèi)容 /// </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> /// 根據(jù)字符串生成條碼圖片( 需添加引用: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();//實(shí)例化一個(gè)條碼對(duì)象 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; } //預(yù)覽條碼 private void button2_Click(object sender, EventArgs e) { pictureBox1.Image = CreateBarcodePicture(BarcodeString, ImgWidth, ImgHeight); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WPF TextBox實(shí)現(xiàn)按字節(jié)長(zhǎng)度限制輸入功能
這篇文章主要為大家詳細(xì)介紹了WPF TextBox實(shí)現(xiàn)按字節(jié)長(zhǎng)度限制輸入功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11C# winform 模擬鍵盤輸入自動(dòng)接入訪問(wèn)網(wǎng)絡(luò)的實(shí)例
本篇文章主要介紹了C# winform 模擬鍵盤輸入自動(dòng)接入訪問(wèn)網(wǎng)絡(luò),有興趣的可以了解一下。2016-11-11解析StreamReader與文件亂碼問(wèn)題的解決方法
本篇文章是對(duì)StreamReader與文件亂碼問(wèn)題的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05WPF仿LiveCharts實(shí)現(xiàn)餅圖的繪制
這篇文章主要介紹了如何利用WPF仿LiveCharts實(shí)現(xiàn)餅圖的繪制,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的可以參考一下2022-07-07C#?CefSharp?根據(jù)輸入日期段自動(dòng)選擇日期的操作代碼
這篇文章主要介紹了C#?CefSharp?根據(jù)輸入日期段自動(dòng)選擇日期的操作代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01