How to convert digits, text string to barcode using C#?

How to convert numbers, ASCII chars, Unicode text & GS1 to barcode images using Visual C# class

Create GS1 compatible barcodes and encode Unicode into barcodes for C#.NET programs



In this C# tutorial, you will learn how to generate linear and matrix barcodes with various data encoding formats in your C# ASP.NET and Windows apps.

  • Quick to encode digits, AlphaNumeric chars, non-printable characters
  • Barcode with GS1 data elements
  • Barcode encoding Unicode data message
  • Store a large data message (audio, video file) on multiple 2d barcodes

How to create barcode with valid data formats using C#

  1. Download C#.NET Barcode Generator Suite
  2. Install C# library to create barcode with valid data in .NET apps
  3. Step by Step Tutorial










  • Simple to use .NET barcode generator dll components for C# barcode generating & encoding
  • Enable C# developers to generate & make barcodes in Windows Forms, ASP.NET, .NET Reporting and Console projects
  • Use free Visual C# sample code to generate high print-quality GS1 compatible 1d & 2d barcodes
  • Support encoding Unicode into C#.NET barcode images, like QR Code and Data Matrix
  • Provide several developer licenses & packages, with the option to get C#.NET Source Code
  • C# demo code available to assign bar code pictures in C#.NET
  • Mature C# barcode device with user-friendly interface for barcode designer
  • Advanced Barcode Control SDKs for .NET, C#.NET, ASP.NET website, Visual Basic program
OnBarcode.com develops comprehensive Barcode Library SDK for .NET, C#, VB.NET & ASP.NET barcode creating applications. .NET developers can easily generate readable barcode images in various .NET framework projects, like C#.NET Windows Forms, ASP.NET C# Web Service, C#.NET Crystal Reports and RDLC Reports applications. More than twenty linear & 2d barcodes are supported by this mature C#.NET barcode generator tool.
If you are looking for complete guides to create barcodes in C# server side or in WinForms, navigate to pages below:
  1. Tutorial for C# Barcode Generation - Generating batch barcode (linear & bidimensional) images in .NET projects using C#
  2. Tutorial for C# Barcode ASP.NET Generation - Streaming & creating barcodes in ASP.NET web applications (html) in C#
  3. Tutorial for C# Barcode WinForms Generation - Downloading barcode dll to encrypt barcode graphics in Windows Forms using C#
  4. Tutorial for C# Barcode Imaging Setting - Creating & printing barcodes and adjust image settings in Visual Studio using C#




Quick to convert number, text string to barcode using C#

Top
It is an easy job to encode simple barcode data message.

You can directly pass the encoding data message to the barcode property Data.

Here we will create a simple QR Code image file using C#.
  • Firstly, create a new QRCode object, which allows you to create a QR Code barcode image with customized barcode settings.
  • Set the QR Code encoding data as "QR Code"
  • Call method drawBarcode() to create a new QR Code image file using C# code
            QRCode barcode = new QRCode();
            barcode.Data = "QR Code";
            barcode.drawBarcode("C://Output//qrcode-quick-create.png");

Here is the QR Code generated using the above C# codes.




Now let's generate a Code 128 linear barcode in memory using C#.
  • Create a new Linear object, which allows you to create any 1d/linear barcode type
  • Set the barcode data as "ABC-123-abc"
  • Set the barcode type as "BarcodeType.CODE128"
  • Create a new Stream object, which will contain the Code 128 barcode data
  • Call method drawBarcode() to create a new Code 128 barcode and save the barcode to the Stream object
            Linear barcode = new Linear();
            barcode.Data = "ABC-123-abc";
            barcode.Type = BarcodeType.CODE128;

            Stream barcodeInStream = new MemoryStream();
            barcode.drawBarcode(barcodeInStream);

Here is the Code 128 generated using the above C# codes.




Advanced barcode data encoding using C#

Top
A barcode symbol may encode numbers, plain text (ASCII characters), Unicode text, and industry standard (such as GS1 standard) data message.

The following content explains how to create 2d and linear barcodes with various text string formats encoded in your C# ASP.NET, Windows applications.


How to Create Barcode with ASCII and extended ASCII (ISO/IEC 8859-1) in C#?

