generate barcode using C#

Barcodes are used to visually represent the data in a machine-readable form. Most often, barcodes appear to be a means of a product’s identification. Various types of barcodes are used in different scenarios such as Code128, QR, Datamatrix, Aztec, etc. In this article, you will learn how to generate the barcodes in C#. Furthermore, you will see how to customize the appearance of a barcode.

C# Barcode API - Free Download

Aspose.BarCode for .NET is a powerful barcode generator and scanner API. It lets you generate and read a wide range of barcode symbologies, including but not limited to:

  • Code128
  • Code11
  • Code39
  • QR
  • Datamatrix
  • EAN13
  • EAN8
  • ITF14
  • PDF417
  • and more.

You can download the API for free or get it installed within your .NET application using NuGet.

PM> Install-Package Aspose.BarCode

How to Generate a Barcode using C#

The following are the steps to generate a barcode using Aspose.BarCode for .NET.

  • First, create an object of BarcodeGenerator class and specify the barcode’s type and text in its constructor.
  • Set barcode’s features such as resolution and etc.
  • Finally, generate barcode using BarcodeGenerator.Save(String) method.

The following code sample shows how to generate a barcode using C#.

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "Aspose.BarCode");
// set resolution
generator.Parameters.Resolution = 400;
// generate barcode
generator.Save("generate-barcode.png");
generate barcode in C#

How to Generate a QR Barcode using C#

You can generate other types of barcodes in a similar way. For the demonstration, let’s generate a QR barcode. The following are the steps to generate a QR barcode.

The following code sample shows how to generate a QR barcode using C#.

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, "Aspose.BarCode");
// set resolution
generator.Parameters.Resolution = 400;
// generate barcode
generator.Save("generate-barcode.png");
generate QR barcode in C#

Customize the Appearance of a Barcode in C#

You can also customize the appearance of the barcode. For example, you can change its font, forecolor, background color, text color, and etc. The following are the steps to customize a barcode using Aspose.BarCode for .NET.

The following code sample shows how to generate a customized Aztec barcode using C#.

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Aztec, "Aspose.BarCode");
// set barcode's back color
generator.Parameters.BackColor = System.Drawing.Color.DarkGray;
// set barcode's bar color
generator.Parameters.Barcode.BarColor = System.Drawing.Color.Orange;
// set border color
generator.Parameters.Border.Color = System.Drawing.Color.Black;
// set text color
generator.Parameters.Barcode.CodeTextParameters.Color = System.Drawing.Color.Orange;
// set resolution
generator.Parameters.Resolution = 400;
// generate barcode
generator.Save("generate-barcode.png");
generate Aztec barcode in C#

Add Caption in Barcodes using C#

You might have seen barcodes with a caption above or below the barcode image. These captions can be used to display additional information about the barcode itself. Aspose.BarCode for .NET also allows you to add a caption below, above, or on both sides of the barcode. The following are the steps to add a caption.

The following code sample shows how to add caption in a barcode using C#.

BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "Aspose.BarCode");
// set captions
generator.Parameters.CaptionAbove.Text = "The caption above.";
generator.Parameters.CaptionAbove.Visible = true;
generator.Parameters.CaptionBelow.Text = "The caption below.";
generator.Parameters.CaptionBelow.Visible = true;
// generate barcode
generator.Save("generate-barcode.png");
generate barcode with caption in C#

Conclusion

In this article, you have learned how to generate barcodes programmatically using C#. In addition, you have seen how to customize a barcode by modifying its appearance. You can explore more about the C# barcode generator API using documentation.

See Also