How to Create Barcode Images from a string in C#
By FoxLearn 7/16/2024 9:25:12 AM 11.81K
If you want to generate one dimensional barcodes, you can use the BarcodeLib to create different types of barcodes in c#. The barcodelib is an open source library that provides an easy class for developers to use when they need to create barcode images from a string of data.
How to Create Barcode Images from a string in C#
Creating barcode images from strings using BarcodeLib in a C# Windows Forms Application is relatively straightforward.
Here's a step-by-step guide to help you achieve this
Creating Barcode Images in C#
For example: UPC-A, CODE 128, CODE 11, ISBN, ITF14, EAN13
To create barcode image c# you can design a simple ui as shown below.
First, you need to install the BarcodeLib library into your project by right-clicking on your project, then select Manage Nuget Packages... ->search 'barcodelib'->install it.
You can also do this via NuGet Package Manager Console by running the following command.
Install-Package BarcodeLib
Make sure to import the BarcodeLib namespace in your C# file where you want to use it
using BarcodeLib;
Adding the click event handler to the Generate button that allows you to create barcode image in c#.
You can create a barcode image by instantiating the Barcode
class and then calling its Encode
method, passing your string and the type of barcode you want to generate.
// c# generate barcode image private void btnGenerate_Click(object sender, EventArgs e) { // c# barcode barcodelib generate Barcode barcode = new Barcode(); Color foreColor = Color.Black; Color backColor = Color.Transparent; Image image = barcode.Encode(TYPE.UPCA, txtBarcode.Text, foreColor, backColor, (int)(picBarcode.Width * 0.8), (int)(picBarcode.Height * 0.8)); picBarcode.Image = image; }
You can display the generated barcode image in your Windows Forms application. For example, if you have a PictureBox named picBarcode
on your form, you can assign the generated image to it.
picBarcode.Image = image;
Alternatively, you can save the image to a file.
barcode.Save("barcode.png", ImageFormat.Png);
BarcodeLib allows for customization of various parameters such as barcode width, height, font, etc. You can explore these options to tailor the appearance of your barcode to your needs.
You can create a barcode image with the width*80% and height*80% of PictureBox size.
UPC-A: You should enter 11 or 12 number
For example to encode "027010346149"
If you want to save the barcode to an image file you can write your code as shown below.
string fileName = Application.StartupPath + "\\upca.png"; barcode.Save(fileName, ImageFormat.Png);
You can also use the SaveFileDialog to enter your barcode image name and save it to your disk.
using(SaveFileDialog sfd = new SaveFileDialog() { Filter = "PNG|*.png"}) { if(sfd.ShowDialog() == DialogResult.OK) image.Save(sfd.FileName, ImageFormat.Png); }
CODE128
You only need to change TYPE.UPCA to TYPE.CODE128
For example to encode "12345678".
CODE 11
Change the barcode type to TYPE.CODE11
For example to encode "123-4610"
ISBN
Change the barcode type to TYPE.ISBN
For example to encode "9782181474100"
ITF14
Change the barcode type to TYPE.ITF14
For example to encode "13352056870252"
EAN13
Change the barcode type to TYPE.EAN13
For example to encode "965120147762"
Through this c# example above, I showed you how to use the free open source barcodelib to generate multiple types of barcodes. Such as, UPC-A, CODE 128, CODE 11, ISBN, ITF14, EAN13.