1, CODE128 basic knowledge
CODE128 has three versions: code128a: Standard numbers and letters, controls, special characters code128b: standard numbers and letters, lowercase letters, special characters code128c: [00]-[99] Number pairs collection, total 100
With the code128b version, the Code 128 font needs to be installed in the system.
2. Barcode Coding Rules
Bar code consists of start bit, data, check bit, stop bit
Barcode =convert.tochar (204) + Data +convert.tochar (check code) +convert.tochar (206);
Need to calculate is the check code, the following directly gives the conversion function:
Private stringGet128codestring (stringinputdata) { stringresult; intchecksum=104; for(intIi=0; ii<inputdata.length;ii++) { if(inputdata[ii]>= +) {Checksum+ = (inputdata[ii]- +) * (ii+1); } Else{checksum+ = (inputdata[ii]+ -) * (ii+1); }} Checksum=checksum%103; if(checksum< the) {Checksum+= +; } Else{checksum+= -; } result=convert.tochar (204) +inputdata.tostring () +convert.tochar (checksum) +convert.tochar (206); returnresult;}
3. Printing
Public voidprintlable () {PrintDocument PD=NewPrintDocument (); Standardprintcontroller Controler=NewStandardprintcontroller (); Try{PD. PrintPage+=NewPrintpageeventhandler ( This. printcustomlable); Pd. Printcontroller=controler; Pd. Print (); return; } Catch(Exception Err) {Console.WriteLine (Err. Message); return; } finally{PD. Dispose (); }} Public voidprintcustomlable (Object sender,printpageeventargs av) {Font ft1=NewSystem.Drawing.Font ("Times New Roman", -, Fontstyle.regular,graphicsunit.world); Font ft2=NewSystem.Drawing.Font ("Code", -, Fontstyle.regular,graphicsunit.world); Brush BR=NewSolidBrush (Color.Black); Margins Margins=NewMargins ( -, -, -,145); Av. Pagesettings.margins=margins; Av. Graphics.DrawString (Get128codestring (inputstring), FT2,BR, -,-3); Av. Graphics.DrawString (INPUTSTRING,FT1,BR, the, -); Av. HasMorePages=false;}
The code used to print something needs to be referenced:
using System.Drawing.Printing;
To define a global variable:
string inputstring;
In the button click Test:
" 0123456789 " ; // to view the value of the Code128 code: // Lblbarcode.text = get128codestring (inputstring); printlable ();
C # Implementation of one-dimensional barcode printing (Code128)