It is quite common to generate barcode or QR Code in C# web-application or win-form application while working on any E-commerce related project,as In one of the previous post, we mentioned how you can generate QR Code in C# MVC project usng Zxing.net, so in this post, I will explaining how we can generate barcode in C# using Nuget package Barcodelib and read barcode in C#
Step 1: Create a new Console app in Visual Studio to generate or read barcode in C#
I will be creating C# console application in Visual Studio to show example to generate barcode in C#, but you can use the same C# code in Win-Forms or MVC web-app or Web-Forms.( You may have to modify few things like image path etc.)
Open your Visual Studio, and navigate to File-> New -> Project -> Select "Windows Classic" from left-pane and "Console app (.NET Framework)", provide the name "CSharpBarcodeGenerator" and click "Ok"
Step 2: Install BarcodeLibe Nuget package using Nuget Package manager
Now, we need to install BarCodeLib Nuget package, which we will use to generate barcode using C# code, so navigate to "Tools"-> "Nuget Package Manager" -> "Manage nuget package for this solution" -> Select "Browse" tab and search for "BarcodeLib".
Select your project in which you want to install this, and click "Install".
Barcodelib library, contains a class called BarcodeLib with three constructors:
Barcode();
Barcode(string);
Barcode(string, BarcodeLib.TYPE);
If you decide to create an instance with parameters, the parameters are as follows: the string is the data to be encoded into the barcode, and BarcodeLib.TYPE is the symbology to encode the data with.
Step 3: Generate barcode in C# using BarCodeLib
Once you have installed the Nuget package, we can now use it to generate barcode, so in your Program.cs
, you can write the code as below
Barcode type 11
using BarcodeLib;
using System.Drawing;
using System.Drawing.Imaging;
namespace CSharpBarcodeGenerator
{
class Program
{
static void Main(string[] args)
{
// Create an instance of the API
Barcode barcodLib = new Barcode();
int imageWidth = 250; // barcode image width
int imageHeight = 110; //barcode image height
Color foreColor = Color.Black; // Color to print barcode
Color backColor = Color.Transparent; //background color
//numeric string to generate barcode
string NumericString = "123-90122-333";
// Generate the barcode with your settings
Image barcodeImage = barcodLib.Encode(TYPE.CODE11, NumericString, foreColor, backColor, imageWidth, imageHeight);
// Store image in some path with the desired format
//note: you must have permission to save file in the specified path
barcodeImage.Save(@"D:\Barcode.png", ImageFormat.Png);
}
}
}
Once you will execute above code in your C# Console app, you can see barcode image will be generate at at "D:\Barcode.png", here is the output of the image
BarCode type 128
We just need to change barcode type in the code placed here
// Generate the barcode with your settings
Image barcodeImage = barcodLib.Encode(TYPE.CODE128, NumericString, foreColor, backColor, imageWidth, imageHeight);
So complete code remain's same
using BarcodeLib;
using System.Drawing;
using System.Drawing.Imaging;
namespace CSharpBarcodeGenerator
{
class Program
{
static void Main(string[] args)
{
// Create an instance of the API
Barcode barcodLib = new Barcode();
int imageWidth = 250; // barcode image width
int imageHeight = 110; //barcode image height
Color foreColor = Color.Black; // Color to print barcode
Color backColor = Color.Transparent; //background color
//numeric string to generate barcode
string NumericString = "123-90122-333";
// Generate the barcode with your settings
Image barcodeImage = barcodLib.Encode(TYPE.CODE128, NumericString, foreColor, backColor, imageWidth, imageHeight);
// Store image in some path with the desired format
//note: you must have permission to save file in the specified path
barcodeImage.Save(@"D:\Barcode.png", ImageFormat.Png);
}
}
}
Output:
Barcode type UPCA
In this type barcode only numeric string is allowed
using BarcodeLib;
using System.Drawing;
using System.Drawing.Imaging;
namespace CSharpBarcodeGenerator
{
class Program
{
static void Main(string[] args)
{
// Create an instance of the API
Barcode barcodLib = new Barcode();
int imageWidth = 290; // barcode image width
int imageHeight = 120; //barcode image height
Color foreColor = Color.Black; // Color to print barcode
Color backColor = Color.White; //background color
//only numbers are allowed in UPCA type
string NumericString = "038000350216";
//type upca
Image barcodeImage = barcodLib.Encode(TYPE.UPCA, NumericString, foreColor, backColor, imageWidth, imageHeight);
// Store image in some path with the desired format
//note: you must have permission to save file in the specified path
barcodeImage.Save(@"D:\Barcode.png", ImageFormat.Png);
}
}
}
Output
Read Barcode in C#
In the above examples, we are generating barcode, but now we will read barcode and convert barcode image into text using C#.
We will need to download BarcodeReader library from here
Once we have downloaded the barcode reader library, right-click on your project inside your Visual Studio solution, and click "Add Reference"
Select "Browse" tab, and add "BarcodeLib.BarcodeReader.dll" in your project
Once you have added barcode reader library and considering we have barcode images saved as "D:\Barcode-upca.png" in our hard drive location, we can read barcode using C# as below
using BarcodeLib.BarcodeReader;
using System;
using System.Text;
namespace CSharpBarcodeGenerator
{
class Program
{
static void Main(string[] args)
{
string[] BarcodeUPCA = BarcodeReader.read(@"D:\Barcode-upca.png", BarcodeReader.UPCA);
Console.WriteLine("UPCA Code:" + ConvertStringArrayToString(BarcodeUPCA));
}
static string ConvertStringArrayToString(string[] array)
{
// Concatenate all the elements into a StringBuilder.
StringBuilder builder = new StringBuilder();
foreach (string value in array)
{
builder.Append(value);
}
return builder.ToString();
}
}
}
Output:
The above UPCA Code "038000350214" as same as we entered above while generating barcode image.
You may also like to read:
QR Code generate and Read in MVC
Read Excel File in C# using OleDB / EPPlus
Import csv into SQL server (with query OR without query using SSMS)