C# Barcode Library > Barcode Generation Guide > How to generate barcodes in C# ASP.NET, WinForms apps?

C# Barcode Generator Tutorial


How to create, print QR Code and barcodes in C# ASP.NET, Windows application?
  • Easy & Simple to generate barcodes in C#.net applications
  • Enable C# programmers to control 1d and 2d barcodes generation parameters
  • Compiled in C#.NET, also supports implementation in VB.NET barcode generation programs
  • Professional C#.net barcode component, enjoying wide popularity
  • 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.

Download Now
NuGet Ready
.NET 5+
.NET Framework 4.x, 3.x, 2.x

C# Barcode Generator Supporting Symbologies

Linear/1D Barcode Images:
Matrix/2D Barcode Images:
C# Barcode Generates 1D (Linear) Barcode Images: Codabar, Code 11, Code 2 of 5, Code 39, Code 93, Code 128, EAN 8, EAN 13, GS1-128, Interleaved 2 of 5, ITF14 (UPC Shipping Container Symbol), MSI Plessey, OneCode (Intelligent Mail), PLANET, POSTNET, RM4SCC (British Royal Mail 4-State Customer Barcode), UPC-A, UPC-E,
C# Barcode Generates 2D (Matrix) Barcode Images: Data Matrix, PDF 417, QR-Code.


Download & Install C# Barcode Generator Library


From NuGet

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


Download & Install from Site

BarcodeLib.com C#.NET Barcode Generator library is free to download. You can click the following link to download the trial package:
Download C# barcode generator SDK free trial package


Install barcode library to .NET projects


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.
  1. 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.
  2. 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.
  3. Set barcode property Data to input barcode encoding data message
  4. 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:
  1. Create a new QRCode object in the C# class.
  2. Set QR Code data text to encoding.
  3. Generate QR Code image to the target file path.
BarcodeLib.Barcode.QRCode qrcode = new BarcodeLib.Barcode.QRCode();
qrcode.Data = "https://www.barcodelib.com";
qrcode.drawBarcode("C://temp//csharp-quick-start-qrcode.png");


Create Code 128 linear barcode using C#

Here is another C# sample code to quickly generate a Code 128 1d barcode using C# barcode generator library.
  1. Create a new Linear barcode object in the C# class.
  2. Set barcode object Type property to Barcode.BarcodeType.CODE128 for Code 128 barcode symbology.
  3. Set barcode object Data property to the Code 128 barcode text.
  4. Generate barcode image to the specified image file path.
BarcodeLib.Barcode.Linear code128 = new BarcodeLib.Barcode.Linear();
code128.Type = Barcode.BarcodeType.CODE128;
code128.Data = "CODE 128";
code128.drawBarcode("C://temp//csharp-quick-start-code128.png");



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.
  1. Enable a special data encoding format by setting property ProcessTilde to true.
  2. 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.
BarcodeLib.Barcode.Linear code128 = new BarcodeLib.Barcode.Linear();
code128.Type = Barcode.BarcodeType.CODE128;
code128.ProcessTilde = true;
code128.Data = "aaa~013bbb";
code128.drawBarcode("C://temp//csharp-code128-non-printalbe-char.png");


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.

QRCode barcode = new QRCode();
barcode.IsUnicodeData = true;



You can learn more details about Unicode text encoding in barcode here:
How to create, print barcode with Unicode text encoded 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".

Linear barcode = new Linear();
barcode.Type = BarcodeType.EAN128;
barcode.Data = "(00)395012345678912345";



You can view the details here:
How to generate barcode with GS1 data message using C# Barcode library


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.
QRCode barcode = new QRCode();

String message = "https://www.barcodelib.com";
byte[] bytes = Encoding.UTF8.GetBytes(message);
StringBuilder sb = new StringBuilder();
foreach (byte b in bytes)
    sb.Append("~" + b.ToString().PadLeft(3, '0'));

barcode.Encoding = QRCodeEncoding.Byte;
barcode.ProcessTilde = true;
barcode.Data = sb.ToString();

barcode.drawBarcode("C://BarcodeLib//csharp-barcode-qrcode-binary-data.png");




Use BarcodeUtils.ByteArrayToString() method to help format the binary data

BarcodeLib C# Barcode library provides a help method to format the binary data with ~ char appended automatically.
  • Use method BarcodeUtils.ByteArrayToString() to convert byte array data to formatted string

byte[] data = ...;
string formattedString = BarcodeUtils.ByteArrayToString(data);


How to create barcode with specified size in C#?

