BarcodeImageRenderer Class

Provides methods to render a barcode as an Image<TPixel>.

public static class BarcodeImageRenderer

Methods

NameDescription
DrawBarcode(DrawingCanvas, Barcode, PointD, BarcodeDrawingOptions?)Draws the barcode on a canvas, as a vector shape, with its human-readable text when a font is set.
MeasureBarcode(Barcode, BarcodeDrawingOptions?)Measures the rectangle that BarcodeImageRenderer.DrawBarcode covers, quiet zone and text included.
ToImage(Barcode, BarcodeDrawingOptions?)Renders the barcode as an image: what BarcodeImageRenderer.DrawBarcode draws on a canvas of the size BarcodeImageRenderer.MeasureBarcode gives, rounded up.

DrawBarcode(DrawingCanvas, Barcode, PointD, BarcodeDrawingOptions?)

BarcodeImageRenderer.cs

Draws the barcode on a canvas, as a vector shape, with its human-readable text when a font is set.

public static RectangleD DrawBarcode(this DrawingCanvas canvas, Barcode barcode, PointD position, BarcodeDrawingOptions? options = null)

Parameters

canvas DrawingCanvas
The canvas to draw on.
barcode Barcode
The barcode to draw.
position PointD
The top-left corner of the drawing. It is rounded to whole units when BarcodeDrawingOptions.SnapToPixels is set.
options BarcodeDrawingOptions?
The drawing options, or null for the defaults.

Returns

RectangleD The rectangle the drawing covers, quiet zone and text included.

Exceptions

ArgumentNullException
barcode or BarcodeDrawingOptions.DarkBrush is null.
ArgumentException
A coordinate of position is not a finite number.
ArgumentOutOfRangeException
An option is out of range, or the magnitude of a coordinate of the drawing exceeds 16777216.
InvalidOperationException
The callback that received the canvas has returned.
InvalidImageContentException
The data of a glyph of the text is malformed.
UnsupportedImageFeatureException
The outlines of the font use a feature that is not supported.
ImageResourceLimitException
The text exceeds a limit of the configuration of the font, or the working storage would exceed the allocation limit.
OperationCanceledException
The drawing was canceled.

Remarks

The barcode is drawn in the coordinates of the canvas, so the transform, clip and layers of the canvas apply. The bars are one shape, painted with BarcodeDrawingOptions.DarkBrush. The text (Barcode.Text) is centered under the bars; a text wider than the barcode widens the drawing, and the barcode is centered above it. Use BarcodeImageRenderer.MeasureBarcode to know the size before drawing.

MeasureBarcode(Barcode, BarcodeDrawingOptions?)

BarcodeImageRenderer.cs

Measures the rectangle that BarcodeImageRenderer.DrawBarcode covers, quiet zone and text included.

public static RectangleD MeasureBarcode(Barcode barcode, BarcodeDrawingOptions? options = null)

Parameters

barcode Barcode
The barcode to measure.
options BarcodeDrawingOptions?
The drawing options, or null for the defaults.

Returns

RectangleD The size of the drawing, as a rectangle located at the origin.

Exceptions

ArgumentNullException
barcode is null.
ArgumentOutOfRangeException
An option is out of range.
InvalidImageContentException
The data of a glyph of the text is malformed.
UnsupportedImageFeatureException
The outlines of the font use a feature that is not supported.
ImageResourceLimitException
The text exceeds a limit of the configuration of the font.

ToImage(Barcode, BarcodeDrawingOptions?)

BarcodeImageRenderer.cs

Renders the barcode as an image: what BarcodeImageRenderer.DrawBarcode draws on a canvas of the size BarcodeImageRenderer.MeasureBarcode gives, rounded up.

public static Image<Rgba32> ToImage(this Barcode barcode, BarcodeDrawingOptions? options = null)

Parameters

barcode Barcode
The barcode to render.
options BarcodeDrawingOptions?
The drawing options, or null for the defaults.

Returns

Image<Rgba32> A single-frame image. The caller owns it and must dispose it.

Exceptions

ArgumentNullException
barcode or BarcodeDrawingOptions.DarkBrush is null.
ArgumentOutOfRangeException
An option is out of range, or the image dimensions are too large.
ImageResourceLimitException
The image exceeds a limit of ImageConfiguration.Default.

Remarks

With the default BarcodeDrawingOptions.SnapToPixels, the modules are whole pixels and the image has two colors when the brushes are solid. With a null BarcodeDrawingOptions.LightBrush, the background is transparent.