The BarcodeOptions class provides configuration options for customizing barcode appearance, behavior, and font settings. Options are configured using an action delegate pattern.
Important: Once a Barcode instance is created, the options are automatically locked and cannot be modified. This ensures thread safety and prevents configuration changes after the encoder has been initialized.
var options = new BarcodeOptions();
options.Type = BarcodeTypes.Ean13; // This works
using var barcode = new Barcode(opt => opt.Type = BarcodeTypes.Ean13);
// Options are now locked - any attempt to modify them will be ignoredTyKonKet.BarcodeGeneratorpublic sealed class BarcodeOptionsThe default typeface used for rendering text in barcodes when no custom typeface is specified.
public static readonly SKTypeface DefaultTypefaceExample Usage:
// Check if using default typeface
if (options.Typeface == BarcodeOptions.DefaultTypeface)
{
Console.WriteLine("Using default system font");
}Gets or sets the type of barcode to generate.
public BarcodeTypes Type { get; set; }| Attribute | Value |
|---|---|
| Default Value | BarcodeTypes.Ean8 |
| Available Types | EAN-13, UPC-A, ISBN-13, EAN-8, CODE-93 |
Example:
var options = new BarcodeOptions
{
Type = BarcodeTypes.Ean13
};Gets or sets the height of the barcode bars in pixels.
public int Height { get; set; }| Attribute | Value |
|---|---|
| Default Value | 30 pixels |
| Recommended Range | 20-100 pixels |
| Use Case | Larger values improve scanning reliability |
Example:
options.Height = 50; // Taller bars for better scanningGets or sets the scaling factor for the barcode. Minimum value is 1.
public int Scaling { get; set; }| Attribute | Value |
|---|---|
| Default Value | 5 |
| Minimum Value | 1 |
| Behavior | Values < 1 adjusted to 1, negative values converted to absolute |
Example:
options.Scaling = 3; // 3x scale for larger outputGets or sets the margins around the barcode in pixels.
public int Margins { get; set; }| Attribute | Value |
|---|---|
| Default Value | 2 pixels |
| Purpose | Provides white space around barcode for better scanning |
| Recommended | 5-20 pixels for most applications |
Example:
options.Margins = 10; // More white space around barcodeGets or sets the background color of the barcode.
public SKColor BackgroundColor { get; set; }| Attribute | Value |
|---|---|
| Default Value | SKColors.White |
| Common Options | White, Transparent, Light Gray |
| Best Practice | High contrast with foreground color |
Examples:
options.BackgroundColor = SKColors.Transparent; // Transparent background
options.BackgroundColor = SKColor.Parse("#F0F0F0"); // Light grayGets or sets the color of the barcode bars and text.
public SKColor ForegroundColor { get; set; }| Attribute | Value |
|---|---|
| Default Value | SKColors.Black |
| Best Practice | Dark colors for better contrast |
| Scanner Compatibility | Black provides best scanning results |
Examples:
options.ForegroundColor = SKColors.DarkBlue;
options.ForegroundColor = SKColor.Parse("#FF0000"); // Red barsGets or sets a value indicating whether to render text below the barcode.
public bool RenderText { get; set; }| Attribute | Value |
|---|---|
| Default Value | true |
| When to Disable | Space-constrained layouts, purely machine-readable codes |
| When to Enable | Human verification needed, retail applications |
Example:
options.RenderText = false; // Bars only, no textGets the typeface used for rendering text in the barcode. This is read-only; use the UseTypeface methods to set it.
public SKTypeface Typeface { get; }| Attribute | Value |
|---|---|
| Access | Read-only property |
| Modification | Use UseTypeface() methods to change |
| Default | System default font |
Example:
Console.WriteLine($"Current font: {options.Typeface.FamilyName}");public SKTypeface Typeface { get; }Console.WriteLine($"Current font: {options.Typeface.FamilyName}");Sets the typeface used for rendering text in the barcode.
public void UseTypeface(SKTypeface typeface)typeface(SKTypeface) - The typeface to use.
InvalidOperationException- Thrown when the options are locked and cannot be modified.
var customTypeface = SKTypeface.FromFamilyName("Arial");
options.UseTypeface(customTypeface);Sets the typeface used for rendering text using a FontFamily.
public void UseTypeface(FontFamily fontFamily)fontFamily(FontFamily) - The font family to use. Can be a string or FontFamilies enum value.
Using string:
options.UseTypeface("Times New Roman");Using FontFamilies enum:
options.UseTypeface(FontFamilies.Arial);Implicit conversion from string:
FontFamily family = "Verdana"; // Implicit conversion
options.UseTypeface(family);Sets the typeface with a specific font style.
public void UseTypeface(FontFamily fontFamily, SKFontStyle fontStyle)fontFamily(FontFamily) - The font family to use.fontStyle(SKFontStyle) - The font style to apply.
options.UseTypeface("Arial", SKFontStyle.Bold);
options.UseTypeface(FontFamilies.TimesNewRoman, SKFontStyle.Italic);Sets the typeface with custom weight, width, and slant.
public void UseTypeface(FontFamily fontFamily, int weight, int width, SKFontStyleSlant fontStyleSlant)fontFamily(FontFamily) - The font family to use.weight(int) - The font weight (100-900, where 400 is normal, 700 is bold).width(int) - The font width (1-9, where 5 is normal).fontStyleSlant(SKFontStyleSlant) - The font slant (upright, italic, or oblique).
options.UseTypeface("Roboto", 600, 5, SKFontStyleSlant.Upright); // Semi-bold
options.UseTypeface("Open Sans", 300, 3, SKFontStyleSlant.Italic); // Light, condensed, italicSets the typeface using strongly-typed weight and width enums.
public void UseTypeface(FontFamily fontFamily, SKFontStyleWeight weight, SKFontStyleWidth width, SKFontStyleSlant fontStyleSlant)fontFamily(FontFamily) - The font family to use.weight(SKFontStyleWeight) - The font weight enum value.width(SKFontStyleWidth) - The font width enum value.fontStyleSlant(SKFontStyleSlant) - The font slant.
options.UseTypeface("Helvetica",
SKFontStyleWeight.SemiBold,
SKFontStyleWidth.Condensed,
SKFontStyleSlant.Upright);Sets the typeface from a font file.
public void UseTypefaceFromFile(string path, int index = 0)path(string) - The path to the font file (TTF, OTF, etc.).index(int) - The index of the font in the file (for font collections). Default is 0.
options.UseTypefaceFromFile("fonts/custom-font.ttf");
options.UseTypefaceFromFile("fonts/font-collection.ttc", 2); // Third font in collectionSets the typeface from font data in memory.
public void UseTypefaceFromData(SKData data, int index = 0)data(SKData) - The font data.index(int) - The index of the font in the data. Default is 0.
byte[] fontBytes = File.ReadAllBytes("custom-font.ttf");
using var fontData = SKData.CreateCopy(fontBytes);
options.UseTypefaceFromData(fontData);Sets the typeface from an SKStreamAsset.
public void UseTypefaceFromStream(SKStreamAsset stream, int index = 0)stream(SKStreamAsset) - The font stream.index(int) - The index of the font in the stream. Default is 0.
using var fontStream = new SKFileStream("fonts/custom.ttf");
options.UseTypefaceFromStream(fontStream);Sets the typeface from a .NET Stream.
public void UseTypefaceFromStream(Stream stream, int index = 0)stream(Stream) - The font stream.index(int) - The index of the font in the stream. Default is 0.
using var fileStream = File.OpenRead("fonts/custom.ttf");
options.UseTypefaceFromStream(fileStream);var options = new BarcodeOptions
{
Type = BarcodeTypes.Ean13,
Height = 50,
Scaling = 3,
RenderText = true
};options.BackgroundColor = SKColors.White;
options.ForegroundColor = SKColor.Parse("#2C3E50"); // Dark blue-gray
options.Margins = 15; // Generous margins for printing// Simple font family
options.UseTypeface("Arial");
// Font with style
options.UseTypeface("Times New Roman", SKFontStyle.Bold);
// Custom font file
options.UseTypefaceFromFile("assets/fonts/barcode-font.ttf");var printOptions = new BarcodeOptions
{
Type = BarcodeTypes.Ean13,
Height = 100, // Taller for better scanning
Scaling = 6, // Larger scale for high DPI
Margins = 20, // Adequate white space
BackgroundColor = SKColors.White,
ForegroundColor = SKColors.Black,
RenderText = true
};
printOptions.UseTypeface("Arial", SKFontStyle.Normal);var minimalOptions = new BarcodeOptions
{
Type = BarcodeTypes.Code93,
Height = 25, // Compact height
Scaling = 1, // Minimal scale
Margins = 0, // No margins
RenderText = false, // No text
BackgroundColor = SKColors.Transparent
};var webOptions = new BarcodeOptions
{
Type = BarcodeTypes.Ean13,
Height = 40,
Scaling = 2,
Margins = 5,
BackgroundColor = SKColors.Transparent,
ForegroundColor = SKColors.Black,
RenderText = true
};
webOptions.UseTypeface("Arial", SKFontStyle.Normal);using var barcode = new Barcode(options =>
{
options.Type = BarcodeTypes.Code93;
options.Height = 60;
options.Scaling = 4;
options.UseTypeface("Consolas", SKFontStyle.Bold);
});public static void ConfigureForPrint(BarcodeOptions options)
{
options.Height = 80;
options.Scaling = 5;
options.Margins = 20;
options.BackgroundColor = SKColors.White;
options.ForegroundColor = SKColors.Black;
options.UseTypeface("Arial", SKFontStyle.Normal);
}
// Use the configuration
using var barcode = new Barcode(ConfigureForPrint);public static bool ValidateOptions(BarcodeOptions options)
{
return options.Height > 0
&& options.Scaling > 0
&& options.Margins >= 0;
}- Barcode Class - Main barcode generation class
- BarcodeTypes Enum - Available barcode types
- Font System - Font family and typeface details
- Customization Examples - Advanced styling examples