ASCII, an acronym for American Standard Code for Information Interchange, includes 95 printable characters and 33 control characters (non-printing chars).

List of barcode formats encoding full ASCII 128 characters
  • Code 39 full ASCII mode
  • Code 128
  • Data Matrix
  • PDF417
  • QR Code

ISO 8859-1 character encoding, also known as Latin1 or ISO-Latin-1, that supports Western European languages. ISO 8859-1 character set is referred to as extended ASCII.

List of barcode formats encoding ISO 8859-1 characters
  • Code 128
  • Data Matrix
  • PDF417
  • QR Code
ASCII and extended ASCII character set includes printable characters and non-printing chars. To create above barcode images with printable characters, you can pass the characters to the "Data" property in the C# program.
            Linear barcode = new Linear();
            barcode.Type = BarcodeType.CODE128;
            barcode.Data = "ASCII-123-abc";


How to Print Barcode with ASCII Non-printing Chars using C#?

However some special characters are impossible to be entered using keyboard to a string directly, such as char 'CR' (Carriage Return).
In C# barcode generator library, it provides an easy solution for encoding these non-printable characters.

Here is an example to encode char 'CR' (Carriage Return) in Code 128 barcode using C#.
  1. Set property ProcessTilde to true.
  2. Convert non-printable char to '~ddd' format. Here ddd is the char ASCII Integer code in 3-digit. 'CR' should be converted to '~013'. Click to view all 128 ASCII character values
  3. Set property Data to '~013'
Here is the C# example source code:
            Linear code128 = new Linear();

            // encode non printable char '[CR]', between 'a' and 'b'
            code128.Data = "a~013bc";
            // Barcode symbology type. Code 128 supports types: CODE128, CODE128A, CODE128B, CODE128C.
            code128.Type = BarcodeType.CODE128;

            code128.ProcessTilde = true;




Note: the non-printable chars will be encoded into barcode modules, but will not be displayed in the barcode text.




How to create barcode with Unicode text encoded using C#?

Instead of encoding ISO 8859-1 standard chars, you may need to encode other character sets, such as Arabic, Cyrillic, Greek, Hebrew. And our C# Barcode Generator for .NET supports encoding these special characters through Unicode encoding. The following are examples for encoding Unicode into QR Code, Data Matrix and PDF417.

It is really easly to create 2d barcode with Unicode text encoded using C# Barcode Library. Here we will explain how to generate QR Code with Unicode text encoded using C# code.
  1. Create a new QRCode object (Data Matrix, PDF-417 are also working)
  2. Set EncodeUnicodeText property to true
  3. Set the Data property with the Unicode text message, such as
  4. Utilize drawBarcode() method to print QR Code to an image file or create QRCode in memory
            QRCode barcode = new QRCode();
            barcode.EncodeUnicodeText = true;
            barcode.Data = "Unicode text message here";
            barcode.drawBarcode("C://Output//qrcode-unicode-sample.png");



Please note that once the "EncodeUnicodeText" property is true, the library will automatically apply the following settings
  • "ProcessTilde" will be changed to true
  • "DataMode" will be changed to "QRCodeDataMode.Byte" for QR Code. For Data Matrix, the value will be "DataMatrixDataMode.Base256". For PDF-417, the value will be "PDF417DataMode.Byte"
The C# barcode library will use UTF8 encode for Unicode text during 2d barcodes generation. If you want to apply other text encode, you can try the following code
            barcode.UnicodeEncoding = TextEncoding.UTF8;
Here are the available options for TextEncoding enum
  • UTF8 (default encode)
  • UTF7
  • UTF32
  • Unicode
  • BigEndianUnicode


Create QR Code with binary data from Unicode text converted

You can also convert the Unicode text to binary data using UTF8 encodes in C#, and create QR Code with binary data using C#. Here are the details.

To encode Unicode text, you need process the Unicode characters first.
  1. Convert unicode text to byte array using UTF8 encoding or others
  2. Convert eacy byte value to three digits text with char '~' in the beginning
  3. Append all converted byte text into one single String object
You can use one of the following encoding to convert Unicode characters to byte array
  • System.Text.Encoding.UTF8 (by default)
  • System.Text.Encoding.UTF7
  • System.Text.Encoding.UTF32
  • System.Text.Encoding.Unicode
  • System.Text.Encoding.BigEndianUnicode
