Generating Barcode in C#

Introduction

In this article, we will learn how to generate barcode from C# windows application using QRCODE GENERATOR LIBRARY.

Background

In so many situations we need to generate the barcode images for products using C#. We can generate these barcode images and print them on the products, then after we can scan those barcode images and get the description like price, image, etc from the database.

Here I'll discuss how to generate the barcode using C#.

What Is Barcode?

Barcode or Qrcode is the technique to encode the data ex. Text, URL or etc into encoded image format to provide confidential access to the data. In shopping malls, you may see they are scanning those barcodes printed on products and then get the description of the particular product from their server database. For such kind of activity we have to generate those barcodes.

Here we will learn to generate barcode as well as QRcode images also.

Let's take a look step by step.

Step 1: Download QRCODE GENERATOR LIBRARY from onbarcode.com.

Step 2: Open Visual Studio - Create New Project - Windows Form.

Step 3: Add a reference to OnBarcode.Barcode.Winforms.dll.

Step 4: Design form with some input fields for accepting data to encode and the targeted location to save barcode generated image.

Step 5: To generate Barcode, as well as Qrcode images, write two different methods as follows.

private void GenerateBacode(string _data, string _filename)
{
    Linear barcode = new Linear();
    barcode.Type = BarcodeType.CODE11;
    barcode.Data = _data;
    barcode.drawBarcode(_filename);
}
private void GenerateQrcode(string _data, string _filename)
{
    QRCode qrcode = new QRCode();
    qrcode.Data = _data;
    qrcode.DataMode = QRCodeDataMode.Byte;
    qrcode.UOM = UnitOfMeasure.PIXEL;
    qrcode.X = 3;
    qrcode.LeftMargin = 0;
    qrcode.RightMargin = 0;
    qrcode.TopMargin = 0;
    qrcode.BottomMargin = 0;
    qrcode.Resolution = 72;
    qrcode.Rotate = Rotate.Rotate0;
    qrcode.ImageFormat = ImageFormat.Gif;
    qrcode.drawBarcode(_filename);
}

Conclusion

In this way, you can generate barcode and QRcode images in C#. The output will look like bellow

Barcode:

Barcode in C#

Qrcode:

qrcode in C#


Similar Articles