Quick to Generate QR Code using C#
Using C# Barcode Generator library, you can easily generate QR Code symbols in your ASP.NET, MVC, WinForms, WPF .NET web and desktop applications.
After you have installed the C# barcode library into your .net project, you will quickly generate a QR Code in the C# code.
- Create a new
QRCodeobject - Set the QR Code data message to property
Data - Call method
drawBarcode()to draw and print QR Code to a PNG image file
QRCode barcode = new QRCode(); barcode.Data = "https://www.onbarcode.com"; barcode.drawBarcode("C://Output//qrcode-sample.png");
Print QR Code in MemoryStream object
- Call method
drawBarcode()to export, print QR Code to aMemoryStreamobject. You can further process the memory object in your C# application.
Stream barcodeInStream = new MemoryStream(); barcode.drawBarcode(barcodeInStream);
Now we have generated the first QR Code in the C# program successfully. Here we will learn more about QR Code generation in C#.
- QR Code data encoding and data mode
- QR Code dimension width and height customization
- QR Code advanced text encoding (including ASCII non-printable chars, Unicode text)
- QR Code advanced data format encoding (GS1 data elements)
How to Convert Text Characters to QR Code using C#?
QR Code encodable character set
QR Code supports the following data formats :
- Numbers (digits 0-9)
- ASCII characters (plain text). View details here: ASCII table
- Unicode text
- 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.
QR Code Data Mode
To encode the QR Code data text efficiently, QR Code will analyze the input data string, and select the right data modes combinations.
In QR Code generator C# library, there are five data mode options: Auto, Numeric, AlphaNumeric, Byte, Kanji.
In QR Code generator C# library, there are five data mode options: Auto, Numeric, AlphaNumeric, Byte, Kanji.
- QRCodeDataMode.Auto : The data modes will be automatically selected by barcode library
- QRCodeDataMode.Numeric : For numbers (digits 0 - 9)
- QRCodeDataMode.AlphaNumeric : alphanumeric data (digits 0 - 9; upper case letters A -Z; nine other characters: space, $ % * + - . / : )
- QRCodeDataMode.Byte : byte data (default: ISO/IEC 8859-1; or other sets as otherwise defined)
- QRCodeDataMode.Kanji : Kanji characters. Kanji characters in QR Code can be compacted into 13 bits.
/* 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 in QR Code
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
How to Create QR Code with various Text formats using C#?
Create QR Code with ASCII non-printing characters using C#
When we are creating QR Code with ASCII text data, there are several special (control) characters which do not have character labels. Here we will instruct how to
encode these non-printing ASCII characters into QR Code using C# QR Code Generator library.
Key property settings to encode non printable characters '[CR]' (Carriage Return) using QR Code C# barcode generator library:
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; barcode.drawBarcode("W://Projects//Test-Output//OnBarcode.com//csharp-qrcode-mode-non-print.png");
Encode Unicode text in QR Code barcode using C#
QR Code barcode does not support international text (Unicode 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
- Use ECI to encode international text in QR Code barcode
Here we will explain how to create QR Code with Unicode text encoded using C# QR Code library.
Key property settings to encode international characters in QR Code C# barcode generator library:
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; barcode.drawBarcode("C://Projects//Test-Output//OnBarcode.com//csharp-qrcode-mode-thai.png");
Generate GS1 QR Code images with GS1 data message encoded 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.
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; barcode.drawBarcode("C://Projects//Test-Output//OnBarcode.com//csharp-qrcode-mode-gs1.png");
Create QR Code with binary data encode using C#
Most of the 2d barcode symbologies (including QR Code) supports binary data encoding. Using C# Barcode library, you can easily
create QR Code images with binary data encoded using C#.
You can find the detailed C# sample code here How to create 2d barcode with binary data encode using C#?
You can find the detailed C# sample code here How to create 2d barcode with binary data encode using C#?
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; barcode.drawBarcode("C://Projects//Test-Output//OnBarcode.com//csharp-qrcode-mode-kanji.png");
Create and Customize QR Code Image Dimension Size in C#
QR Code image dimension size in C#
Using C# QR Code Generator library, you can print QR Code with customized dimension size through property
BarcodeWidth
- Set property
BarcodeWidthto create a square QR Code with width size applied.
QRCode barcode = new QRCode(); barcode.Data = "https://www.onbarcode.com"; barcode.BarcodeWidth = 200; barcode.drawBarcode("C://Output//qrcode-dimension-size.png");
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:
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;
How to print QR Code with customized Colors and Image options using C#?
Using C# QR Code Generator library, you can easily create, print QR Code with colors, image options customized in C# program.
- Create a new QRCode object
- Customize QR Code bar and space colors through ForeColor (bar color) and BackColor (space color) properties in C# class.
- Call drawBarcode() method to draw and print QR Code to an image file or in memory
QRCode barcode = new QRCode(); barcode.Data = "OnBarcode.com"; barcode.ForeColor = Color.Red; barcode.BackColor = Color.White; barcode.drawBarcode("C://Output//qrcode-colors.png");
You can also easy to create high resolution QR Code images for printing, or create QR Code image with tranparency background in C#.
You can find all details here : How to create, customize QR Code and barcode images in C# ASP.NET, Windows applications?
You can find all details here : How to create, customize QR Code and barcode images in C# ASP.NET, Windows applications?
How to create, print QR Code with options in C# ASP.NET, Windows project?
QR Code error correction mode using 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% (default)
- M: 15%
- Q: 25%
- H: 30%
Using QR Code generator C# library, you shall set QR Code error correction mode through property "ECL". The default value is QRCodeECL.L. 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 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.
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;
QR Code Structured Append mode using C#
A large QR Code data message can be divided and stored in up to 16 QR Code barcodes in a QR Code structured format mode.
In QR Code generator C# library, to implement Structured Append feature, you should apply the following property options :
- StructuredAppend: Set value to true, to enable Structured Append mode in QR Code
- 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
QR Code FNC1 in first position
This QR Code FNC1 mode indicator identifies the current QR Code encoding data formatted according to the GS1 Application Identifiers standard.
To enable this mode using C# QR Code generator library, you need apply the following property in the C# source code.
To enable this mode using C# QR Code generator library, you need apply the following property in the C# source code.
- Set property
FNC1with valueFNC1.FNC1_1ST_POS
QRCode barcode = new QRCode(); barcode.FNC1 = FNC1.FNC1_1ST_POS;
QR Code Extended Channel Interpretation (ECI) mode in C#
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:
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; // Use ECI 000013 (ISO/IEC 8859-11) for 8-bit data bytes. barcode.ECI = 13; // Set flag to enable '~' in data message. barcode.ProcessTilde = true; // ISO/IEC 8859-11 Latin/Thai alphabet // Byte values from 0xA0 to 0xFF are used for Thai characters. // Eg. ก - 0xA1 - 161 barcode.Data = "~161"; barcode.drawBarcode(@"C:\QRCode_ECI000013.png");
Tutorials: How to Create, Print QR Codes in C# .NET Projects?
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
QR Code Data Options
| Category | Properties | Value | Comments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic | Property: Data
URL: DATA |
Type: string
Default: "QRCode" |
Barcode value to encode
QRCode Valid Data Char Set:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| QRCode Special |
Property: ProcessTilde
URL: PROCESS-TILDE |
Type: bool
Default: true |
Set the ProcessTilde property to true, if you want use the tilde character "~" to specify special characters in the input data. Default is true.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: DataMode
URL: DATA-MODE |
Type: QRCodeDataMode
Default: QRCodeDataMode.Auto (0) |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: ECL
URL: ECL |
Type: QRCodeECL
Default: QRCodeECL.L (0) |
QRCode Error Correction Level. Default is QRCodeECL.L (0).
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Property: FNC1
URL: FNC1 |
Type: FNC1
Default: FNC1. FNC1_NONE (0) |
To encode GS1 compatible QR-Code barcode, you need set FNC1 value to FNC1.FNC1_1ST_POS (1). | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: StructuredAppend
URL: STRUCTURED-APPEND |
Type: bool
Default: false |
Set StructuredAppend property to true, then Structured Append is enabled. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: SymbolCount
URL: SYMBOL-COUNT |
Type: int
Default: 0 |
the number of total symbols which make the sequence. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: SymbolIndex
URL: SYMBOL-INDEX |
Type: int
Default: 0 |
the position of current symbol in the secuence (Start with 0). | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Parity
URL: PARITY |
Type: int
Default: 0 |
- | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Version
URL: VERSION |
Type: QRCodeVersion
Default: QRCodeVersion.V1 (1). |
Valid values are from V1 to V40. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: Logo | Type: System.Drawing.Bitmap
Default: null |
A Bitmap object containing logo image | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Property: LogoActualSize | Type: System.Drawing.SizeF | Logo displayed area on QR Code | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
QR Code Image Dimension Size, Color, Image related options
You can click here to view the
QR Code image dimension size, color, Image related option in C#
Summary
OnBarcode C# Barcode Generator makes it easy to generate, create QR Code and other linear & 2d barcodes in Microsoft Word.
OnBarcode provides several QR Code Generator components and software, including QR Code .NET, QR Code Java, QR Code VB.NET, QR Code ASP.NET, QR Code Excel, QR Code Word, QR Code Generator, Online QR Code Barcode Generator. This page is providing a detailed C# source code about generating QR Code barcodes in C# class using C# Barcode generation component.
OnBarcode provides several QR Code Generator components and software, including QR Code .NET, QR Code Java, QR Code VB.NET, QR Code ASP.NET, QR Code Excel, QR Code Word, QR Code Generator, Online QR Code Barcode Generator. This page is providing a detailed C# source code about generating QR Code barcodes in C# class using C# Barcode generation component.
