Manual - Code 128

Introduction

Code 128 allows you to encode all the ASCII characters from 0 to 127. Different encoding tables allow you to encode data more efficiently. When a character is not available in the table, a "change table" character is written. You can select the table when you start your code, it is a good choice to choose a table which contains the first characters you want to write.
If you don't specify a specific table, the table is automatically chosen.
Here is a list of the supported table characters:

  • Table A : ASCII 0-95 (capital letters, numbers, special characters)
  • Table B : ASCII 32-127 (capital letters, lower case, numbers, special characters)
  • Table C : Group numbers by 2 from 00 to 99

Code 128 also contains 4 special characters that can't be written directly. These are the characters FNC1, FNC2, FNC3, and FNC4. You can write them by activating the tilde character (~) with the SetTilde() method.

You can pass normal text to the method Parse() in order to let this method analyze your text and choose the best encoding method. You can also still force the encoding method; below are the possibilities you can pass to the Parse() method:

  • auto encoding : automatic encoding method
  • new BCGDataInput(BCGcode128.Code.A, "TEXT") : Table A
  • new BCGDataInput(BCGcode128.Code.B, "text") : Table B
  • new BCGDataInput(BCGcode128.Code.C, "012345") : Table C
  • new BCGDataInput[] { new BCGDataInput(BCGcode128.Code.C, "012345"), new BCGDataInput(BCGcode128.Code.AUTO, "auto encoding") } : multiple encoding method

This class inherits the BCGBarcode1D class.

Extended ASCII character support

In order to support extended ASCII characters such as ü (u-umlaut/diaeresis) or ß (eszett), you have to precede the character by an FNC4 then subtract the unicode point of the character by 128.

The FNC4 code acts as a toggle for only a single character. If you plan to encode more than 1 subsequent character, you can use two consecutive FNC4 characters to latch into the extended values. You can repeat the double FNC4 characters to revert back to non-extended values.

For instance, to encode Grüße, you would do the following:

var text = "Gr~F4" + (char)('ü' - 128) + "~F4" + (char)('ß' - 128) + "e";

or

var text = "Gr~F4~F4" + (char)('ü' - 128) + (char)('ß' - 128) + "~F4e";

Example

Methods

BCGcode128's Methods

BCGBarcode1D's Methods

BCGBarcode's Methods

Code Example


var font = new BCGFont(new Font("Arial", 18));
var colorBlack = new BCGColor(Color.Black);
var colorWhite = new BCGColor(Color.White);

// Barcode Part
var code = new BCGcode128();
code.SetScale(2);
code.SetThickness(30);
code.SetForegroundColor(colorBlack);
code.SetBackgroundColor(colorWhite);
code.SetFont(font);
code.SetStart(null);
code.SetTilde(true);
code.Parse("a123");

// Drawing Part
var drawing = new BCGDrawing(code, colorWhite);

var memoryStream = new MemoryStream();
await drawing
    .FinishAsync(BCGDrawing.ImageFormat.Png, memoryStream);

