|
C# QR Code Generator Library SDK
Integration & Developer Guide for QR Code 2D barcode image generation in C#
Generate 2d barcode QR Code images in Visual C# .NET with complete sample C# source code
In this C# tutorial, you will learn how to generate, encode QR Code barcode images in ASP.NET, WinForms, WPF applications using C#.NET.
- Support ISO / IEC 18004 standard
- Support Full ASCII characters and Unicode text encoding
- QR Code barcode dimension size
- Customized color setting on QR Code and support full transparent background color
- Simple to split large data message, and encode them into multiple QR Code barcodes
- Quick to encode international text characters using byte array or ECI encoding
- Easy to create and embed QR Code barcode in web pages, Windows image controller, Crystal Reports, SSRS reports and RDLC reports
- Support .NET 8, 7, 6, 5, .NET Core 3.1, 2.1, and .NET Framework 4.x, 3.x, 2.x
- Easy to integrate into your ASP.NET Core, MVC, ASP.NET Framework, WinForms, WPF
How to create QR Code barcodes in ASP.NET and Windows application using C#
C# QR-Code Generator Introduction
Top
QR Code barcode, also known as Denso Barcode, QRCode, Quick Response Code, is a matrix code (or two-dimensional bar code) created by Japanese corporation Denso-Wave in 1994. The "QR" is derived from "Quick Response", as the creator intended the code to allow its contents to be decoded at high speed.
C# QR Code Generator is one of the functions in OnBarcode's Barcode for .NET Generation Controls, which supports generating & printing QR Code and 20+ other linear & 2D bar codes for C# applications.
OnBarcode C# Barcode Generator makes it easy to generate, create QR Code and other linear & 2d barcodes in Microsoft Word.
This document is providing a detailed C# source code about generating QR Code barcodes in C# class
using C# Barcode generation component.
Complete QR Code custmoization settings is included in
C# QR Code generation guide.
QR Code Basic Characteristics
Top
Encode character set & QR Code Data Mode in C#
- Numeric data (digits 0 - 9)
- Alphanumeric data (digits 0 - 9; upper case letters A -Z; nine other characters: space, $ % * + - . / : )
- Byte data (default: ISO/IEC 8859-1; or other sets as otherwise defined)
- Kanji characters. Kanji characters in QR Code can be compacted into 13 bits.
In QR Code generator C# library, there are five data mode options: Auto, Numeric, AlphaNumeric, Byte, Kanji.
If you set the DataMode value to QRCodeDataMode.Auto, C# QR Code generator sdk will choose the best data encoding modes for you.
/*
QRCodeDataMode.Numeric: for numeric QR Code data
QRCodeDataMode.AlphaNumeric: for alphanumeric QR Code data
QRCodeDataMode.Byte: for byte array QR Code data
QRCodeDataMode.Kanji: for Kanji characters QR Code data
*/
barcode.DataMode = QRCodeDataMode.Auto;
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
QR Code error correction mode in C#
Four levels of Reed-Solomon error correction (referred to as L, M, Q and H in increasing order of
capacity) allowing recovery of the QR Code barcode data.
- L: 7%
- M: 15%
- Q: 25%
- H: 30%
Using QR Code generator C# library, you shall set QR Code error correction mode through property "ECL". Here is the sample C# source code.
QRCode barcode = new QRCode();
barcode.Data = "QR Code";
barcode.DataMode = QRCodeDataMode.Auto;
/*
QRCode Error Correction Level. Default is QRCodeECL.L (0).
QRCodeECL.L (0)
QRCodeECL.M (1)
QRCodeECL.Q (2)
QRCodeECL.H (3)
*
*/
barcode.ECL = QRCodeECL.M;
QR Code Barcode Dimension Size in C#
Top
Barcode size & QR Code Version in C#
There are forty sizes of QR Code barcode referred to as Version 1, Version 2 ... Version 40.
View details at
How to set QR Code Versions
Using C# QR Code generator, you shall set property Version with 40 options, from QRCodeVersion.V1 to QRCodeVersion.V40.
// Selecte format mode
// from V1 to V40
barcode.Version = QRCodeVersion.V3;
Quiet Zone
Four sides on the QR Code barcode should be surrounded by quiet zones.
View details at
About QR Code quiet zone
Using C#, you can set quiet zones for QR Code using the following four properties:
- LeftMargin
- RightMargin
- TopMargin
- BottomMargin
barcode.LeftMargin = 50;
barcode.RightMargin = 50;
barcode.TopMargin = 50;
barcode.BottomMargin = 50;
Generate GS1 QR Code barcode images using C#
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.
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.
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");
QR Code Extended Channel Interpretation (ECI) in C#
Top
The Extended Channel Interpretation (ECI) protocol defined in the AIM Inc. ECI allows the output data stream to have interpretations different
from that of the default character set.
The default interpretation for QR Code is ECI 000003 representing the ISO/IEC 8859-1 character set.
In QR Code generator C# library, to change encoding character set using ECI, you need apply the following property:
- ECI: Six digits ECI value for the new character set defined by the AIM. For example: 000013 for ISO/IEC 8859-11
- ProcessTilde: Set the ProcessTilde property to true. You need convert new character data to byte array.
Complete C# sample code to encode Thai text in QR Code barcode, using ECI.
QRCode barcode = new QRCode();
// Set to Byte mode
barcode.DataMode = QRCodeDataMode.Byte;
barcode.ECL = QRCodeECL.M;
// Use ECI 000013 (ISO/IEC 8859-11) for 8-bit data bytes.
barcode.ECI = 13;
// 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";
// Set flag to enable '~' in data message.
barcode.ProcessTilde = true;
barcode.Data = bytesInISO8859_11;
// 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:\QRCode_ECI000013.png");
QR Code Structure Append using C#
Top
A larget QR Code data message can be divided and stored in up to 16 QR Code barcodes in a structured format.
In QR Code generator C# library, to implement Structure Append feature, you should apply the following property settings.
- StructuredAppend: Set value to true, to enable Structured Append
- SymbolCount: Total number of symbols for one QR Code data message
- SymbolIndex: The position of current symbol in the sequence (The first symbol index is 0)
barcode.StructuredAppend = true;
barcode.SymbolCount = 3; // The data message will be stored in three QR Code barcodes
barcode.SymbolIndex = 0; // This symbol is the first one
C# QR Code: Encoding Special Characters
Top
Encode Japanese (Kanji) text in QR Code barcode using C#
Key property settings to encode Kanji characters in QR Code C# barcode generator library:
- ProcessTilde : Set value to true, to enable '~' in data message.
- DataMode: It should be QRCodeDataMode.Kanji
Sample C# source code to encode Japanese (Kanji) text in QR Code barcode.
QRCode barcode = new QRCode();
// Kanji mode encodes Kanji characters defined in JIS X 0208 and valid character values are in the ranges
// 0x8140 ~ 0x9FFC and 0xE040 ~ 0xEBBF. Each character (SJIS value) should be represented in format "~9ddddd",
// which "ddddd" is a decimal number in ranges 33088 (0x8140) ~ 40956 (0x9FFC) and 57408 (0xE040) ~ 60351 (0xEBBF).
// ProcessTilde must be enable to support this feature.
barcode.Data = "~937727~958538"; // 0x935F0xE4AA - "点茗"
barcode.ProcessTilde = true;
barcode.DataMode = QRCodeDataMode.Kanji;
// Selecte format mode
barcode.Version = QRCodeVersion.V1;
// 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-kanji.png");
Encode International (Non-English) text in QR Code barcode 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.
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");
Encode Non-Printable chars in QR Code barcode using C#
Key property settings to encode non printable characters '[CR]' (Carriage Return) using QR Code C# barcode generator library:
- ProcessTilde : Set value to true, to enable '~' in data message.
- DataMode: It should be QRCodeDataMode.Auto
- Data: The non printable character should be converted to three digits (in ASCII value)
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");
Generate QR Code in .NET Projects
Top
Creating QR-Code barcode in C# class example
using System;
using System.Collections.Generic;
using System.Text;
using OnBarcode.Barcode;
using System.Drawing.Imaging;
using System.Drawing;
QRCode qrcode = new QRCode();
// Barcode data to encode
qrcode.Data = "ONBARCODE";
// QR-Code data mode
qrcode.DataMode = QRCodeDataMode.AlphaNumeric;
// QR-Code format mode
//qrcode.Version = QRCodeVersion.V10;
/*
* Barcode Image Related Settings
*/
// Unit of meature for all size related setting in the library.
qrcode.UOM = UnitOfMeasure.PIXEL;
// Bar module size (X), default is 3 pixel;
qrcode.X = 3;
// Barcode image left, right, top, bottom margins. Defaults are 0.
qrcode.LeftMargin = 0;
qrcode.RightMargin = 0;
qrcode.TopMargin = 0;
qrcode.BottomMargin = 0;
// Image resolution in dpi, default is 72 dpi.
qrcode.Resolution = 72;
// Created barcode orientation.
//4 options are: facing left, facing right, facing bottom, and facing top
qrcode.Rotate = Rotate.Rotate0;
// Generate QR-Code and encode barcode to gif format
qrcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Gif;
qrcode.drawBarcode("C:\\qrcode.gif");
/*
You can also call other drawing methods to generate barcodes
public void drawBarcode(Graphics graphics);
public void drawBarcode(string filename);
public Bitmap drawBarcode();
public void drawBarcode(Stream stream);
*/
Creating QR-Code barcode in ASP.NET MVC web applications
- Under demo package, copy barcode folder and its contents to your IIS, and create a new virtual directory.
- Restart IIS, navigate to http://YourDomain:Port/barcode/qrcode.aspx?DATA=0123456789.
- To create barcode QR-Code image in html or aspx pages, you can insert a image tag (img) into your page.
For example,
<img src="http://YourDomain:Port/barcode/qrcode.aspx?DATA=0123456789" />
Creating QR-Code using ASP.NET Barcode Controller
- Add OnBarcode.Barcode.ASPNET.dll to asp.net project reference.
- Add .NET Barcode to .NET Visual Studio Toolbox.
- Right click .NET Visual Studio Toolbox, select menu Choose Items...
- In "Choose Toolbox Items" form, click button "Browse...", and select dll OnBarcode.Barcode.ASPNET.dll.
- After selection, you will find four items under "Components" section: LinearWebForm, DataMatrixWebForm, PDF417WebForm,
and QRCodeWebForm.
Creating QR-Code in C#.NET WinForms Windows Application
- Add OnBarcode.Barcode.WinForms.dll to .net project reference.
- Add .NET Barcode to .NET Visual Studio Toolbox.
- Right click .NET Visual Studio Toolbox, select menu Choose Items...
- In "Choose Toolbox Items" form, click button "Browse...", and select dll OnBarcode.Barcode.WinForms.dll.
- After selection, you will find four items under "Components" section: LinearWinForm, DataMatrixWinForm,
PDF417WinForm, and QRCodeWinForm.
QR Code Barcode Property Settings in C#.NET
Top
More C# Barcode Generation Tutorials for Each Barcode Top Barcode Control for C#.NET - Bar Code Type Generation |