Show / Hide Table of Contents

Class QrCode

Represents a QR code containing text or binary data.

Instances of this class represent an immutable square grid of dark and light pixels (called modules by the QR code specification). Static factory methods are provided to create QR codes from text or binary data. Some of the methods provide detailed control about the encoding parameters such a QR code size (called version by the standard), error correction level and mask.

QR codes are a type of two-dimensional barcodes, invented by Denso Wave and described in the ISO/IEC 18004 standard.

This class covers the QR Code Model 2 specification, supporting all versions (sizes) from 1 to 40, all 4 error correction levels, and 4 character encoding modes.

Inheritance
System.Object
QrCode
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Net.Codecrete.QrCodeGenerator
Assembly: QrCodeGenerator.dll
Syntax
public class QrCode
Remarks

To create a QR code instance:

  • High level: Take the payload data and call EncodeText(String, QrCode.Ecc) or EncodeBinary(Byte[], QrCode.Ecc).
  • Mid level: Custom-make a list of QrSegment instances and call EncodeSegments(List<QrSegment>, QrCode.Ecc, Int32, Int32, Int32, Boolean)
  • Low level: Custom-make an array of data codeword bytes (including segment headers and final padding, excluding error correction codewords), supply the appropriate version number, and call the QrCode(Int32, QrCode.Ecc, Byte[], Int32).

Constructors

| Improve this Doc View Source

QrCode(Int32, QrCode.Ecc, Byte[], Int32)

Constructs a QR code with the specified version number, error correction level, data codeword bytes, and mask number.

Declaration
public QrCode(int version, QrCode.Ecc ecl, byte[] dataCodewords, int mask = -1)
Parameters
Type Name Description
System.Int32 version

The version (size) to use (between 1 to 40).

QrCode.Ecc ecl

The error correction level to use.

System.Byte[] dataCodewords

The bytes representing segments to encode (without ECC).

System.Int32 mask

The mask pattern to use (either -1 for automatic selection, or a value from 0 to 7 for fixed choice).

Remarks

This is a low-level API that most users should not use directly. A mid-level API is the EncodeSegments(List<QrSegment>, QrCode.Ecc, Int32, Int32, Int32, Boolean) function.

Exceptions
Type Condition
System.ArgumentNullException

ecl or dataCodewords is null.

System.ArgumentOutOfRangeException

The version or mask value is out of range, or the data has an invalid length for the specified version and error correction level.

Fields

| Improve this Doc View Source

MaxVersion

The maximum version (size) supported in the QR Code Model 2 standard – namely 40.

Declaration
public const int MaxVersion = 40
Field Value
Type Description
System.Int32

The maximum version.

| Improve this Doc View Source

MinVersion

The minimum version (size) supported in the QR Code Model 2 standard – namely 1.

Declaration
public const int MinVersion = 1
Field Value
Type Description
System.Int32

The minimum version.

Properties

| Improve this Doc View Source

ErrorCorrectionLevel

The error correction level used for this QR code.

Declaration
public QrCode.Ecc ErrorCorrectionLevel { get; }
Property Value
Type Description
QrCode.Ecc

The error correction level.

| Improve this Doc View Source

Mask

The index of the mask pattern used fort this QR code (between 0 and 7).

Even if a QR code is created with automatic mask selection (mask = 1), this property returns the effective mask used.

Declaration
public int Mask { get; }
Property Value
Type Description
System.Int32

The mask pattern index.

| Improve this Doc View Source

Size

The width and height of this QR code, in modules (pixels). The size is a value between 21 and 177. This is equal to version × 4 + 17.

Declaration
public int Size { get; }
Property Value
Type Description
System.Int32

The QR code size.

| Improve this Doc View Source

Version

The version (size) of this QR code (between 1 for the smallest and 40 for the biggest).

Declaration
public int Version { get; }
Property Value
Type Description
System.Int32

The QR code version (size).

Methods

| Improve this Doc View Source

EncodeBinary(Byte[], QrCode.Ecc)

Creates a QR code representing the specified binary data using the specified error correction level.