Please note that your barcode reader software or scanner device must use the same encode to decode the barcode data. Most mobile barcode scanner softwares are using UTF8 encode.

After the Unicode characters processed, you can easily generate barcode with Unicode characters encoded in C#.
  1. Set the property Data with processed Unicode bytes string object
  2. Enable property ProcessTilde to true
  3. Set barcode data encode mode as Byte mode
QR Code with Unicode chacters in C#
            QRCode barcode = new QRCode();
            //  It may encode any Unicode characters after converting them to bytes in UTF-8 encode.
            //  And then, use Byte encodation to encode these byte data.
            String message = "สวัสดี";
            byte[] bytes = Encoding.UTF8.GetBytes(message);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
                sb.Append("~" + b.ToString().PadLeft(3, '0'));
            barcode.Data = sb.ToString();

            barcode.ProcessTilde = true;
            barcode.DataMode = QRCodeDataMode.Byte;

            barcode.drawBarcode("C://Output//qrcode-unicode-sample.png");
Data Matrix with Unicode chacters in C#
            DataMatrix barcode = new DataMatrix();

            //  It may encode any Unicode characters after converting them to bytes in UTF-8 encode.
            //  And then, use Base256 encodation to encode these byte data.
            String message = "สวัสดี";
            byte[] bytes = Encoding.UTF8.GetBytes(message);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
                sb.Append("~" + b.ToString().PadLeft(3, '0'));

            barcode.Data = sb.ToString();
            barcode.ProcessTilde = true;
            barcode.DataMode = DataMatrixDataMode.Base256;

            barcode.drawBarcode("C://Output//csharp-datamatrix-unicode.png");
PDF-417 with Unicode chacters in C#
            PDF417 barcode = new PDF417();

            //  It may encode any Unicode characters after converting them to bytes in UTF-8 encode.
            //  And then, use Byte mode to encode these byte data.
            String message = "早上好";
            byte[] bytes = Encoding.UTF8.GetBytes(message);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
                sb.Append("~" + b.ToString().PadLeft(3, '0'));

            barcode.Data = sb.ToString();
            barcode.ProcessTilde = true;
            barcode.DataMode = PDF417DataMode.Byte;


            barcode.drawBarcode("C://Output//OnBarcode.com//csharp-pdf417-unicode-text.png");





How to encode binary (byte array) data into barcode using C# code?

Most of 2d barcode symbologies such as QR Code, Data Matrix, PDF-417 do support byte array data encoding. Using C# barcode library, you can easily create barcodes with byte array data encoded in your C# program.

  1. Set the barcode data mode to byte
  2. Enable property ProcessTilde to true. Now the C# barcode library can process byte array data
  3. Convert each byte to '~ddd' format. For example, a byte '80' will be converted to '~080'.
  4. Set property Data with the converted bytes combined in string
  5. Generate barcode with the bytes data encoded
            QRCode barcode = new QRCode();

            String message = "abc";
            byte[] bytes = Encoding.UTF8.GetBytes(message);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
                sb.Append("~" + b.ToString().PadLeft(3, '0'));

            barcode.DataMode = QRCodeDataMode.Byte;
            barcode.ProcessTilde = true;
            barcode.Data = sb.ToString();

            barcode.drawBarcode("C://Output//qrcode-data-in-bytes.png");




How to encode GS1 barcode in C#?

C# Barcode Library supports encoding these GS1 compatible barcode images: GS1 DataBar, EAN-128/GS1-128, ITF-14, Data Matrix, and QR Code. Add barcode dll to reference first, and see GS1 encoding examples of these barcodes as follows. FYI, select your barcode according to the encoding C# barcode text string (alphanumeric characters or numeric data from database).
Besides data encoding settings, C# Barcode Dll also provides designing functions to customize barcode symbology, such as automatically adding checksum, adjusting barcode length & height, display barcode formats (jpeg, gif, png, tiff, bitmap), manage resolution /color or image export. Just add the dll to reference and copy the C# code step by step.

Generate GS1 DataBar Barcode in C# Class

   Linear gs1_databar = new Linear();
