Getting started

edit

Install the package

Add the Meziantou.Framework.Barcodes package to a project, and Meziantou.Framework.Imaging.Barcodes to save the codes as PNG files or other images:

dotnet package add Meziantou.Framework.Barcodes
dotnet package add Meziantou.Framework.Imaging.Barcodes

The examples of this documentation omit the using directives: they need using Meziantou.Framework.Barcodes;, using Meziantou.Framework.VectorGraphics; for the colors, and using Meziantou.Framework.Imaging.Barcodes; for the images.

Generate a QR code

QRCode.Create encodes a text in the smallest QR code that holds it, and ToImage renders it as an image, to save as a PNG file:

var qrCode = QRCode.Create("https://www.meziantou.net");

// ToImage comes from Meziantou.Framework.Imaging.Barcodes: save the image as PNG with image.Save("qrcode.png")
using var image = qrCode.ToImage(new QRCodeRenderOptions { ModuleSize = 4 });

A black and white QR code that encodes the address of a website

  • ModuleSize is the size of a module, the small square of a QR code, in pixels. It is 10 by default.
  • The image includes the quiet zone, the light margin a scanner needs around the code: 4 modules by default.
  • The same options give an SVG document with ToSvg, which needs only Meziantou.Framework.Barcodes, or a PDF document. See SVG, images and PDF.

Generate a barcode

The Barcode.Create* methods create linear barcodes, one method per symbology:

var barcode = Barcode.CreateCode128("SKU-123456");

// ToImage comes from Meziantou.Framework.Imaging.Barcodes: save the image as PNG with image.Save("barcode.png")
using var image = barcode.ToImage(new BarcodeRenderOptions { ModuleHeight = 60 });

A Code 128 barcode

To print the human-readable text under the bars, set Text: see Print the text under a barcode.

Next steps

  • QR codes: the kinds of QR codes, the error correction level and how the data is encoded.
  • Linear barcodes: which symbology to use, check digits and add-ons.
  • QR code content: Wi-Fi networks, contacts, payments and more.