This function encodes the data in the binary segment mode. The maximum number of bytes allowed is 2953. The smallest possible QR code version is automatically chosen. The resulting ECC level will be higher than the one specified if it can be achieved without increasing the size (version).

Declaration
public static QrCode EncodeBinary(byte[] data, QrCode.Ecc ecl)
Parameters
Type Name Description
System.Byte[] data

The binary data to encode.

QrCode.Ecc ecl

The minimum error correction level to use.

Returns
Type Description
QrCode

The created QR code representing the specified data.

Exceptions
Type Condition
System.ArgumentNullException

data or ecl is null.

DataTooLongException

The specified data is too long to fit in the largest QR code size (version) at the specified error correction level.

| Improve this Doc View Source

EncodeSegments(List<QrSegment>, QrCode.Ecc, Int32, Int32, Int32, Boolean)

Creates a QR code representing the specified segments with the specified encoding parameters.

The smallest possible QR code version (size) is used. The range of versions can be restricted by the minVersion and maxVersion parameters.

If boostEcl is true, the resulting ECC level will be higher than the one specified if it can be achieved without increasing the size (version).

The QR code mask is usually automatically chosen. It can be explicitly set with the mask parameter by using a value between 0 to 7 (inclusive). -1 is for automatic mode (which may be slow).

This function allows the user to create a custom sequence of segments that switches between modes (such as alphanumeric and byte) to encode text in less space and gives full control over all encoding parameters.

Declaration
public static QrCode EncodeSegments(List<QrSegment> segments, QrCode.Ecc ecl, int minVersion = 1, int maxVersion = 40, int mask = -1, bool boostEcl = true)
Parameters
Type Name Description
System.Collections.Generic.List<QrSegment> segments

The segments to encode.

QrCode.Ecc ecl

The minimal or fixed error correction level to use .

System.Int32 minVersion

The minimum version (size) of the QR code (between 1 and 40).

System.Int32 maxVersion

The maximum version (size) of the QR code (between 1 and 40).

System.Int32 mask

The mask number to use (between 0 and 7), or -1 for automatic mask selection.

System.Boolean boostEcl

If true the ECC level wil be increased if it can be achieved without increasing the size (version).

Returns
Type Description
QrCode

The created QR code representing the segments.

Remarks

This is a mid-level API; the high-level APIs are EncodeText(String, QrCode.Ecc) and EncodeBinary(Byte[], QrCode.Ecc).

Exceptions
Type Condition
System.ArgumentNullException

segments, any list element, or ecl is null.

System.ArgumentOutOfRangeException

1 ≤ minVersion ≤ maxVersion ≤ 40 or -1 ≤ mask ≤ 7 is violated.

DataTooLongException

The segments are too long to fit in the largest QR code size (version) at the specified error correction level.

| Improve this Doc View Source

EncodeText(String, QrCode.Ecc)

Creates a QR code representing the specified text using the specified error correction level.

As a conservative upper bound, this function is guaranteed to succeed for strings with up to 738 Unicode code points (not UTF-16 code units) if the low error correction level is used. The smallest possible QR code version (size) is automatically chosen. The resulting ECC level will be higher than the one specified if it can be achieved without increasing the size (version).

Declaration
public static QrCode EncodeText(string text, QrCode.Ecc ecl)
Parameters
Type Name Description
System.String text

The text to be encoded. The full range of Unicode characters may be used.

QrCode.Ecc ecl

The minimum error correction level to use.

Returns
Type Description
QrCode

The created QR code instance representing the specified text.

Exceptions
Type Condition
System.ArgumentNullException

text or ecl is null.

DataTooLongException

The text is too long to fit in the largest QR code size (version) at the specified error correction level.

| Improve this Doc View Source

GetModule(Int32, Int32)

Gets the color of the module (pixel) at the specified coordinates.

The top left corner has the coordinates (x=0, y=0). x-coordinates extend from left to right, y-coordinates extend from top to bottom.

If coordinates outside the bounds of this QR code are specified, light (false) is returned.

Declaration
public bool GetModule(int x, int y)
Parameters
Type Name Description
System.Int32 x

The x coordinate.

System.Int32 y

The y coordinate.

