KA.Barcode Generator for .NET Suite
How to Print Code 128 in C# with Valid Data
Complete C#.NET source code to generate Code 128 images using Barcode Generator for .NET

C# Code 128 library: creating barcode code 128 with checksum, check digit, free c# example source code. no font









  • Complete C#.NET source code to generate, print Code 128 images using Barcode Generator for .NET Control
  • Powerful barcode component barcode Java, barcode .NET, ASP.NET barcode developed for easy barcode creation
  • Print, Draw Code 128 linear bar codes in Windows forms, ASP.NET web applications in C#.NET
  • Simple to encode all 128 ISO/IEC 646 characters, i.e. characters 0 to 127 inclusive
  • Characters with byte values 128 to 255 may also be encoded
  • High-quality Code 128 image output for all printers and scanners/readers
  • Recongize linear & 2D barcodes using barcode Excel or ASP.NET barcode
  • Automatically calculate and add checksum digit according to latest ISO/IEC Code 128 specification
  • Quiet zones automatically added as required in Code 128 standard
KA.Barcode Generator for .NET Suite is a powerful barcode generator component SDK for easy Code 128 image generation in ASP.NET web applications, Windows Forms, C#.NET class library and Crystal Reports.
Code 128 Introduction
Code 128, also named as ANSI/AIM 128, ANSI/AIM Code 128, USS Code 128, Uniform Symbology Specification Code 128, Code 128 Code Set A, Code 128 Code Set B, Code 128 Code Set C, Code 128A, Code 128B, Code 128C, is a self-checking linear bar code which encodes 128 ISO/IEC 646 characters.

Valid Data

  • Code 128A: A-Z, 0-9, and seven special characters
  • Code 128B: A-Z, a-z, 0-9, and seven special characters
  • Code 128C: digit pairs from 00-99 inclusive, and three special characters

Valid Length

  • Code 128 barcode has a variable symbol length.
Barcode code 128 symbol structure
Code 128 barcode symbols will include:
  • a leading quiet zone,

  • a Start character,

  • one or more characters representing Code 128 data and special characters,

  • a symbol check character, (checksum digit)

  • a Stop character,

  • a trailing quiet zone.


The image below illustrates a Code 128 symbol with encoding the text "AIM".

Barcode code 128 encodable character set
Code 128 barcode supports encoding the following character set
  • All 128 ISO/IEC 646 characters, i.e. characters 0 to 127 inclusive, in accordance with ISO/IEC 646.

  • Characters with byte values 128 to 255 may also be encoded.

  • 4 non-data function characters.

  • 4 code set selection characters.

  • 3 Start characters.

  • 1 Stop character.
Barcode Code sets
To encode the Code 128 supported character sets, Code 128 has three unique data character code sets: Code Sets A, B, and C, each comprising a subset of the ISO/IEC 646 character set.
  • Code Set A: Code Set A includes all of the standard upper case alphanumeric characters and punctuation characters together with the control characters, i.e. characters with values from 00 to 95 inclusive, as defined in ISO/IEC 646, and seven special characters.

  • Code Set B: Code Set B includes all of the standard upper case alphanumeric characters and punctuation characters together with the lower case alphabetic characters (i.e. characters with values from 32 to 127 inclusive, as defined in ISO/IEC 646) and seven special characters.

  • Code Set C: Code Set C includes the set of 100 digit pairs from 00 to 99 inclusive, as well as three special characters. This allows numeric data to be encoded as two data digits per symbol character.


If the encoding Code 128 data covers more than 1 Code set, you can choose "Code128Auto", and our software will automatically decide the best Code sets for you.




Code 128 barcode check sum digit
The symbol check character shall be included as the last symbol character before the Stop character. The symbol check character shall not be represented in the human readable interpretation, nor shall it be transmitted by the decoder.

