Images
editMeziantou.Framework.Imaging.Barcodes renders codes with Meziantou.Framework.Imaging: save them in any format the library writes, draw them on existing images, and paint them with gradients or patterns. It draws the scene of the code (CreateScene), so an image is the same as the SVG document of the same options, logo and text included: see SVG, images and PDF.
dotnet package add Meziantou.Framework.Imaging.BarcodesThe types are in the Meziantou.Framework.Imaging.Barcodes namespace. The examples also use the Meziantou.Framework.Barcodes, Meziantou.Framework.VectorGraphics, Meziantou.Framework.Imaging, Meziantou.Framework.Imaging.Drawing and Meziantou.Framework.Imaging.Formats namespaces.
Render a code as an image
ToImage renders a QR code or a barcode on a new Image<Rgba32> of its size, quiet zone included:
var qrCode = QRCode.Create("https://www.meziantou.net", ErrorCorrectionLevel.M);
// Default options: 10 pixels per module, a 4-module quiet zone, black on white
using var image = qrCode.ToImage();
Assert.Equal((330, 330), (image.Width, image.Height));
// Save to a file (image.Save("qrcode.webp", new WebPEncoder())) or to a stream
using var webp = new MemoryStream();
image.Save(webp, new WebPEncoder());
// Custom options, the same as the SVG renderer takes: any paint, and no background
using var customImage = qrCode.ToImage(new QRCodeRenderOptions
{
ModuleSize = 4,
QuietZoneModules = 2,
DarkPaint = new SceneColor(0x00, 0x33, 0x66),
LightPaint = null,
});
Assert.Equal((116, 116), (customImage.Width, customImage.Height));
Assert.Equal(Colors.Transparent, customImage.Frames[0][0, 0]);
Assert.Equal(new Rgba32(0x00, 0x33, 0x66), customImage.Frames[0][8, 8]);
// An indexed PNG stores the two colors in a palette
using var png = new MemoryStream();
customImage.Save(png, new PngEncoder { PixelLayout = PngPixelLayout.Indexed });var barcode = Barcode.CreateEan13("5901234123457");
using var image = barcode.ToImage(new BarcodeRenderOptions { ModuleWidth = 3, ModuleHeight = 100 });
Assert.Equal((345, 100), (image.Width, image.Height));Data Matrix, PDF417 and Aztec symbols (Barcode2D) have the same methods, with Barcode2DRenderOptions:
var dataMatrix = Barcode2D.CreateDataMatrix("https://www.meziantou.net");
// Default options: 4 pixels per module and a 2-module quiet zone
using var image = dataMatrix.ToImage();
Assert.Equal((104, 104), (image.Width, image.Height));
// Draw a PDF417 symbol at the bottom of a ticket
var pdf417 = Barcode2D.CreatePdf417("TICKET 0042", new Pdf417Options { Columns = 3 });
var options = new Barcode2DRenderOptions { ModuleSize = 2 };
var size = Barcode2DImageRenderer.MeasureBarcode2D(pdf417, options);
using var ticket = new Image<Rgba32>(400, 200, Colors.White);
ticket.Draw(canvas => canvas.DrawBarcode2D(pdf417, new PointD((400 - size.Width) / 2, 200 - 16 - size.Height), options));Save the image like any other: see Saving images. The last parameter of ToImage takes the options of the image renderer (SceneImageOptions): its Scale draws more pixels per unit, for high-density screens.
Paint with any paint
The modules are painted with the paints of Meziantou.Framework.VectorGraphics, so a code can have a color, a gradient or a pattern. The paints are in the coordinates of the scene of the code: (0, 0) is the top-left corner of its quiet zone, wherever the code is drawn:
var qrCode = QRCode.Create("https://www.meziantou.net", ErrorCorrectionLevel.Q);
// The paints are in the coordinates of the scene: (0, 0) is the top-left corner of the quiet zone, and the
// gradient goes across the whole symbol
var options = new QRCodeRenderOptions
{
ModuleSize = 5,
QuietZoneModules = 2,
DarkPaint = new LinearGradientPaint(0, 0, 165, 165, [new SceneGradientStop(0, new SceneColor(0x7c, 0x3a, 0xed)), new SceneGradientStop(1, new SceneColor(0x0e, 0x74, 0x90))]),
};
using var image = qrCode.ToImage(options);
Keep the modules dark and the background light everywhere: a gradient that gets too light loses the contrast scanners need.
Draw on an existing image
DrawQRCode and DrawBarcode draw on the canvas of Image.Draw, as vector shapes: the transform, the clip and the layers of the canvas apply. MeasureQRCode and MeasureBarcode give the size of the drawing first, to place it:
using var ticket = new Image<Rgba32>(400, 200, Colors.White); // or Image.Load<Rgba32>("ticket.png")
var qrCode = QRCode.Create("TICKET-0042");
var options = new QRCodeRenderOptions { ModuleSize = 4, QuietZoneModules = 2 };
// The size of the drawing, quiet zone included, to lay out the page before drawing
var size = QRCodeImageRenderer.MeasureQRCode(qrCode, options);
Assert.Equal((100, 100), (size.Width, size.Height)); // (21 modules + 2 x 2) x 4 pixels
// Drawn as a vector shape: the transform and the clip of the canvas apply, any brush works
ticket.Draw(canvas => canvas.DrawQRCode(qrCode, new PointD(400 - 16 - size.Width, 16), options));
Assert.Equal(Colors.Black, ticket.Frames[0][284 + 8, 16 + 8]); // the top-left finder pattern, after the 8-pixel quiet zone
// Save to a file (ticket.Save("ticket-with-qrcode.png")) or to a stream
using var png = new MemoryStream();
ticket.Save(png, new PngEncoder());
With SnapToPixels (the default), the module sizes and the position are rounded to whole pixels, so that every module has the same size and sharp edges. Turn it off to draw a code at an exact fractional size, for instance under a scale transform.
Print the text under a barcode
ToImage and DrawBarcode print the human-readable text of the barcode (Barcode.Text, with its check digit) under the bars when BarcodeRenderOptions.Text is set, as the SVG renderer does:
var barcode = Barcode.CreateEan13("590123412345");
var options = new BarcodeRenderOptions
{
ModuleWidth = 2,
ModuleHeight = 60,
QuietZoneModules = 9,
DarkPaint = new SceneColor(0x00, 0x33, 0x66),
Text = new BarcodeTextOptions { Fonts = BarcodeFonts.OcrB, FontSize = 24 }, // prints the digits of barcode.Text in groups under the bars
};
var size = BarcodeImageRenderer.MeasureBarcode(barcode, options);
Assert.Equal(226, size.Width); // (95 modules + 2 x 9) x 2 pixels
using var label = new Image<Rgba32>(300, 140, Colors.White);
label.Draw(canvas => canvas.DrawBarcode(barcode, new PointD(20, 20), options));
Assert.Equal(new Rgba32(0x00, 0x33, 0x66), label.Frames[0][20 + 18, 50]); // the first guard bar
BarcodeFonts has the two fonts of optical character recognition usually printed under barcodes: OcrB under EAN and UPC barcodes, and OcrA. They are font collections of Meziantou.Framework.VectorGraphics; load their TrueType file in a Font of Meziantou.Framework.Imaging to draw any text with them:
// The TrueType files of the fonts, loaded with Meziantou.Framework.Imaging
var ocrA = Font.Load(BarcodeFonts.OcrA.Faces[0].Data.Span);
var ocrB = Font.Load(BarcodeFonts.OcrB.Faces[0].Data.Span);
using var image = new Image<Rgba32>(280, 72, Colors.White);
image.Draw(canvas =>
{
var ink = new SolidBrush(Colors.Black);
canvas.FillText(ink, "OCR-A 0123456789", ocrA, size: 24, new PointD(8, 6));
canvas.FillText(ink, "OCR-B 0123456789", ocrB, size: 24, new PointD(8, 38));
});
- Both fonts are generated from the character shapes of their standards (ANSI X3.17-1981 for OCR-A, ANSI X3.49-1975 for OCR-B), are monospaced, map the printable ASCII characters, and are licensed with the package.
- OCR-A also has the Hook, the Chair and the Fork (U+2440 to U+2442), which stand for the tilde, the grave accent and the underline. OCR-B also has the letters of its extended repertoire (Ä, Å, Æ, Ö, Ø, Ü, Ñ, IJ, å, æ, ø, ij, ß) and §, ¤, £, ¥, ¨ and ´.
- Any other font works too: see Fonts.
Options
ToImage, Measure* and the canvas drawing take the options of every renderer: see the options. The sizes are in units of the canvas: pixels, unless a transform or a Scale applies. A null light paint keeps the content under the code when drawing on a canvas, and gives a transparent background with ToImage.