Compatible with latest barcode symbology standards
Create linear barcode images, including Code39, Code128, GS1-128, Interleaved 2 of 5, EAN / UPC
Create 2d barcode images, including Data Matrix, PDF-417, & QR-Code for C#
Offer cost-effective developer licenses for .NET Barcode Library SDK
This article introduces tutorial on how to use Barcode for C#.NET components to generate barcodes in your Visual C# ASP.NET website, C# classes, and C# Windows applications.
You may directly download free C#.NET Barcode Library Control trial package and test it in your C# barcode generation for Visual Studio .NET applications.
C# Barcode Generator Library
QR Code, Data Matrix, Code 128, GS1, EAN/UPC. Unicode text encoding supported.
We have published the following nuget packages for Barcode for .NET Suite C# library. You can search it on the NuGet Package Manager from Visual Studio and install it.
BarcodeLib.Barcode.Common : The barcode generator library is built by .NET Standard 2.0. It works for .NET 8, .NET 7, 6, 5, .NET Core 3.1, 2.1, and .NET Framework 4.8.
Visit it from nuget.org
BarcodeLib.Barcode.AspNetCore : It includes APIs for barcode generation in ASP.NET Core web app. It depends on the package BarcodeLib.Barcode.Common.
Visit it from nuget.org
BarcodeLib.Barcode.SkiaSharp : This library is a modified version of BarcodeLib.Barcode.Common. It is using SkiaSharp package instead of System.Drawing.Common.
Visit it from nuget.org
BarcodeLib.Barcode.AspNetCore.SkiaSharp : This library is a modified version of BarcodeLib.Barcode.AspNetCore. It is using SkiaSharp package instead of System.Drawing.Common
Visit it from nuget.org
BarcodeLib.Barcode.Generator.ASP.NET.Framework.Web.Control : The nuget package is for .NET Framework 4.x, 3.x and 2.x. It supports barcode generation
in C# class. It includes barcode generator web control for ASP.NET web application.
Visit it from nuget.org
BarcodeLib.Barcode.Generator.WinForms.NET.Framework : The nuget package is for .NET Framework 4.x, 3.x and 2.x. It supports barcode generation
in C# class in Windows Forms, WPF, console applications. It includes barcode generator Windows control for .NET WinForms application.
Visit it from nuget.org
Install on .NET Core apps on Windows Operating System
For ASP.NET Core web app, add BarcodeLib.Barcode.Common.dll and BarcodeLib.Barcode.AspNetCore.dll
from downloaded package /Dll/net-standard/ to the .NET project reference.
For Windows Forms or WPF Desktop app, add BarcodeLib.Barcode.Common.dll to the .NET project reference.
For other .NET Core app, add BarcodeLib.Barcode.Common.dll to the .NET project reference.
Search and install NuGet package System.Drawing.Common from NuGet Package Manager.
Install on .NET Core apps on Linux, MacOS... (non-Windows Operating System)
For ASP.NET Core web app, add BarcodeLib.Barcode.SkiaSharp.dll and BarcodeLib.Barcode.AspNetCore.SkiaSharp.dll
from downloaded package /Dll/net-standard/ to the .NET project reference.
For Windows Forms or WPF Desktop app, add BarcodeLib.Barcode.SkiaSharp.dll to the .NET project reference.
For other .NET Core app, add BarcodeLib.Barcode.SkiaSharp.dll to the .NET project reference.
Search and install NuGet package SkiaSharp from NuGet Package Manager.
Install on .NET Framework applications
For .NET Framework 4.x application, navigate to /Dll/net40/ from the downloaded package.
For .NET Framework 3.x or 2.x application, navigate to /Dll/net20/ from the downloaded package.
For ASP.NET web application, add BarcodeLib.Barcode.ASP.NET.dll to the .NET project reference.
For Windows Forms, WPF Desktop and other applications, add BarcodeLib.Barcode.WinForms.dll to the .NET project reference.
For Crystal Reports for .NET application, add BarcodeLib.Barcode.CrystalReports.dll to the .NET project reference.
For RDL local reports application, add BarcodeLib.Barcode.RDLCReports.dll to the .NET project reference.
Create barcodes in C# class
It's really easy to create barcodes in C# class.
Create a new Linear object to create 1d barcodes, such as Code 128, Code 39, GS1-128, EAN/UPC,
or a new QRCode, DataMatrix, PDF417 object to generate QR Code, Data Matrix, or PDF-417 2d barcodes.
If it is a Linear object, set barcode property Type to the barcode format you need create, such as Barcode.BarcodeType.CODE128 for Code 128.
Set barcode property Data to input barcode encoding data message
Call method drawBarcode() to create and encode barcode image to an image file path.
Generate QR Code 2d barcode in C# class
The C# source code below shows how to quickly create a QR Code image through 3 steps:
Advanced barcode data encoding using C# Barcode Generator library
You can easily encode barcode with some printable text, such as digits (0, 1, ..., 9), upper case letters (A - Z), lower case letters (a - z), and '-', '$', '%', ' ', '.', '/', '+' these common printable characters.
However you can not enter invisible characters (such as char carriage return 'CR'), which are also supported by some barcode formats, such as Code 128, QR Code, Data Matrix, and PDF-417.
The following content we will learn how to create barcode with special data encoded in C# class using C# barcode library.
How to generate barcode with control (non-printable) characters using C#?
It is straightforward to encode non-printable characters in the barcode using C# barcode library.
Enable a special data encoding format by setting property ProcessTilde to true.
In barcode data property, use the following format to enter non-printable character: '~' + the character ASCII value in 3 digits. For example, character 'Carrriage Return' ASCII value is 13.
To encode it, you can use the following code: barcode.Data = '~013'.
The C# sample code below shows how to encode the char 'Carrriage Return' in the barcode Code 128 using C# barcode generator library.
Note: the non-printable characters will not be displayed in the barcode text label (Human Readable Interpretation).
How to generate barcode with Unicode text using C#?
Most of the 2d barcode types (such as QR Code, Data Matrix) supports Unicode text encoding. BarcodeLib C# barcode library is easy to encode Unicode text into barcodes in your C# applications.
Set property IsUnicodeData to true in 2d barcodes (QR Code, Data Matrix, PDF417), and you can generate 2d barcode with Unicode text encoding in C# class.
How to create GS1 barcode with GS1 data message using C#?
The GS1 System has defined the special data format to encode GS1 data message in GS1 supported data carrier, such as GS1-128, EAN/UPC, QR Code, Data Matrix, ITF-14, GS1 DataBar.
To encode GS1 data message correctly into supported barcodes, the GS1 system defined special function character (such as FNC1, ) and its rules to insert function chars into GS1 data messages.
C# Barcode Generator library provides a simple GS1 data encoding method, and you do not need know how to or where to encode 'FNC1' in the GS1 data message.
A simple GS1-128 barcode with GS1 Application Identifier "00", and its data string "395012345678912345".
How to create 2d barcode with binary data encoded using C#?
Most 2D barcode symbologies, including QR Code, Data Matrix, and PDF-417, support byte array data encoding.
With a C# barcode library, you can easily create barcodes encoded with byte array data in your C# application. Follow the structured steps below to complete the encoding process.
To encode byte array data into a 2D barcode (Such as QR Code) using a C# barcode library, you need to go through the following key steps:
Convert each byte in the byte array to the '~ddd' format (e.g., a byte with the value '80' is converted to '~080').
Assign the combined converted string (from all bytes) to the Data property.
Set the QR Code barcode data encoding mode to byte (QRCodeEncoding.Byte).
Enable the ProcessTilde property to true, allowing the C# barcode library to process byte array data.
Print the barcode with the encoded byte array data.
With a C# barcode generator, you can effortlessly customize the styles of 1D barcode text (also known as Human-Readable Interpretation, HRI).
To achieve your desired text styles, you only need to configure the corresponding properties of the barcode generator.
Text Font Style
Use the TextFont property to customize the font settings of the barcode text.
Note: Ensure the specified font family is installed on the system where the barcode will be generated; otherwise, the text may display in a default font (e.g., Arial) instead of your desired one.
Text Spacing from Bar Modules Use the TextMargin property to control the spacing between the barcode modules and the printed text label.
Note: Setting a too-small value may cause text-barcode overlap, while a too-large value may waste space and affect the overall barcode layout.
Show or Hide the Text
The ShowText property is used to control whether the 1D barcode text (HRI) is displayed or hidden.
Note: Double-check the ShowText value before generating the barcode, as hiding the text by mistake may affect the readability of the barcode for human operators.
Set property ShowText to false to hide the barcode text
barcode.ShowText=false;
Barcode bar, space, text colors
You can customize the bar, space module and text colors of your barcode by following barcode properties.
Bar Module Color
Use the BarColor property to customize the barcode bar modules' color in RGB
Space Module Color
Use the BackgroundColor property to customize the barcode space modules' color in RGB
Note: Ensure there is sufficient contrast between BarColor and BackgroundColor (e.g., red and white). Low contrast will reduce the barcode's scannability.
Text Color
The TextColor property allows you to define the color of the printed 1D barcode text.
Note: Avoid using light colors (e.g., light gray, yellow) on a light background, as this will reduce text readability.
Print barcode with transparent background-color in C#
If you want to generate a barcode image with a transparent background in a C# class, you can follow the C# codes below.
Background Color
Set the value of the BackgroundColor property to Color.Transparent.
Note: Do not use any other color value (e.g., Color.Empty or custom color codes) for BackgroundColor; otherwise, the background will not be transparent.
Bar Module Color
Note: Ensure the selected BarColor has sufficient contrast with the background where the barcode will be placed, as a transparent background means the barcode will inherit the background of its placement environment.
To generate a barcode image suitable for printing, ensure its resolution is higher than your printer's resolution.To create such a high-resolution barcode image, you'll need to configure the following two key barcode properties:
Resolution
Use Resolution property to set the barcode image's resolution to a value that exceeds your printer's resolution. This ensures the barcode remains clear and scannable after printing.
Output File Format
Use ImageFormat to choose a raster image format for the output file.
Recommended formats include PNG, JPG, or BMP.
Note: Avoid choosing the GIF format.
GIF does not support resolution adjustments, which will prevent you from setting the required high resolution for printing.
Output barcode to raster, vector image formats
With the C# Barcode Generator library, you can efficiently create barcodes in standard raster and vector image formats. The barcode library support the following raster and vector image formats in enum BLImageFormat
When generating barcodes in EPS format files, you can select and configure either RGB or CMYK color modes for drawing and printing your barcode elements.
Choose the barcode output image format as EPS. By default, the library will render the EPS use RGB colors.
Set the value of the UseCMYKInEPS property to true.
Once enabled, the C# barcode library will automatically convert the barcode's colors from the RGB color space to the CMYK color space.
Note: CMYK color mode is recommended for professional printing purposes, while RGB is optimized for digital display.
There are two methods for how to create barcode images in your ASP.NET web applications using C#.net class.
The Simplest way is to stream barcode image using our Buildin ASP.NET Barcode Application.
Under downloaded trial package, copy barcode folder to your IIS folder, e.g. C:\Inetpub.
Create a new virtual directory in IIS, named barcode, and link to the above "barcode" folder.
Restart IIS.
To test your installation, open your web browser and navigate to: http://YourDomain:port/barcode/linear.aspx?Type=7&Data=CODE39-123456789012
To create barcode image in your aspx or html page, you need pass the url to IMG tag src value. For linear barcode <img src="http://YourDomain:port/barcode/linear.aspx?Type=7&Data=CODE39-123456789012" /> or for Data Matrix <img src="http://YourDomain:port/barcode/datamatrix.aspx?Data=BarcodeLib12345678&ModuleSize=4&LeftMargin=8&RightMargin=8&TopMargin=8&BottomMargin=8" /> or for PDF417 <img src="http://YourDomain:port/barcode/pdf417.aspx?Data=123456789&ModuleSize=3&LeftMargin=6&RightMargin=6&TopMargin=6&BottomMargin=6" /> or for QRCode <img src="http://YourDomain:port/barcode/qrcode.aspx?Data=0123456789&ModuleSize=3&LeftMargin=12&RightMargin=12&TopMargin=12&BottomMargin=12" /> Using this method, it will not generate any barcode images in your IIS server side.
The second method is to generate barcode images through ASP.NET Web Form controller.
Install ASP.NET Barcode Controller to your ASP.NET project.
Add Reference BarcodeLib.Barcode.ASP.NET.dll to your project. There's no need to copy barcoding dll to your project bin folder because Visual Studio will do it for you during project compilation time.
Add barcode library dll to your Visual Studio Toolbox.
Open Toolbox in Visual Studio. Click menu View, and check submenu Toolbox.
Right click Toolbox, and click menu Choose Items...
Goto .NET Framework Components tab.
If no BarcodeLib component found, click Browse... button and select BarcodeLib.Barcode.ASP.NET.dll file.
Then sort "Namespace" column. You will find 4 components from BarcodeLib.Barcode.
Check component LinearASPNET, and its namespace is BarcodeLib.Barcode.
Check component DataMatrixASPNET, and its namespace is BarcodeLib.Barcode.
Check component PDF417ASPNET, and its namespace is BarcodeLib.Barcode.
Check component QRCodeASPNET, and its namespace is BarcodeLib.Barcode.
Click "OK" button, and you will find four components under "General": LinearASPNET, DataMatrixASPNET, PDF417ASPNET, QRCodeASPNET.
Go to "barcode" folder in the trial package, and then copy files "linear.aspx", "linear.aspx.cs", "datamatrix.aspx", "datamatrix.aspx.cs", "pdf417.aspx", "pdf417.aspx.cs", "qrcode.aspx", "qrcode.aspx.cs" to the same folder as your aspx page, which will generate barcodes.
You can drag LinearASPNET on your aspx page, and change barcode setting through properties window.
Run the project. You will find barcode images generated in your aspx pages.
Generating Barcodes in C# Windows Applications
Add Reference BarcodeLib.Barcode.WinForms.dll to your project. There's no need to copy barcoding dll to your project bin folder because Visual Studio will do it for you during project compilation time.
In your .NET windows project, right click mouse over Refereces in your Solution Explorer window. Then click menu "Add Reference ...".
Add BarcodeLib.Barcode.WinForms.dll to your project.
Add barcode library dll to your Visual Studio Toolbox.
Open Toolbox in Visual Studio. Click menu View, and check submenu Toolbox.
Right click Toolbox, and click menu Choose Items...
Goto .NET Framework Components tab.
If no BarcodeLib component found, click Browse... button and select BarcodeLib.Barcode.WinForms.dll file.
Then sort "Namespace" column, and you will find 4 components from BarcodeLib.Barcode.
Check component LinearWinForm, and its namespace is BarcodeLib.Barcode.
Check component DataMatrixWinForm, and its namespace is BarcodeLib.Barcode.
Check component PDF417WinForm, and its namespace is BarcodeLib.Barcode.
Check component QRCodeWinForm, and its namespace is BarcodeLib.Barcode.
Click "OK" button and you will find four components under "Common Controls": LinearWinForm, DataMatrixWinForm, PDF417WinForm, QRCodeWinForm.
Now you can see the component displayed on Toolbox. Then, you can drag LinearWinForm on your form and change barcode setting through properties window.