gs1_databar.Type = BarcodeType.RSS14;
gs1_databar.Data = "01666663333300";

// GS1 DataBar Size Settings
gs1_databar.UOM = UnitOfMeasure.PIXEL;
gs1_databar.X = 2;
gs1_databar.Y = 82;
gs1_databar.LeftMargin = 20;
gs1_databar.RightMargin = 20;

// GS1 DataBar Image Format Setting
gs1_databar.Format = System.Drawing.Imaging.ImageFormat.Gif;

// Save GS1 DataBar to C:\
gs1_databar.drawBarcode("C://csharp_gs1_databar.gif");

Generate GS1-128/EAN-128 Barcode in C# Class

   Linear gs1_128 = new Linear();
gs1_128.Type = BarcodeType.EAN128;
gs1_128.Data = "(8101)06789900799";

// GS1-128/EAN-128 Size Settings
gs1_128.UOM = UnitOfMeasure.PIXEL;
gs1_128.X = 1;
gs1_128.Y = 80;
gs1_128.LeftMargin = 10;
gs1_128.RightMargin = 10;

// GS1-128/EAN-128 Image Format Setting
gs1_128.Format = System.Drawing.Imaging.ImageFormat.Png;

// Save GS1-128/EAN-128 to C:\
gs1_128.drawBarcode("C://csharp_gs1_128.png");

Generate GS1 Compatible ITF-14 Barcode in C# Class

   Linear itf_14 = new Linear();
itf_14.Type = BarcodeType.ITF14;
itf_14.Data = "1061414199993";

// ITF-14 Size Settings
itf_14.UOM = UnitOfMeasure.PIXEL;
itf_14.X = 1;
itf_14.Y = 76;
itf_14.N = 2.0f;

// ITF-14 Bearer Bar Settings
itf_14.BearerBarHori = 2;
itf_14.BearerBarVert = 2;

// ITF-14 Image Format Setting
itf_14.Format = System.Drawing.Imaging.ImageFormat.Png;

// Save ITF-14 to C:\
itf_14.drawBarcode("C://csharp_itf_14.png");

Generate Compatible Data Matrix Barcode in C# Class

   DataMatrix data_matrix = new DataMatrix();

// Set Data Matrix barcode data to encode
data_matrix.Data = "(8101)06789900799";

// Set Data Matrix data mode
data_matrix.DataMode = DataMatrixDataMode.ASCII;

// Data Matrix format mode
data_matrix.FormatMode = DataMatrixFormatMode.Format_16X16;

// Set Data Matrix to be GS1 Compatible
data_matrix.FNC1 = FNC1.FNC1_1ST_POS;

// Set Data Matrix barcode size;
data_matrix.UOM = UnitOfMeasure.PIXEL;
data_matrix.X = 4;
data_matrix.LeftMargin = 8;
data_matrix.RightMargin = 8;
data_matrix.TopMargin = 8;
data_matrix.BottomMargin = 8;

// Generate Data Matrix and encode it to gif format
data_matrix.ImageFormat = System.Drawing.Imaging.ImageFormat.Gif;
data_matrix.drawBarcode("C:\\csharp_data_matrix.gif");

Generate Compatible QR Code Barcode in C# Class

   QRCode qr_code = new QRCode();

// Set QR Code barcode data
qr_code.Data = "(8101)06789900799";

// Set QR Code data mode
qr_code.DataMode = QRCodeDataMode.Auto;

// Set QR Code to be GS1 Compatible
qr_code.FNC1 = FNC1.FNC1_1ST_POS;

// Set QR Code barcode size;
qr_code.UOM = UnitOfMeasure.PIXEL;
qr_code.X = 4;
qr_code.LeftMargin = 16;
qr_code.RightMargin = 16;
qr_code.TopMargin = 16;
qr_code.BottomMargin = 16;

// Save created QR Code to png file format
qr_code.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
qr_code.drawBarcode("C:\\csharp_qr_code.png");


How to create 2d barcode with data in ECI mode in C# class?

Most 2d barcode symbologies are supporting data encode in Extended Channel Interpretation (ECI) mode. With 2d barcode ECI mode enabled, the 2d barcode symbol can support more data character sets. Using C# Barcode Generator library, you can generating 2d barcode (QR Code, Data Matrix and PDF-417) with data in ECI mode.