Returns
Type Description
System.Boolean

The color of the specified module: true for dark modules and false for light modules (or if the coordinates are outside the bounds).

| Improve this Doc View Source

RgbColor(Byte, Byte, Byte)

Creates an RGB color value in little endian format.

Declaration
public int RgbColor(byte red, byte green, byte blue)
Parameters
Type Name Description
System.Byte red

Red component.

System.Byte green

Green component.

System.Byte blue

Blue component.

Returns
Type Description
System.Int32

RGB color value

| Improve this Doc View Source

ToBmpBitmap(Int32, Int32)

Creates bitmap in the BMP format data using black for dark modules and white for light modules.

The bitmap uses 1 bit per pixel and a color table with 2 entries.

Declaration
public byte[] ToBmpBitmap(int border = 0, int scale = 1)
Parameters
Type Name Description
System.Int32 border

The border width, as a factor of the module (QR code pixel) size.

System.Int32 scale

The width and height, in pixels, of each module.

Returns
Type Description
System.Byte[]

Bitmap data

Exceptions
Type Condition
System.ArgumentOutOfRangeException

Thrown if border is negative, scale is less than 1 or the resulting image is wider than 32,768 pixels.

| Improve this Doc View Source

ToBmpBitmap(Int32, Int32, Int32, Int32)

Creates a bitmap in the BMP format.

The bitmap uses 1 bit per pixel and a color table with 2 entries.

Color values can be created with RgbColor(Byte, Byte, Byte).

Declaration
public byte[] ToBmpBitmap(int border, int scale, int foreground, int background)
Parameters
Type Name Description
System.Int32 border

The border width, as a factor of the module (QR code pixel) size.

System.Int32 scale

The width and height, in pixels, of each module.

System.Int32 foreground

The foreground color (dark modules), in RGB value (little endian).

System.Int32 background

The background color (light modules), in RGB value (little endian).

Returns
Type Description
System.Byte[]

Bitmap data

Exceptions
Type Condition
System.ArgumentOutOfRangeException

Thrown if border is negative, scale is less than 1 or the resulting image is wider than 32,768 pixels.

| Improve this Doc View Source

ToGraphicsPath(Int32)

Creates a graphics path of this QR code valid in SVG or XAML.

The graphics path uses a coordinate system where each module is 1 unit wide and tall, and the top left module is offset vertically and horizontally by border units.

Note that a border width other than 0 only make sense if the bounding box of the QR code is explicitly set by the graphics using this path. If the bounding box of this path is automatically derived, at least the right and bottom border will be missing.

The path will look like this: M3,3h7v1h-7z M12,3h1v4h-1z ... M70,71h1v1h-1z. It is valid for SVG (<path d="M3,3h..." />) and for XAML (<Path Data="M3,3h..." />). For programmatic geometry creation in WPF see Geometry.Parse(String).

Declaration
public string ToGraphicsPath(int border = 0)
Parameters
Type Name Description
System.Int32 border

The border width, as a factor of the module (QR code pixel) size

Returns
Type Description
System.String

The graphics path

Exceptions
Type Condition
System.ArgumentOutOfRangeException

Thrown if border is negative

| Improve this Doc View Source

ToSvgString(Int32)

Creates an SVG image of this QR code.

The images uses Unix newlines (\n), regardless of the platform.

Declaration
public string ToSvgString(int border)
Parameters
Type Name Description
System.Int32 border

The border width, as a factor of the module (QR code pixel) size

Returns
Type Description
System.String

The SVG image as a string.

| Improve this Doc View Source

ToSvgString(Int32, String, String)

Creates an SVG image of this QR code.

The images uses Unix newlines (\n), regardless of the platform.

Colors are specified using CSS color data type. Examples of valid values are "#339966", "fuchsia", "rgba(137, 23, 89, 0.3)".

Declaration
public string ToSvgString(int border, string foreground, string background)
Parameters
Type Name Description
System.Int32 border

The border width, as a factor of the module (QR code pixel) size

System.String foreground

The foreground color.

System.String background

The background color.

Returns
Type Description
System.String

See Also

QrSegment
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX