C # Implementation of one-dimensional barcode printing (Code128)

Source: Internet
Author: User

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)

Related Article

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.