How to generate Barcode in C# : Full tutorial
Tags: csharp, barcodegenerator, programming, barcode
Published: 2023-10-16 17:55:47

## How to Generate Barcode in C#

1. Download [Barcode Generator for C#](https://ironsoftware.com/csharp/barcode/tutorials/csharp-barcode-image-generator/) Project
2. Create a Visual Studio Project:
3. Install Barcode Library
4. Write a Code to Create a Barcode
5. Add Barcode Value and Text Annotation to the barcode.
6. Styling a Barcode

In this tutorial, we will learn how to generate Barcodes using C# Language in a dot Net Framework.
##C# Barcode Generator Library:

[IronBarcode](https://ironsoftware.com/csharp/barcode/) is a C# library for generating barcodes. It helps them easily create and work with barcodes in their applications. This library simplifies the process of making different types of barcodes, like Data Matrix, PDF417, and much more. It allows developers to customize them according to their needs, such as changing colors and sizes. IronBarcode also makes it easy to read barcodes in applications. Whether you're managing inventory or creating tickets, IronBarcode is a user-friendly solution for handling barcodes in C# projects.
##Step-By-Step Tutorial:
Let's begin the step-by-step tutorial to generate barcodes.
###Step # 1: Create a Visual Studio Project:
The first step is to create or open an existing Project. I will create a new console application project in Visual Studio 2022. You may choose any project template and Visual Studio version.

1. Open Visual Studio.
2. Choose a project template for your project type (I have selected Console Application).
3. Configure project settings, like project name and location.
4. Select the target framework (I have selected .NET 7, you can select any as per your preference).
Visual Studio will step up and create a new project as shown below.
![Visual Studio Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mr7jl536ixe0p4gz633z.png)
###Step # 2: Install Barcode Library:
Before you can get started, you'll need to install the Barcode Library in your project. You have three options to do this. You can install it from any of the options of your choice.

####Package Manager Console:
1.  Open the Package Manager Console.
2.  Type and execute the following command:
    `Install-Package Barcode`
3.  This command will download and install the Barcode package for you.

####Nuget Package Manager Solution:
1.  Navigate to "Tools" => "Nuget Package Manager" => "Manage Nuget Package Solution."
2. This will open the Nuget Package Manager.
3. Click on "Browse" and search for "BarCode."
![NuGet Package Manager For Solution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xvtgh4xinjnhgnoh9xy.png)
4. Select the Project, and click on Install Button to install the library.

####Download From the Link:
If you prefer a manual approach, you can download the IronBarCode.Dll from the provided link. Add this downloaded DLL to your project as a reference.
After installing IronBarcode add the following namespace in your C# Class:
```
using Ironbarcode;
```
###Step # 3: Write Code to create Barcode:
The following code demonstrates the creation of a barcode image in a very simple way.

```
static void Main(string[] args)
{
    GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128);
    barcode.SaveAsPng("barcode.png");
}
```
I have used IronBarCode.BarcodeWriter.CreateBarcode method, to generate barcode images. In the first parameter, specify the data you want to encode (in this case, a website URL) and the barcode encoding type (Code 128). Furthermore, I have used the barcode.SaveAsPng method to store the generated barcode as a PNG image with the filename "barcode.png."
![Barcode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aj9mazpqbfkihumushas.png)
In this way, Iron Barcode makes generating barcodes in c# very easy.
###Add Annotation Text and Barcode Value to the Barcode image:
Adding annotation text and displaying the barcode value on a barcode is a valuable feature in barcode generation, as it provides context and additional information for those scanning the barcode. Whether it's for product labels, inventory management, or document tracking, this capability enhances the functionality and usability of barcodes. In the following example, we will add annotation text and barcode value using Iron Barcode.
```
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128);
barcode.AddAnnotationTextAboveBarcode("This is Sample Barcode");
barcode.AddBarcodeValueTextBelowBarcode();
barcode.SaveAsPng("barcode.png");
```
The line barcode.AddAnnotationTextAboveBarcode("This is Sample Barcode"); adds annotation text above the barcode to provide context, while the barcode.AddBarcodeValueTextBelowBarcode(); places the barcode's actual value below it. IronBarcode provides methods to add this either below or above the barcode. You can use the AddAnnotationTextBelowBarcode(" ") method to add annotation text below the barcode and AddBarcodeValueTextAboveBarcode() for adding value above the barcode.
![Barcode Image with Annotaion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vcqgs74vo24ebrpog8on.png)
###Styling a Barcode
We can style our barcode by specifying dimensions and changing the background color as well as the barcode color.
####Set Width and Height:
We can style the barcode by specifying the width and height of our barcode. The following code will generate the barcode with a specific width and height.
```
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128,600,150);
```
Add two additional Parameters for Width and height in the Create barcode method to specify the dimension. I have provided the value of 600 for widths and 150 for heights.
![Styling Barcode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k0kpjaxkz5hqqjay7eno.png)
####Change the background and barcode color:
We can change the barcode background and barcode color by using the following code.
```
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128,600,150);

barcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.DarkKhaki);
barcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkOrange, true);

barcode.SaveAsPng("barcode.png");
```
The barcode.ChangeBackgroundColor method changes the background color to DarkKhaki, while the barcode.ChangeBarCodeColor sets the barcode color to DarkOrange, affecting both the bars and any associated text, enhancing the barcode's visual appeal and adaptability to specific design or branding requirements.
The output is as:
![Colored Barcode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/na6z7wiuex9nhvpijvg2.png)
##Read Barcode:
We can also read the barcode value from the barcode image using Iron barcode.
```
var resultFromFile = BarcodeReader.Read(@"barcode.png");
            foreach (var barcodeValue in resultFromFile)
            {
                Console.WriteLine(barcodeValue);
            }
```
The above C# code reads a barcode from a barcode image file named "barcode.png" using the IronBarcode library. It stores the detected barcode values in the resultFromFile variable. Then, it iterates through the results and prints each barcode value to the console.
![Read Barcode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0kbut84ilkk5vkupyh1.png)
In this way, we can develop our own net barcode generator.
##Conclusion:
In conclusion, IronBarcode offers a robust and versatile solution for barcode generation and manipulation within .NET applications. Whether it's creating barcode images for inventory management, product labeling, or document tracking, IronBarcode simplifies the process with its user-friendly API and extensive features. Additionally, the ability to customize barcode appearance, add annotation text, and easily read barcode data from images enhances its utility. IronBarcode simplifies the process of integrating barcodes into applications and is further complemented by a [free trial](https://ironsoftware.com/csharp/barcode/licensing/) for developers to explore its full range of features. With its efficiency, flexibility, and comprehensive documentation, IronBarcode stands as a valuable tool for C# developers, providing a robust and user-friendly solution for their barcode-related needs, all with the added benefit of a risk-free trial period.

On the other hand, [IronQR](https://ironsoftware.com/csharp/qr/) specializes in QR code generation and recognition, employing machine-learning techniques to enhance accuracy and performance. This additional capability makes IronQR a versatile library for applications that require QR code functionality. Together, IronBarcode and IronQR offer developers a powerful set of tools for incorporating barcode and QR code features into their .NET applications.