Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Given the swift rise in barcode usage, developers must be able to generate barcodes in their preferred programming language. So, this tutorial will demonstrate how to generate barcodes in .NET.
Barcode Generator .NET Tutorial
This tutorial uses the latest version of Visual Studio and the Windows Forms Application template. You can use the application of your choice and use your own existing project and version.
Open Visual Studio > Click on Create New Project > Select Windows Forms Application Template > Press Next > Name the Project > Press Next => Select your target .NET Framework => Click on Create Button.
Create a new Windows Forms Application
There are many benefits to installing a barcode generator library. IronBarcode, written in C#, provides functions to create barcodes and QR codes with just a single line of code. It also supports saving the QR code or barcode in the desired file format. Furthermore, it provides free service and runtime support for generating barcodes in .NET.
Let's start with installing the IronBarcode NuGet Package. You can install it using one of the following three methods:
Write the following command in the Package Manager Console. It will download and install the package for you.
Install-Package BarCode
Package Manager Console installation step
You can also install the Barcode Package by using the NuGet Package Solution. Simply follow these steps:
Click on Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
This will open NuGet Package Manager for you. Click on Browse and search BarCode, then install the library.
NuGet Package Manager UI
As an alternative, the IronBarCode.Dll can be downloaded and added to your project as a reference from .NET Barcode DLL.
The UI for the .NET Barcode generator should have 2 labels, 1 rich text box, and 1 picture box to display the generated barcode image. The image below shows a simple design for demonstration purposes.
Design the Windows Forms Application
Double-click the "Generate" button. The following code will appear:
private void button1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Add the following namespace
using IronBarCode;
using System;
using System.Drawing;
using System.Windows.Forms;
using IronBarCode;
using System;
using System.Drawing;
using System.Windows.Forms;
Imports IronBarCode
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Write the following code inside the button1_Click()
function:
GeneratedBarcode MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128);
MyBarCode.SaveAsPng("MyBarCode.png");
BarcodeImage.Image = new Bitmap("MyBarCode.png");
GeneratedBarcode MyBarCode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128);
MyBarCode.SaveAsPng("MyBarCode.png");
BarcodeImage.Image = new Bitmap("MyBarCode.png");
Dim MyBarCode As GeneratedBarcode = IronBarCode.BarcodeWriter.CreateBarcode(BarcodeValue.Text, BarcodeWriterEncoding.Code128)
MyBarCode.SaveAsPng("MyBarCode.png")
BarcodeImage.Image = New Bitmap("MyBarCode.png")
Let's understand the code, line by line.
GeneratedBarcode
is a data type of barcode. CreateBarcode
is the function provided by the BarcodeWriter
class of the IronBarCode
package.
BarcodeValue.Text
is the value of the TextBox which will be provided by the user on run time.
BarcodeWriterEncoding.Code128
is the encoding scheme used for generating barcodes. This code type can be changed to another encoding scheme, BarcodeWriterEncoding.QRCode
, to generate a QR code.
IronBarcode provides an ability to interact with multiple image formats such as PNG, JPEG, HTML, PDF, GIF or TIFF, such as SaveAsPng
to save a barcode in PNG format. Additionally, IronBarcode also works with PDF files to extract barcodes or QR codes from all pages.
BarcodeImage
is the name of the PictureBox to display the generated barcode images to the user.
Press Ctrl + F5 to run the application.
Run the Barcode generator application
Write the value inside the text box that you want to encode in the barcode as shown below.
Paste the URL to generate a barcode
Now, click the "Generate" button. The barcode will be generated as shown below.
Generated barcode in the Windows Forms Application
Next, display the value of the barcode in a single line of code:
MyBarCode.AddBarcodeValueTextBelowBarcode();
MyBarCode.AddBarcodeValueTextBelowBarcode();
MyBarCode.AddBarcodeValueTextBelowBarcode()
Generate a barcode from a string value
IronBarcode features a friendly API for developers to read and write barcodes for .NET, optimizing accuracy and ensuring a low error rate in real-world software. Visit the official documentation page for more information about IronBarcode.
Currently, if you buy the complete Iron Suite, you can get five libraries for the price of two. For more information.
You can also download a zip file project.
9 .NET API products for your office documents