DwsBarcodeGenerator
The DwsBarcodeGenerator component renders barcodes and QR codes from a given value string. Supports multiple encode types via the DwsBarcodeEncodeType enum.
API ReferenceHex Colors
Demonstrates barcode color customization using raw hex values. ForegroundHex and BackgroundHex take precedence over the DwsColor enums.
Show Code
BarcodeGenerator.razor
<DwsVertical Name="BarcodeGeneratorDemo_Hex_Layout" Class="gap-3" Style="max-width: 400px;">
<DwsLabel Name="BarcodeGeneratorDemo_Hex_Label_Value" Text="Value" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Hex_Input_Value" T="string" @bind-Value="barcodeValueHex" Label="Barcode Value" />
<DwsLabel Name="BarcodeGeneratorDemo_Hex_Label_Type" Text="Barcode Type" Class="fw-semibold" />
<DwsNativeSelect Name="BarcodeGeneratorDemo_Hex_Select_Type" T="DwsBarcodeEncodeType" @bind-Value="barcodeTypeHex" Label="Barcode Type">
@foreach (var type in _encodableTypes)
{
<DwsSelectOption Name="@($"BarcodeGeneratorDemo_Hex_Option_Type_{type}")" Value="@type">@type</DwsSelectOption>
}
</DwsNativeSelect>
<DwsLabel Name="BarcodeGeneratorDemo_Hex_Label_FG" Text="Foreground Hex" Class="fw-semibold" />
<DwsColorPicker Name="BarcodeGeneratorDemo_Hex_ColorPicker_FG" @bind-RawValue="foregroundHex" Label="Foreground Hex" />
<DwsLabel Name="BarcodeGeneratorDemo_Hex_Label_BG" Text="Background Hex" Class="fw-semibold" />
<DwsColorPicker Name="BarcodeGeneratorDemo_Hex_ColorPicker_BG" @bind-RawValue="backgroundHex" Label="Background Hex" /></DwsVertical>
<DwsErrorBoundary Name="BarcodeGeneratorDemo_Hex_ErrorBoundary" OnError="HandleError">
<ChildContent>
<DwsBarcodeGenerator Name="BarcodeGeneratorDemo_Hex" Value="@barcodeValueHex" MaxHeight="300" BarcodeType="@barcodeTypeHex" ForegroundHex="@foregroundHex" BackgroundHex="@backgroundHex" />
</ChildContent>
</DwsErrorBoundary>BarcodeGenerator.razor.cs
// The formats ZXing can write; MaxiCode, RSS-14, RSS Expanded and the UPC/EAN extension can only be read.
private static readonly DwsBarcodeEncodeType[] _encodableTypes =
[.. Enum.GetValues<DwsBarcodeEncodeType>().Where(t => t is not (DwsBarcodeEncodeType.MaxiCode or DwsBarcodeEncodeType.RSS14 or DwsBarcodeEncodeType.RSSExpanded or DwsBarcodeEncodeType.UPC_EAN_Extension))];
private string barcodeValueHex = "Hex Color Example";
private DwsBarcodeEncodeType barcodeTypeHex = DwsBarcodeEncodeType.QRCode;
private string foregroundHex = "#FF0000";
private string backgroundHex = "#FFFF00";
private Task HandleError(Exception exception)
{
barcodeValue = string.Empty;
return Task.CompletedTask;
}Size & Aspect Ratio
Demonstrates how to control barcode width, height, and aspect ratio using Width, Height, MinWidth, MinHeight, MaxWidth, and MaxHeight parameters.
Show Code
BarcodeGenerator.razor
<DwsVertical Name="BarcodeGeneratorDemo_Size_Layout" Class="gap-3" Style="max-width: 400px;">
<DwsLabel Name="BarcodeGeneratorDemo_Size_Label_Value" Text="Value" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Size_Input_Value" T="string" @bind-Value="barcodeValueSize" Label="Barcode Value" />
<DwsLabel Name="BarcodeGeneratorDemo_Size_Label_Type" Text="Barcode Type" Class="fw-semibold" />
<DwsNativeSelect Name="BarcodeGeneratorDemo_Size_Select_Type" T="DwsBarcodeEncodeType" @bind-Value="barcodeTypeSize" Label="Barcode Type">
@foreach (var type in _encodableTypes)
{
<DwsSelectOption Name="@($"BarcodeGeneratorDemo_Size_Option_Type_{type}")" Value="@type">@type</DwsSelectOption>
}
</DwsNativeSelect>
<DwsLabel Name="BarcodeGeneratorDemo_Size_Label_Width" Text="Width (px)" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Size_Input_Width" T="int?" @bind-Value="barcodeWidth" Label="Width" />
<DwsLabel Name="BarcodeGeneratorDemo_Size_Label_Height" Text="Height (px)" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Size_Input_Height" T="int?" @bind-Value="barcodeHeight" Label="Height" />
<DwsLabel Name="BarcodeGeneratorDemo_Size_Label_MinWidth" Text="Min Width (px)" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Size_Input_MinWidth" T="int?" @bind-Value="barcodeMinWidth" Label="Min Width" />
<DwsLabel Name="BarcodeGeneratorDemo_Size_Label_MinHeight" Text="Min Height (px)" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Size_Input_MinHeight" T="int?" @bind-Value="barcodeMinHeight" Label="Min Height" />
<DwsLabel Name="BarcodeGeneratorDemo_Size_Label_MaxWidth" Text="Max Width (px)" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Size_Input_MaxWidth" T="int?" @bind-Value="barcodeMaxWidth" Label="Max Width" />
<DwsLabel Name="BarcodeGeneratorDemo_Size_Label_MaxHeight" Text="Max Height (px)" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Size_Input_MaxHeight" T="int?" @bind-Value="barcodeMaxHeight" Label="Max Height" /></DwsVertical>
<DwsErrorBoundary Name="BarcodeGeneratorDemo_Size_ErrorBoundary" OnError="HandleError">
<ChildContent>
<DwsBarcodeGenerator Name="BarcodeGeneratorDemo_Size" Value="@barcodeValueSize" BarcodeType="@barcodeTypeSize" Width="@barcodeWidth" Height="@barcodeHeight" MinWidth="@barcodeMinWidth" MinHeight="@barcodeMinHeight" MaxWidth="@barcodeMaxWidth" MaxHeight="@barcodeMaxHeight" />
</ChildContent>
</DwsErrorBoundary>BarcodeGenerator.razor.cs
// The formats ZXing can write; MaxiCode, RSS-14, RSS Expanded and the UPC/EAN extension can only be read.
private static readonly DwsBarcodeEncodeType[] _encodableTypes =
[.. Enum.GetValues<DwsBarcodeEncodeType>().Where(t => t is not (DwsBarcodeEncodeType.MaxiCode or DwsBarcodeEncodeType.RSS14 or DwsBarcodeEncodeType.RSSExpanded or DwsBarcodeEncodeType.UPC_EAN_Extension))];
private string barcodeValueSize = "Size Example";
private DwsBarcodeEncodeType barcodeTypeSize = DwsBarcodeEncodeType.QRCode;
private int? barcodeWidth = 300;
private int? barcodeHeight = 150;
private int? barcodeMinWidth;
private int? barcodeMinHeight;
private int? barcodeMaxWidth;
private int? barcodeMaxHeight;
private Task HandleError(Exception exception)
{
barcodeValue = string.Empty;
return Task.CompletedTask;
}Margin / Quiet Zone
Demonstrates the Margin (quiet zone) parameter for controlling the whitespace around the barcode.
Show Code
BarcodeGenerator.razor
<DwsVertical Name="BarcodeGeneratorDemo_Margin_Layout" Class="gap-3" Style="max-width: 400px;">
<DwsLabel Name="BarcodeGeneratorDemo_Margin_Label_Value" Text="Value" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Margin_Input_Value" T="string" @bind-Value="barcodeValueMargin" Label="Barcode Value" />
<DwsLabel Name="BarcodeGeneratorDemo_Margin_Label_Type" Text="Barcode Type" Class="fw-semibold" />
<DwsNativeSelect Name="BarcodeGeneratorDemo_Margin_Select_Type" T="DwsBarcodeEncodeType" @bind-Value="barcodeTypeMargin" Label="Barcode Type">
@foreach (var type in _encodableTypes)
{
<DwsSelectOption Name="@($"BarcodeGeneratorDemo_Margin_Option_Type_{type}")" Value="@type">@type</DwsSelectOption>
}
</DwsNativeSelect>
<DwsLabel Name="BarcodeGeneratorDemo_Margin_Label_Margin" Text="Margin (px)" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Margin_Input_Margin" T="int" @bind-Value="barcodeMargin" Label="Margin" /></DwsVertical>
<DwsErrorBoundary Name="BarcodeGeneratorDemo_Margin_ErrorBoundary" OnError="HandleError">
<ChildContent>
<DwsBarcodeGenerator Name="BarcodeGeneratorDemo_Margin" Value="@barcodeValueMargin" MaxHeight="300" BarcodeType="@barcodeTypeMargin" Margin="@barcodeMargin" />
</ChildContent>
</DwsErrorBoundary>BarcodeGenerator.razor.cs
// The formats ZXing can write; MaxiCode, RSS-14, RSS Expanded and the UPC/EAN extension can only be read.
private static readonly DwsBarcodeEncodeType[] _encodableTypes =
[.. Enum.GetValues<DwsBarcodeEncodeType>().Where(t => t is not (DwsBarcodeEncodeType.MaxiCode or DwsBarcodeEncodeType.RSS14 or DwsBarcodeEncodeType.RSSExpanded or DwsBarcodeEncodeType.UPC_EAN_Extension))];
private string barcodeValueMargin = "Margin Demo";
private DwsBarcodeEncodeType barcodeTypeMargin = DwsBarcodeEncodeType.QRCode;
private int barcodeMargin = 10;
private Task HandleError(Exception exception)
{
barcodeValue = string.Empty;
return Task.CompletedTask;
}Border & BorderColor
Demonstrates border styling with adjustable border width and color; BorderColorHex wins over BorderColor.
Show Code
BarcodeGenerator.razor
<DwsVertical Name="BarcodeGeneratorDemo_Border_Layout" Class="gap-3" Style="max-width: 400px;">
<DwsLabel Name="BarcodeGeneratorDemo_Border_Label_Value" Text="Value" Class="fw-semibold" />
<DwsInput Name="BarcodeGeneratorDemo_Border_Input_Value" T="string" @bind-Value="barcodeValueBorder" Label="Barcode Value" />
<DwsLabel Name="BarcodeGeneratorDemo_Border_Label_Type" Text="Barcode Type" Class="fw-semibold" />
<DwsNativeSelect Name="BarcodeGeneratorDemo_Border_Select_Type" T="DwsBarcodeEncodeType" @bind-Value="barcodeTypeBorder" Label="Barcode Type">
@foreach (var type in _encodableTypes)
{
<DwsSelectOption Name="@($"BarcodeGeneratorDemo_Border_Option_Type_{type}")" Value="@type">@type</DwsSelectOption>
}
</DwsNativeSelect>
<DwsLabel Name="BarcodeGeneratorDemo_Border_Label_Width" Text="Border Width (pixels)" Class="fw-semibold" />
<DwsSlider Name="BarcodeGeneratorDemo_Border_Slider_Width" Min="0" Max="20" Step="1" @bind-Value="barcodeBorder">Border width</DwsSlider>
<div>Current: @barcodeBorder px</div>
<DwsLabel Name="BarcodeGeneratorDemo_Border_Label_Color" Text="Border Color" Class="fw-semibold" />
<DwsNativeSelect Name="BarcodeGeneratorDemo_Border_Select_Color" T="DwsColor" @bind-Value="barcodeBorderColor" Label="Border Color">
@foreach (var color in Enum.GetValues<DwsColor>())
{
<DwsSelectOption Name="@($"BarcodeGeneratorDemo_Border_Option_Color_{color}")" Value="@color">@color</DwsSelectOption>
}
</DwsNativeSelect>
<DwsLabel Name="BarcodeGeneratorDemo_Border_Label_HexColor" Text="Border Hex Color (optional)" Class="fw-semibold" />
<DwsColorPicker Name="BarcodeGeneratorDemo_Border_ColorPicker_Hex" @bind-RawValue="barcodeBorderColorHex" Label="Border Hex" />
</DwsVertical>
<DwsBarcodeGenerator Name="BarcodeGeneratorDemo_Border_Component" Value="@barcodeValueBorder" BarcodeType="@barcodeTypeBorder"
Border="@barcodeBorder" BorderColor="@barcodeBorderColor" BorderColorHex="@BorderColorHexOrNull" Class="mt-3" />BarcodeGenerator.razor.cs
// The formats ZXing can write; MaxiCode, RSS-14, RSS Expanded and the UPC/EAN extension can only be read.
private static readonly DwsBarcodeEncodeType[] _encodableTypes =
[.. Enum.GetValues<DwsBarcodeEncodeType>().Where(t => t is not (DwsBarcodeEncodeType.MaxiCode or DwsBarcodeEncodeType.RSS14 or DwsBarcodeEncodeType.RSSExpanded or DwsBarcodeEncodeType.UPC_EAN_Extension))];
private string barcodeValueBorder = "Border Demo";
private DwsBarcodeEncodeType barcodeTypeBorder = DwsBarcodeEncodeType.QRCode;
private int barcodeBorder = 2; // border width in pixels
private DwsColor barcodeBorderColor = DwsColor.Black;
private string barcodeBorderColorHex = string.Empty; // a hex colour here wins over BorderColor
private string? BorderColorHexOrNull => string.IsNullOrEmpty(barcodeBorderColorHex) ? null : barcodeBorderColorHex;Text Line
A linear code shows its value as a line of text underneath (ShowText: null = linear codes only, true / false to force it); a QR code does not, unless asked. The text line is part of the page, not of the image.
Code 128, default text line
QR code with ShowText = true
Code 128 with ShowText = false
Show Code
BarcodeGenerator.razor
@* Default: a linear code shows its text. *@
<DwsBarcodeGenerator Name="BarcodeGeneratorDemo_Text_Code128" BarcodeType="DwsBarcodeEncodeType.Code128" Value="DWS-2026-0042"
Width="280" Height="90" ShowDownloadButton="false" ShowCopyButton="false" />
@* A two-dimensional code shows no text unless asked. *@
<DwsBarcodeGenerator Name="BarcodeGeneratorDemo_Text_QR" BarcodeType="DwsBarcodeEncodeType.QRCode" Value="https://example.com/t/42"
Width="140" Height="140" ShowText="true" ShowDownloadButton="false" ShowCopyButton="false" />
@* The code only. *@
<DwsBarcodeGenerator Name="BarcodeGeneratorDemo_Text_Plain" BarcodeType="DwsBarcodeEncodeType.Code128" Value="DWS-2026-0042"
Width="280" Height="90" ShowText="false" ShowDownloadButton="false" ShowCopyButton="false" />BarcodeGenerator.razor.cs
// No additional code-behind is required for this example.Interface-Description
| Name | Typ | Parameters | Description |
|---|---|---|---|
| DownloadAsync | Task<bool> | Saves the generated image as a file (optional file name without extension). False when there is no image or the browser refused. | |
| CopyImageAsync | Task<bool> | Copies the generated image to the clipboard as PNG. False when there is no image or the browser refused (needs a secure context). |
| Name | Typ | Default | Description |
|---|---|---|---|
| ShowText | bool? | null | Shows the value as a line of text under the code. Null: under linear codes only (Code 128, EAN, UPC, ITF, ...), not under QR, Data Matrix, Aztec, PDF417, MaxiCode. Not part of the image. |
| ShowDownloadButton | bool | true | Shows a button under the code that saves the image. |
| ShowCopyButton | bool | true | Shows a button under the code that copies the image to the clipboard (as PNG). |
| DownloadFileName | string? | null | File name of a download without extension. Null: "barcode-" and the value reduced to letters, digits, - and _ (40 characters at most). |
| CopyFeedbackDuration | int | 2000 | How long the copy button shows its "copied" state, in ms. |
| Value | string | string.Empty | The text or data to encode as a barcode. |
| EmptyPlaceholder | bool | true | Shows an empty dashed box in the barcode's place while Value is empty (Width / Height where set, a default square otherwise), so the layout does not jump when a value arrives. |
| BarcodeType | DwsBarcodeEncodeType | DwsBarcodeEncodeType.QRCode | The barcode encoding format (QRCode, Code128, Code39, EAN13, EAN8, UPC_A, UPC_E, ITF, PDF417, Aztec, DataMatrix, Codabar, Plessey, MSI). |
| Height | int? | null | Fixed height of the rendered barcode in pixels. Overrides MinHeight/MaxHeight if set. |
| Width | int? | null | Fixed width of the rendered barcode in pixels. Overrides MinWidth/MaxWidth if set. |
| MinHeight | int? | null | Minimum height constraint in pixels. Used if Height is not set. |
| MinWidth | int? | null | Minimum width constraint in pixels. Used if Width is not set. |
| MaxHeight | int? | null | Maximum height constraint in pixels. Used if Height is not set. |
| MaxWidth | int? | null | Maximum width constraint in pixels. Used if Width is not set. |
| ForegroundColor | DwsColor | DwsColor.Black | Foreground color for the barcode (used if ForegroundHex is null or empty). |
| BackgroundColor | DwsColor | DwsColor.White | Background color for the barcode (used if BackgroundHex is null or empty). |
| ForegroundHex | string? | null | Foreground color as a hex string (e.g. #FF0000). If set and not empty, takes precedence over ForegroundColor. |
| BackgroundHex | string? | null | Background color as a hex string (e.g. #FFFFFF). If set and not empty, takes precedence over BackgroundColor. |
| BarcodeImageBytes | byte[]? | null | The generated barcode image as a byte array (SVG UTF-8 bytes or PNG bytes, depending on BarcodeImageFormat). Updated on each generation. |
| BarcodeImageFormat | BarcodeImageFormat | BarcodeImageFormat.SVG | Output image format for the barcode (SVG or PNG). SVG is vector and best for most use cases; PNG is raster. |
| Disabled | bool | false | Disables the component and suppresses barcode generation/output while it remains enabled. |
| IsHeadless | bool | false | If true, no barcode UI is rendered. The component can still generate programmatically via @ref methods. |
| Readonly | bool | false | When true after a barcode has been generated, parameter changes no longer regenerate the current barcode until Readonly is turned off. |
| CharacterSet | string | UTF-8 | Character set/encoding for barcode content (e.g. UTF-8, ISO-8859-1). |
| ErrorCorrectionLevel | QRCodeEncoderErrorCorrectionLevel | QRCodeEncoderErrorCorrectionLevel.L | Error correction level for QR codes (L, M, Q, H). Only applies to QRCode type. |
| Margin | int | 10 | Margin (quiet zone) around the barcode in pixels. |
| Border | int | 0 | Border width in pixels around the barcode container. Default is 0 (no border). |
| BorderColor | DwsColor | DwsColor.Black | Border color as a DwsColor enum (used if BorderColorHex is null or empty). |
| BorderColorHex | string? | null | Border color as a hex string (e.g. #000000). If set and not empty, takes precedence over BorderColor. |
| OnClickPropagation | bool | false | Lets a click on the barcode bubble up to the parent elements. |
| Name | Typ | Description |
|---|---|---|
| BarcodeImageGenerated | EventCallback<byte[]> | Fired when a barcode image is generated, with the byte array (SVG UTF-8 bytes or PNG bytes) as argument. |
| BarCodeGenerationFailed | EventCallback<Exception> | Fired when barcode generation fails (e.g. invalid value for the selected format). |
| Name | Typ | Default | Description |
|---|---|---|---|
| Element | ElementReference | The associated web component. May be null if accessed before the component is rendered. | |
| ComponentGuid | Guid | Unique identifier for the component. | |
| TypeString | string | The name of the component type, obtained from reflection. | |
| Authorized_RegisterEnabled | bool | false | Indicates whether this component is enabled through a permission registry. |
| Authorized_RegisterSelected | bool | false | Indicates whether this component is selected in permission selection mode. |
| GetInitializedState | bool | false | Returns whether the component is initialized. |
| GetRenderModeState | DwsRenderMode | Returns the current render mode. | |
| GetComponentState | DwsComponentStateType | Returns the current component state. | |
| GetIsInteractiveState | bool | false | Returns whether the component is interactive. |
| GetIsPrerenderingState | bool | false | Returns whether the component is prerendering. |
| GetComponentLoadingState | bool | false | Returns whether the component is loading. |
| IsFormBusy | bool | false | Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState). |
| Name | Typ | Parameters | Description |
|---|---|---|---|
| DebugInfo | void | Logs debug information about the component. | |
| UpdateState | void | Updates the component's state and triggers StateHasChanged. | |
| UpdateStateAsync | Task | Asynchronously updates the component's state and triggers StateHasChanged. | |
| Dispose | void | Disposes the component and releases resources. | |
| FocusAsync | ValueTask | bool containedInScrollableList | Sets focus to the component's element. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Name | string | Required unique identifier for the component within its container. | |
| Visible | bool | true | Determines if the component is visible in the UI. |
| Disabled | bool | false | Disables the form control, ensuring it doesn't participate in form submission. |
| ComponentNameSpace | string | The namespace for permission-based access control. | |
| PermissionKey | string? | null | A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins. |
| Class | string | Optional CSS class names. | |
| Style | string | Optional in-line styles. | |
| Tag | object | An optional user-defined object to store any additional data with the component. | |
| BackReference | DwsReference | A reference to the enclosing component. | |
| ID | string | The HTML ID attribute for the component. | |
| AdditionalAttributes | Dictionary<string, object> | Additional HTML attributes for the component's root element. | |
| IgnorePermissionChecks | bool | false | Bypasses all permission checks for this component. |
| IgnorePermissionSelection | bool | false | Ignore permission selection mode for this component. |
| IgnoreFormBusy | bool | false | Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it. |
| Name | Typ | Description |
|---|---|---|
| OnInitializedCallbackStarted | EventCallback | Callback when OnInitialized starts. |
| OnInitializedCallback | EventCallback | Callback during OnInitialized. |
| OnInitializedCallbackCompleted | EventCallback | Callback when OnInitialized completes. |
| OnInitializedAsyncCallbackStarted | EventCallback | Callback when OnInitializedAsync starts. |
| OnInitializedAsyncCallback | EventCallback | Callback during OnInitializedAsync. |
| OnInitializedAsyncCallbackCompleted | EventCallback | Callback when OnInitializedAsync completes. |
| OnIsComponentLoadingChanged | EventCallback<bool> | Callback when IsComponentLoading changes. |
| OnCurrentRenderModeChanged | EventCallback<DwsRenderMode> | Callback when CurrentRenderMode changes. |
| OnComponentStateChanged | EventCallback<DwsComponentStateType> | Callback when ComponentState changes. |
