Encode QR Code data message using C#
Tutorial for QR Code barcode generation in C#
Generate 2d barcode QR Code images in Visual C# .NET with complete sample C# source code
In this C# tutorial page, you will learn how to create QR Code with valid data encoding in ASP.NET, WinForms, WPF applications using C#.NET.
- QR Code data encode with right data mode
- GS1 QR Code data message encode
- Besides Kanji (Japanese) characters, QR Code also supports international characters, such as Thai, Chinese chars
- QR Code special characters encoding
- Support .NET 7, 6, 5, .NET Core 3.1, 2.1 and .NET Framework 4.x, 3.x, 2.x
- Word with C#, Visual Basic .NET, ASP.NET Core, MVC, Web Services, Console & Windows Applications.
How to create QR Code barcodes in ASP.NET MVC and Desktop application using C#
C# QR Code Generator Introduction
Top
QR Code, also known as Denso Barcode, QRCode, Quick Response Code, is a kind of 2D (two dimensional) barcode widely used today.
C# QR Code barcode is .NET barcode generating SDK, which is a user-friendly, light weight barcode component for making QR Code barcodes in C#.NET Winforms application, C#.NET ASP.NET Web forms applications, C#.NET class and console applications.
This document provides 100% C# source code to explain how to encode QR Code valid data with .NET Barcode suite using C# class.
OnBarcode C# Barcode Generator is designed to generate, create QR Code and other linear & 2d barcodes in Microsoft Word.
Here are some more tutorials for C# QR Code generation concerning
size &
image setting.
QR Code Barcode Basic Characteristics
Top
Encode QR Code Valid Character in C#.NET
QR Code valid character set:
- Numeric data. Quick to encode digits.
- Alphanumeric data. Encode digits and letters only.
- Byte characters. Perfect method to encode special characters, such as Unicode
- Kanji characters. For Japanese characters only.
Encode Alphanumeric Character into QR Code Using C#.NET class
It allows encoding alphanumeric data, including digits 0 - 9; upper case letters A -Z; and nine other characters: space, $ % * + - . / : .
QRCode barcode = new QRCode();
barcode.Data = "OnBarcode";
barcode.DataMode = QRCodeDataMode.Alphanumeic;
barcode.Format = ImageFormat.Png;
barcode.drawBarcode("c:/qrcode.png");
Encode Numeric into QR Code Using C#.NET class
It allows encoding numeric data (digits 0 - 9).
QRCode barcode = new QRCode();
barcode.Data = "123456789";
barcode.DataMode = QRCodeDataMode.Numeric;
barcode.Format = ImageFormat.Png;
barcode.drawBarcode("c:/qrcode.png");
Encode Byte Characters into QR Code Using C#.NET class
It allows encoding byte data (default: ISO/IEC 8859-1).
QRCode barcode = new QRCode();
barcode.Data = "$ % * + - . / ";
barcode.DataMode = QRCodeDataMode.Byte;
barcode.Format = ImageFormat.Png;
barcode.drawBarcode("c:/qrcode.png");
Encode Kanji Characters into QR Code Using C#.NET class
It allows encoding Kanji characters. Please see SJIS (Shift JIS encoding): from ~9ddddd (Shift JIS 0x8140 ~ 0x9FFC and 0xE040 ~ 0xEBBF)
QRCode barcode = new QRCode();
barcode.Data = "~937727";
barcode.ProcessTild = True;
barcode.DataMode = QRCodeDataMode.Kanji;
barcode.Format = ImageFormat.Png;
barcode.drawBarcode("c:/qrcode.png");
Modify QR Code Valid Length in C#.NET
QR Code is variable-length 2-dimensional barcodes, the storage capacity see the table below:
Data Type |
Maximum Data Storage Capacity |
Alphanumeric Data |
4,296 characters |
Numeric Data |
7,089 characters |
Byte Character |
2,953 characters |
Kanji Character |
1,817 characters |
The length of QR Code is determined by the length of encoding data, for example:
barcode.Data = "1"; // in 1-digit length
barcode.Data = "123456789"; // in 9-digit length
barcode.Data = "12345678901234567890"; // in 20-digit length
QR Code maximum data length
Maximum data characters per QR Code symbol with Version 40-L
- numeric data: 7,089 characters
- alphanumeric data: 4,296 characters
- byte data: 2,953 characters
- Kanji data: 1,817 characters
Generate GS1 QR Code barcode images using C#
The foundational GS1 standard that defines how identification keys, data attributes and barcodes must be used in business applications.
GS1 QR Code barcode is a standalone, two-dimensional matrix symbology that is made up of square modules arranged in an overall square pattern, including a unique finder pattern located at three corners of the symbol.
QR Code is the only member of the QR Code family that supports GS1 system data structures, including Function 1 Symbol Character. ISO/IEC QR Code also contains specifications for Micro QR Code,
but this symbology is not supported for the GS1 system. QR Code uses Reed-Solomon error correction (four selectable levels of error correction are specified), and this feature helps correct for partially damaged symbols.
GS1 QR Code symbols are read by two-dimensional imaging scanners or vision systems. Most other scanners that are not two-dimensional imagers cannot read GS1 QR Code. GS1 QR Code symbols are restricted for use with applications that will involve imaging scanners throughout the supply chain.
In QR Code generator C# library, there are two key properties to create GS1 QR Code.
- FNC1: Value should be "FNC1.FNC1_1ST_POS"
- Data: GS1 data should be pair of Application Identifier code (AI code) and data message (AI data), and AI code should be surrounded by parentheses. For example: GS1 data message "(17)050101(10)ABC123"
Sample C# source code to encode GS1 QR Code barcode
QRCode barcode = new QRCode();
// It could encode GS1 element(s) by inserting a FNC1 symbol before all data characters.
// Each element contains a GS1 prefix (in parentheses) and fixed (or variable) length data content.
// Set FNC1 to FNC1.FNC1_1ST_POS to enable this feature.
barcode.Data = "(17)050101(10)ABC123";
barcode.FNC1 = FNC1.FNC1_1ST_POS;
barcode.DataMode = QRCodeDataMode.Auto;
// Selecte format mode
barcode.Version = QRCodeVersion.V3;
// Barcode Size Related Settings
barcode.UOM = UnitOfMeasure.PIXEL;
barcode.X = 5;
barcode.LeftMargin = 50;
barcode.RightMargin = 50;
barcode.TopMargin = 50;
barcode.BottomMargin = 50;
barcode.Resolution = 96;
// Image format setting
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
barcode.drawBarcode("C://Projects//Test-Output//OnBarcode.com//csharp-qrcode-mode-gs1.png");
How to generate QR Code with International text using C#?
QR Code barcode does not support international text by default. If you need encode Arabic, Greek, Thai text. There are two solutions for you.
- Convert internation text to byte array using UTF-8 encode, and generate QR Code using byte mode
Key property settings to encode international characters in QR Code C# barcode generator library:
- ProcessTilde : Set value to true, to enable '~' in data message.
- DataMode: It should be QRCodeDataMode.Byte
- Data: The non-English text should be converted to byte array using UTF8 encoding.
Sample C# source code to encode Thai Text in QR Code barcode
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;
// Selecte format mode
barcode.Version = QRCodeVersion.V3;
// Barcode Size Related Settings
barcode.UOM = UnitOfMeasure.PIXEL;
barcode.X = 5;
barcode.LeftMargin = 50;
barcode.RightMargin = 50;
barcode.TopMargin = 50;
barcode.BottomMargin = 50;
barcode.Resolution = 96;
// Image format setting
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
barcode.drawBarcode("C://Projects//Test-Output//OnBarcode.com//csharp-qrcode-mode-thai.png");
How to create QR Code barcode with non-printable chars (such as '[CR]' (Carriage Return)) using C#?
To encode QR Code with non-printable or non-visible chars, such as '[CR]' (Carriage Return), you need generate QR Code with byte data mode (QRCodeDataMode.Auto or QRCodeDataMode.Byte). Here are the sample property settings
- ProcessTilde : Set value to true, to enable '~' in data message.
- DataMode: It should be QRCodeDataMode.Auto or QRCodeDataMode.Byte
- Data: The non printable character should be converted to three digits (in ASCII value), such as '~013'.
Sample C# source code to encode non printable chars '[CR]' (Carriage Return) in QR Code
QRCode barcode = new QRCode();
// It could encode non-printable chars by converting char ascii value to THREE digits, in format "~ddd",
// Set ProcessTilde to true to enable this feature.
barcode.Data = "~013"; // char '[CR]' or carriage return
barcode.ProcessTilde = true;
barcode.DataMode = QRCodeDataMode.Auto;
// Selecte format mode
barcode.Version = QRCodeVersion.V3;
// Barcode Size Related Settings
barcode.UOM = UnitOfMeasure.PIXEL;
barcode.X = 5;
barcode.LeftMargin = 50;
barcode.RightMargin = 50;
barcode.TopMargin = 50;
barcode.BottomMargin = 50;
barcode.Resolution = 96;
// Image format setting
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
barcode.drawBarcode("W://Projects//Test-Output//OnBarcode.com//csharp-qrcode-mode-non-print.png");