In the following C# sample code, we will encode Thai text using ECI in PDF417 barcode.

Key Settings:

  • ProcessTilde: it should be enabled, and set to true.
  • ECI value for Thai: Use ECI 000013 (ISO/IEC 8859-11) for 8-bit data bytes. In OnBarcode ECI solution, the value should be "~7000013".
  • Each Thai text char value fromISO/IEC 8859-11: Eg. ก - 0xA1 - 161
            PDF417 barcode = new PDF417();

            //  Set flag to enable '~' in data message.
            barcode.ProcessTilde = true;

            //  Use ECI 000013 (ISO/IEC 8859-11) for 8-bit data bytes.
            String eci000013 = "~7000013";
            //  ISO/IEC 8859-11 Latin/Thai alphabet
            //  Byte values from 0xA0 to 0xFF are used for Thai characters.
            //  Eg. ก - 0xA1 - 161
            String bytesInISO8859_11 = "~161";

            barcode.Data = eci000013 + bytesInISO8859_11;






How to create barcode with add-on symbol using C#?

A two-digit or five-digit add-on symbol may be used in specific applications to accompany an EAN/UPC, ISBN, ISSN barcode. The add-on symbol is positioned following the right Quiet Zone of the main barcode symbol.

The add-on symbol does not have an explicit check digit.

List of barcode symbols supporting two-digit or five-digit add-on symbol.
  • EAN-8
  • EAN-13
  • UPC-A
  • UPC-E
  • ISBN
  • ISSN
OnBarcode barcode library supports barcode add-on symbols with the following properties.
  • Type. Barcode type for add-on barcode.
  • SupData. Add-on data message. 2 or 5 digits.
  • SupHeight. Add-on symbol height, % of the main symbol. The default is 0.8f (80% of the main symbol height).
  • SubSpace. The space between tha main symbol and add-on symbol. The default is 15 pixel. The maximum space shall be 12X (property X is the bar module width).

The C# source code below explains how to create an EAN-13 barcode with two digits add-on. Here the add-on symbol height is 60% of the main symbol, and the space between two symbol is 30 pixels.
            Linear barcode = new Linear();
            barcode.Type = BarcodeType.EAN13_2;
            barcode.Data = "012345678912"; // 12 or 13 digits
            barcode.SupData = "01";
            barcode.SupHeight = 0.6f;
            barcode.SupSpace = 30;
            barcode.drawBarcode("C://Output//barcode-sample-ean13-2-digit.png");





How to split a large data file into multiple barcodes in C#

Top
PDF417, Data Matrix, QR Code provide a mechanism for the data in a file to be split into blocks and be represented in more than one barcode symbol.

In PDF417, it is named Macro PDF417. In Data Matrix and QR Code, it is called Structure Append.

Each barocde symbol shall contain additional control information to enable the original data file to be properly reconstructed, irrespective of the sequence in which the individual barcode symbols are scanned and decoded.

2D barcodes Data Matrix, PDF-417, QR Code support above feature. Check each barcode symbology guide page for details.




C# Barcode Generation Guide for Linear & 2D Barcode Types

Top

.NET Barcode Component for C# - Barcodes Generation











OnBarcode is a market-leading provider of barcode imaging generator, reader controls and components for ASP.NET, Windows Forms, WPF, as well Java, Android, iOS (iPhone, iPad) across all major enterprise development platforms. We provides comprehensive tutorials and how-tos for various linear, 2d barcode information, such as C# in ASP.NET, C# .NET, C# Barcode Encoding, C# Barcode Image, VB.NET in ASP.NET, VB.NET Winforms, VB.NET Barcode Encoding. OnBarcode barcode products are supported by RasterEdge ASP.NET Document Viewer, which supports ASP.NET PDF Viewer, ASP.NET MVC Document Viewer, ASP.NET PDF Editor, MVC PDF Viewer. And provide high quality C# Convert PDF to Tiff, C# Convert PDF to Word, C# Convert PDF to HTML, C# Convert PDF to Jpeg images, and their easy and simple documents, like C# PDF SDK, C# extract text from PDF, C# Compress PDF, Print PDF in C# and C# extract image from PDF.
Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.