C # Barcode Generation and print instance code

Source: Internet
Author: User
Tags print object




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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.