When generating Code 128 barcodes using our software, you should not provide checksum digit in the encoding data. Our software will automatically add, include check sum digit in the generated barcode code 128 images.
  • Demo code: create a Code 128 barcode image
  • Demo code: encode non-printable chars in Code 128
  • Complete C# source code to create a Code 128 barcode image
                BarCode barcode = new BarCode();
                barcode.Symbology = KeepAutomation.Barcode.Symbology.Code128Auto;
    
                //Input your barcode encoding data:
                barcode.CodeToEncode = "Code128SetAuto123";
    
                // Unit of measure, pixel, cm and inch supported.
                barcode.BarcodeUnit = BarcodeUnit.Cm;
    
                barcode.AutoSizeAdjust = true;
    
                barcode.BarCodeWidth = 3;
                barcode.BarCodeHeight = 2;
    
                // barcode image resolution in dpi
                barcode.DPI = 300;
                // barcode bar module width (X dimention)
                barcode.X = 1;
                // barcode bar module height (Y dimention), Y=X
                barcode.Y = 0.1f;
    
                // barcode image left margin size, the minimum value is 4X.
                barcode.LeftMargin = 0;
                // Image right margin size, minimum value is 4X.
                barcode.RightMargin = 0;
                // Image top margin size, minimum value is 4X.
                barcode.TopMargin = 0;
                // Image bottom margin size, minimum value is 4X.
                barcode.BottomMargin = 0;
    
                // barcode orientation, 90, 180, 270 degrees supported.
                barcode.Orientation = KeepAutomation.Barcode.Orientation.Degree0;
    
    
                barcode.TextColor = System.Drawing.Color.Black;
                barcode.TextFont = new System.Drawing.Font("Arial", 9F * (96F / barcode.DPI));
                barcode.TextMargin = 0.3f;
    
    
                // barcode image formats, supporting Png, Jpeg, Gif, Tiff, Bmp, etc.
                barcode.ImageFormat = ImageFormat.Png;
    
                // Generate barcode barcodes in image format GIF
                barcode.generateBarcodeToImageFile(TestMain.TEST_OUTPUT_PATH + "barcode-code128-set-auto-csharp.png");
    
                /* Create barcode barcodes in Stream object
                barcode.generateBarcodeToStream(".NET System.IO.Stream Object");
    
                Draw & Print barcode barcodes to Graphics object
                barcode.generateBarcodeToGraphics(".NET System.Drawing.Graphics Object");
    
                Generate barcode barcodes & write to byte[]
                byte[] barcodeInBytes = barcode.generateBarcodeToByteArray();
    
                Generate barcode barcodes & encode to System.Drawing.Bitmap object
                Bitmap barcodeInBitmap = barcode.generateBarcodeToBitmap();
                */
    


    How to encode non-printable chars in Code 128 using C#
    To encode non-printable chars in Code 128, you need the following settings.
    • TildeEnabled to be true. It allows you to encode chars using 3-digit numbers

    • CodeToEncode: you can use '~ddd' (such as '~013') to represent a char, the 'ddd' is the char ASCII value.
                BarCode barcode = new BarCode();
                barcode.Symbology = KeepAutomation.Barcode.Symbology.Code128Auto;
    
                //  Non-pritable characters could be represented in format "~ddd", which "ddd" is a decimal number between 0 to 255.
                //  TildeEnabled must be enable to support this feature.
                barcode.CodeToEncode = "ABC~013DEF";        //  "~013" - ASCII character [CR]
                barcode.TildeEnabled = true;
    
                // Unit of measure, pixel, cm and inch supported.
                barcode.BarcodeUnit = BarcodeUnit.Pixel;
    
                // barcode bar module width (X dimention)
                barcode.X = 6;
                // barcode bar module height (Y dimention)
                barcode.Y = 120;
    
                barcode.TextColor = System.Drawing.Color.Black;
                barcode.TextFont = new System.Drawing.Font("Arial", 12);
                barcode.TextMargin = 20;
    
                // barcode image formats, supporting Png, Jpeg, Gif, Tiff, Bmp, etc.
                barcode.ImageFormat = ImageFormat.Png;
    
                // Generate barcode barcodes in image format GIF
                barcode.generateBarcodeToImageFile(TestMain.TEST_OUTPUT_PATH + "barcode-code128-non-printable-chars-csharp.png");
    


    Code 128 Data Encoding in KA.Barcode Generator for .NET Suite

    Install KA.Barcode Generator for .NET Suite into Project

    1. 1.
    2. 2.
      Add Reference: Add "KeepAutomation.Barcode.Window.dll" to your .NET WinForms project reference.
    3. 3.
      Add to Toolbox: Add BarCodeControl to your .NET Visual Studio toolbox.
    Copy the following C#.NET Code onto your project to create a Code 128 barcode image
    using KeepAutomation.Barcode.Bean;

    BarCode code128= new BarCode();
    code128.Symbology = KeepAutomation.Barcode.Symbology.Code128Auto;
    code128.CodeToEncode = "Code128";
    code128.X = 3;
    code128.generateBarcodeToImageFile("C:/code128_csharp.png");
    Stream barcode images in ASP.NET web sites & web forms application using free Visual C# .NET trial. KA.Barcode Generator for ASP.NET ....
    How to Generate QR Code in VB.NET application. How to generate 2D QR Code barcode images in VB.NET, ASP.NET web applications, and windows forms. ....
    Successfully. How to Generate QR Code in Java Class with Demo Code. Here is the detailed sample code to create QR Code in Java class. All ....
    Code Barcode Generation functionality of KA.Barcode Generator for .NET Suite, which enables developers to efficiently print, produce QR Codes in...
    This Barcode Generator control SDK library support generating ....
    more>

    How to Encode Alphanumeric Data for Code128A

    KeepAutomation Code 128A allows you to encode all of the standard upper-case alphanumeric characters (A-Z, 0-9) and punctuation characters ( , ; < = > ? @ ) together with the control characters, i.e. characters with values from 00-95 inclusive, as defined in ISO/IEC 646, and seven special characters.
    using KeepAutomation.Barcode.Bean;

    BarCode code128= new BarCode();
    code128. Symbology = Symbology. Code128A;
    code128.CodeToEncode = "CODE128";

    How to Encode Alphanumeric Data for Code128A

    KeepAutomation Code 128B allows you to encode all of the standard upper-case & lower-case alphanumeric characters (A-Z, a-z, 0-9) and punctuation characters ( , ; < = > ? @ ) together with the control characters.
    using KeepAutomation.Barcode.Bean;

    BarCode code128= new BarCode();
    code128. Symbology = Symbology. Code128B;
    code128.CodeToEncode = "Code128";

    How to Encode Numeric Data for Code128C

    As specified in the Code 128 standard, KeepAutomation Code 128C allows you to encode the set of 100 digit pairs from 00-99 inclusive, as well as special characters. This allows numeric data to be encoded as two data digits per symbol character. Because of the encoding pattern, if you input numeric data with odd digits, a zero will be added in the end of the encoding. For example, if you input data "123" in a Code 128C, the output will be “1230”.
    using KeepAutomation.Barcode.Bean;

    BarCode code128= new BarCode();
    code128. Symbology = Symbology. Code128C;
    code128.CodeToEncode = "1234";
    Please note that if you are not sure which data mode is proper for your encoding, you can simply select Code 128 Auto mode, KA.Barcode Generator for .NET Suite will automatically print valid Code 128 images according to the data you input and make necessary shift when necessary.