stringBarcodestring ="13043404455";//Barcode intImgWidth =520; intImgHeight = -; //Print button Private voidButton1_Click (Objectsender, EventArgs e) { //instantiating a Print objectPrintDocument PrintDocument1 =NewPrintDocument (); //To set the paper for printing, you can customize the size of the paper (in mm). can also not be set when print height is indeterminate//printDocument1.DefaultPageSettings.PaperSize = new PaperSize ("Custum", 585, 800); //registers the PrintPage event, which is triggered when each page is printedPrintdocument1.printpage + =NewPrintpageeventhandler ( This. Printdocument1_printpage); //Start printingPrintdocument1.print (); //Print Preview//PrintPreviewDialog ppd = new PrintPreviewDialog (); //PPD. Document = PrintDocument1; //PPD. ShowDialog (); } //Print Event Private voidPrintdocument1_printpage (Objectsender, PrintPageEventArgs e) {StringBuilder SB=NewStringBuilder (); Sb. Append ("\r\n\r\n\r\n"); Sb. Append ("******* Xinglong Supermarket *******\r\n"); Sb. Append ("Name-----Quantity-----price \ r \ n"); Sb. Append ("Fine white sand 1 8 yuan \ r \ n"); Sb. Append ("Zhang Xin fa Betel nut 1 10 yuan \ r \ n"); Sb. Append ("Total: 2 18 USD \ r \ n"); Sb. Append ("---Cashier: Zhang San---\ r \ n"); Sb. Append ("---Technical support: John Doe---\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> ///Draw Printed Content/// </summary> /// <param name= "E" >PrintPageEventArgs</param> /// <param name= "Printstr" >text that needs to be printed</param> /// <param name= "Barcodestr" >Barcode</param> Public voidDrawprint (PrintPageEventArgs E,stringPRINTSTR,stringBARCODESTR,intBarcodewidth,intbarcodeheight) { Try { //draw a printed stringE.graphics.drawstring (PRINTSTR,NewFont (NewFontFamily ("blackbody"),Ten), System.Drawing.Brushes.Black,1,1); if(!string. IsNullOrEmpty (BARCODESTR)) {intPrintWidth =175; intPrintheight = *; //draw a printed pictureE.graphics.drawimage (Createbarcodepicture (Barcodestr, Barcodewidth, Barcodeheight),0,0, PrintWidth, printheight); } } Catch(Exception ex) {MessageBox.Show (ex). ToString ()); } } /// <summary> ///generate a barcode image from a string (Add Reference: BarcodeLib.dll)/// </summary> /// <param name= "barcodestring" >Barcode String</param> /// <param name= "ImgWidth" >Image Broadband</param> /// <param name= "ImgHeight" >Picture Height</param> /// <returns></returns> PublicSystem.Drawing.Image Createbarcodepicture (stringBarcodestring,intImgWidth,intimgheight) {Barcodelib.barcode B=NewBarcodelib.barcode ();//instantiate a Barcode objectBarcodelib.type TYPE = BarcodeLib.TYPE.CODE128;//Encoding Type//Get Barcode PicturesSystem.Drawing.Image barcodepicture =B.encode (Type, barcodestring, System.Drawing.Color.Black, System.Drawing.Color.White, ImgWidth, imgheight); //Barcodepicture.save (@ "D:\Barcode.jpg");B.dispose (); returnbarcodepicture; } //Preview Barcode Private voidButton2_Click (Objectsender, EventArgs e) {pictureBox1.Image=createbarcodepicture (barcodestring, ImgWidth, imgheight); }
C # Barcode Generation and print instance code