Using C# barcode generator library, you can easily customize the barcode with your specified width and height in C# class.
  • UOM: Unit of measure. You can choose one of the following values: "PIXEL", "INCH", "CM". The default value is Pixel.
  • ImageWidth: set the generated barcode image width
  • ImageHeight: set the barcode image height
Using the following C# source code, you will get a QR Code with same data encoded in 200 pixels width and 200 pixels height.

QRCode qrcode = new QRCode();
qrcode.Data = "https://www.barcodelib.com";
qrcode.UOM = UnitOfMeasure.PIXEL;
qrcode.ImageWidth = 200;
qrcode.ImageHeight = 200;
qrcode.drawBarcode("C://output//csharp-create-barcode-size-qrcode-simple-setting.png");




C# barcode library provides list of properties to fully customize the barcode dimension width and height. You can view more details here: How to create and customize barcode dimension size using C#?




Create barcode with customized options using C#



Customize linear barcode text label

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.
Linear barcode = new Linear();
barcode.Type = BarcodeType.EAN128;
barcode.Data = "(17)050101";
barcode.TextFont = new Font("OCR-B", 22);
barcode.TextMargin = 10;
barcode.drawBarcode("C://BarcodeLib//csharp-barcode-text-style.png");




Hide linear barcode text label

You can hide the 1d barcode text
  • 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.
Linear barcode = new Linear();
barcode.Type = BarcodeType.EAN13_5;
barcode.Data = "123456789012";
barcode.SData = "12345";
barcode.BarColor = Color.Red;
barcode.BackgroundColor = Color.LightBlue;
barcode.TextFontColor = Color.Red;
barcode.drawBarcode("C://BarcodeLib//csharp-barcode-ean13-colors.png");




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.

barcode.BarColor = Color.Red;
barcode.BackgroundColor = Color.Transparent;
barcode.TextFontColor = Color.Red;




Rotate barcode

Use the Rotate property to rotate the barcode to different orientations.

Linear barcode = new Linear();
barcode.Type = BarcodeType.CODE39;
barcode.Data = "CODE-39";
barcode.Rotate = RotateOrientation.BottomFacingDown;
barcode.drawBarcode("C://BarcodeLib//csharp-barcode-rotate-face-down.png");

barcode.Rotate = RotateOrientation.BottomFacingRight;
barcode.drawBarcode("C://BarcodeLib//csharp-barcode-rotate-face-right.png");

barcode.Rotate = RotateOrientation.BottomFacingLeft;
barcode.drawBarcode("C://BarcodeLib//csharp-barcode-rotate-face-left.png");


Rotate face to right
Rotate face to left


High resolution barcode for printer

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.

barcode.Resolution = 900;
barcode.ImageFormat = BLImageFormat.PNG;



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
  • PNG (default)
  • JPG
  • BMP
  • GIF
  • TIF
  • EPS
  • SVG
Linear barcode = new Linear();
barcode.Type = BarcodeType.CODE128;
barcode.Data = "Code 128";
barcode.ImageFormat = BLImageFormat.PNG;
barcode.drawBarcode("C://BarcodeLib//csharp-barcode-image-format.png");

barcode.ImageFormat = BLImageFormat.SVG;
barcode.drawBarcode("C://BarcodeLib//csharp-barcode-image-format.svg");

barcode.ImageFormat = BLImageFormat.EPS;
barcode.drawBarcode("C://BarcodeLib//csharp-barcode-image-format.eps");


Print barcode to EPS format with CMYK color

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.


Linear barcode = new Linear();
barcode.Type = BarcodeType.CODE128;
barcode.Data = "Code 128";
barcode.ImageFormat = BLImageFormat.EPS;
barcode.ApplyCMYK = true;
barcode.drawBarcode("C://BarcodeLib//csharp-barcode-eps-cmyk-color-applied.eps");




Generating Barcode in C# ASP.NET Web Applications

There are two methods for how to create barcode images in your ASP.NET web applications using C#.net class.
  1. 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.
  2. The second method is to generate barcode images through ASP.NET Web Form controller.
    1. 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.
    2. 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.
    3. 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.
    4. You can drag LinearASPNET on your aspx page, and change barcode setting through properties window.
    5. Run the project. You will find barcode images generated in your aspx pages.

Generating Barcodes in C# Windows Applications

  1. 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.
  2. 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.
  3. Now you can see the component displayed on Toolbox. Then, you can drag LinearWinForm on your form and change barcode setting through properties window.




Other Related .NET Barcoding Articles