C#條碼生成及打印實例代碼。本站提示廣大學習愛好者:(C#條碼生成及打印實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是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); }
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持。