The example of this article for you to share the C # barcode generation and printing methods for your reference, the specific content as follows
String barcodestring = "13043404455";//Barcode int imgwidth = 520;
int imgheight = 120; Print button private void Button1_Click (object sender, EventArgs e) {//Materialized print object PrintDocument PrintDocument1
= new PrintDocument (); Set the paper for printing to customize the size of the paper (in mm).
You can also not set//printdocument1.defaultpagesettings.papersize = new PaperSize ("Custum", 585, 800) when the print height is uncertain;
Registers the PrintPage event, which triggers the event printdocument1.printpage + = new Printpageeventhandler (this.printdocument1_printpage) when printed on each page;
Start printing printdocument1.print ();
Print Preview//printpreviewdialog ppd = new PrintPreviewDialog (); Ppd.
Document = PrintDocument1; Ppd.
ShowDialog (); }//Print event private void Printdocument1_printpage (object sender, PrintPageEventArgs e) {StringBuilder SB
= new StringBuilder (); 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 ("A Xin hair betel nut 1 10 yuan \ r \ n"); Sb.
Append ("total: 2 18 yuan \ r \ n"); Sb.
Append ("---cashier: John---\ r \ n"); Sb.
Append ("---technical support: Dick---\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 the print content///</summary>///<param name= "E" >printpageeventargs</pa
ram>///<param name= "printstr" > Need to print text </param>///<param name= "Barcodestr" > Barcode </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 ("bold"), Syst Em.
Drawing.Brushes.Black, 1, 1); if (!string.
IsNullOrEmpty (BARCODESTR)) {int printwidth = 175;
int printheight = 35; Draw a printed picture E.graphics.drawimage (Createbarcodepicture barcodestr, BarcodewidtH, Barcodeheight), 0, 0, printwidth, printheight); } catch (Exception ex) {MessageBox.Show (ex).
ToString ()); }///<summary>///generate barcode Pictures from strings (Add references: BarcodeLib.dll)///</summary>///<param Name= "barcodestring" > Barcode string </param>///<param name= "ImgWidth" > Picture broadband </param>///<param name = "ImgHeight" > Picture height </param>///<returns></returns> public System.Drawing.Image Createbarcodepi Cture (string barcodestring, int imgwidth, int imgheight) {Barcodelib.barcode b = new Barcodelib.barcode ();//instantiated A Barcode object Barcodelib.type type = barcodelib.type.code128;//encoding type//Get barcode picture System.Drawing.Image BARCODEPICTU
Re = B.encode (type, barcodestring, System.Drawing.Color.Black, System.Drawing.Color.White, ImgWidth, imgheight);
Barcodepicture.save (@ "D:\Barcode.jpg");
B.dispose ();
return barcodepicture; //Preview Barcode Private void button2_click (object sender, EventArgs e) {picturebox1.image = Createbarcodepicture (barcodestring, IMG
Width, ImgHeight);
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.