C # code generation and printing of instance code,
String BarcodeString = "13043404455"; // bar code int ImgWidth = 520; int ImgHeight = 120; // print the button private void button#click (object sender, EventArgs e) {// instantiate the Print Object PrintDocument printDocument1 = new PrintDocument (); // you can set the size of the printed paper (unit: mm ). when the printing height is not determined, you can also do not set // printdocument1.defaultpageset.pdf. paperSize = new PaperSize ("Custum", 585,800); // registers the PrintPage event. When printing each page, the event printDocument1.PrintPage + = new P is triggered. RintPageEventHandler (this. printdocument#printpage); // start printing printDocument1.Print (); // print preview // PrintPreviewDialog ppd = new PrintPreviewDialog (); // ppd. document = printDocument1; // ppd. showDialog ();} // print the event private void printDocument1_PrintPage (object sender, PrintPageEventArgs e) {StringBuilder sb = new StringBuilder (); sb. append ("\ r \ n"); sb. append ("****************** \ r \ n"); sb. append ("Product Name ----- Quantity ----- price \ r \ n "); sb. append ("Boutique Baisha 1 8 Yuan \ r \ n"); sb. append ("Zhang xinfa betel nut 1 10 Yuan \ r \ n"); sb. append ("Total: 2 18 RMB \ r \ n"); sb. append ("--- CASHIER: James --- \ r \ n"); sb. append ("--- technical support: li Si --- \ r \ n "); drawPrint (e, sb. toString (), BarcodeString, ImgWidth, ImgHeight );} /// <summary> /// draw the printed content /// </summary> /// <param name = "e"> PrintPageEventArgs </param> /// <param name = "PrintStr"> text to be printed </param> /// <Param name = "BarcodeStr"> Bar Code </param> public void DrawPrint (PrintPageEventArgs e, string PrintStr, string BarcodeStr, int BarcodeWidth, int BarcodeHeight) {try {// draw the print string 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; // draw the Print Image e. graphics. drawImage (CreateBarcodePicture (BarcodeStr, BarcodeWidth, BarcodeHeight), 0, 0, PrintWidth, PrintHeight) ;}} catch (Exception ex) {MessageBox. show (ex. toString () ;}/// <summary> /// generates a bar code Image Based on the string (reference required: BarcodeLib. dll) /// </summary> /// <param name = "BarcodeString"> bar code string </param> /// <param name = "ImgWidth"> image bandwidth </param> /// <param name = "ImgHeight"> image height </param> /// <returns> </returns> public System. drawing. image CreateBarcodePicture (string BarcodeString, int ImgWidth, int ImgHeight) {BarcodeLib. barcode B = new BarcodeLib. barcode (); // instantiate a bar code object BarcodeLib. TYPE type = BarcodeLib. TYPE. CODE128; // encoding type // obtain the bar code image 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;} // preview the bar code private void button2_Click (object sender, EventArgs e) {pictureBox1.Image = CreateBarcodePicture (BarcodeString, ImgWidth, ImgHeight );}