Method explanations

  • BCGcode128(string start) — Specifies by which table the barcode should start
    Description
    The argument can be A, B, C or null. See the SetStart() method for more information.
    The default value is null.
    This means the table is automatically chosen.
  • SetStart(string start) — Specifies by which table the barcode should start
    Description
    The argument can be A, B, C or null. This selects the table the barcode will start.
    The default value is null.
    This means the table will be automatically chosen when you provide the text.
    The tables contain different characters which can be encoded in the barcode.
    Check the introduction of this document to obtain more information.
  • GetTilde() — Gets if the behavior for tilde ~ is modified
    Description
    Gets if the behavior for tilde ~ is modified.
    See SetTilde() for more details.
    Returns
    bool - true if activated, false otherwise
  • SetTilde(bool tilde) — Modifies the use of the tilde character ~
    Description
    By setting true in this argument, the tilde characters (ASCII 126 ~) will be processed as special characters. These are the special characters you can write.
    - ~~ : Writes a simple tilde
    - ~Fx : Writes a FNCx character, with x varying between 1 and 4
    The default value is true.
  • GetThickness() — Returns the thickness of the barcode
    Description
    The thickness of the barcode in pixels. The value isn't multiplied by the scale.
    Returns
    int - value in pixels
  • SetThickness(int thickness) — Specifies the thickness of the barcode
    Description
    The thickness of the barcode in pixels. This is the vertical size.
  • GetLabel() — Gets the label
    Description
    Returns the real value that will be displayed with the barcode. You have to have called the Parse() method first.
    Returns
    string - final label
  • SetLabel(string label) — Sets the label
    Description
    The text label will be written below or above the barcode depending on the barcode. You can write the special value BCGBarcode1D.Label if you would like your text to be chosen automatically. It will be the value passed to the Parse() method.
  • GetFont() — Gets the text font for the label
    Description
    Gets the text font for the label.
    Returns
  • SetFont(BCGFont font) — Sets the text font for the label
    Description
    The value of the argument must be an instance of the BCGFontFile class.
  • GetChecksum() — Gets the checksum appended to the barcode
    Description
    Returns the value that will be appended to the barcode. This method must be called after the method Parse().
    Returns
    int - checksum added or false if no checkum is included
  • SetDisplayChecksum(bool display) — Specifies the checksum to be added to the label
    Description
    Setting true will append the checksum to the default label.
    The default value is true.
  • Parse(string text) — Analyzes a text message to draw afterwards
    Description
    The data you pass to the text argument must be supported by the type of barcode you use.
    Check each barcode's introduction section to obtain more information on how to use this method within each symbology.
  • Draw(Graphics image) — Draws the barcode on the image
    Description
    The value of the image argument must be an image resource. The size of the image can be defined by the value received from GetDimension().
  • GetDimension(int width, int height) — Returns an array containing the required size for the image
    Description
    Returns an array in which the first index is the image width and the second index is the image height.
    The arguments are used to specify the starting point of the drawing. Should be 0 for both.
    The BCGDrawing class uses this method to create the image resource.
    Returns
    array(int, int) - [0] is the width, [1] is the height
  • GetScale() — Gets the scale of the barcode
    Description
    Gets the scale of the barcode. The value is the number of the "smallest" unit in pixel.
    Returns
    int - value in pixels
  • SetScale(int scale) — Sets the scale of the barcode
    Description
    The barcode will be x times bigger. Then a pixel will be x by x for its size.
  • GetForegroundColor() — Gets the color of the bars
    Description
    Gets the color of the bars of the barcode.
    Returns
  • SetForegroundColor(BCGColor color) — Sets the color of the bars
    Description
    Sets the color of the bars of the barcode. By default, the color is black. This argument can be a BCGColor class or any other argument that BCGColor can accept in its constructor.
  • GetBackgroundColor() — Gets the color of the spaces
    Description
    Gets the color of the spaces of the barcode.
    Returns
  • SetBackgroundColor(BCGColor color) — Sets the color of the spaces
    Description
    Sets the color of the spaces of the barcode. By default, the color is white. This argument can be a BCGColor class or any other argument that BCGColor can accept in its constructor.
  • SetColor(BCGColor foregroundColor, BCGColor backgroundColor) — Sets the color of the bars and spaces
    Description
    An easy and fast method to set the color of the bars and spaces. Check the SetForegroundColor() and SetBackgroundColor().
  • GetOffsetX() — Gets the X offset
    Description
    Gets the X offset of the barcode in pixels. The value isn't multiplied by the scale.
    Returns
    int - value in pixels
  • SetOffsetX(int value) — Sets the X offset
    Description
    Specifies the X offset of the barcode in pixels multiplied by the scale. The required size returned by GetDimension() will be modified accordingly.
  • GetOffsetY() — Gets the Y offset
    Description
    Gets the Y offset of the barcode in pixels. The value isn't multiplied by the scale.
    Returns
    int - value in pixels
  • SetOffsetY(int value) — Sets the Y offset
    Description
    Specifies the Y offset of the barcode in pixels multiplied by the scale. The required size returned by GetDimension() will be modified accordingly.
  • AddLabel(BCGLabel label) — Adds a label to the graphic
    Description
    Adds a BCGLabel object to the drawing.
  • RemoveLabel(BCGLabel label) — Removes a label from the graphic
    Description
    Removes a specific BCGLabel object from the drawing.
  • ClearLabels() — Removes the labels from the graphic
    Description
    Clears the BCGLabel objects from the drawing.