Windows Forms: How to Create Barcode Image in VB.NET

This post shows you How to Create Barcode Image using BarcodeLib in VB.NET Windows Forms Application.

Here's a simple example of how you can generate barcodes using VB.NET

First, We will create a new windows forms project. Next, open your form designer, then drag TextBox, Label, Button and PictureBox controls from the visual studio toolbox to your winform.

Create Barcode Image in VB.NET

You can design a simple UI allows you to enter a string, then create barcode image from a string in Visual Basic .NET

How to Create Barcode Image in VB.NET

The most important thing you need to install the barcodelib from Mange Nuget Packages in your visual studio.

The BarcodeLib library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.

Add the click event handler to the Create button allows you to generate barcode from textbox.

Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
    Dim barcode As Barcode = New Barcode()
    Dim foreColor As Color = Color.Black
    Dim backColor As Color = Color.Transparent
    Dim image As Image = barcode.Encode(TYPE.UPCA, txtBarcode.Text, foreColor, backColor, CInt(picBarcode.Width * 0.8), CInt(picBarcode.Height * 0.8))
    picBarcode.Image = image
End Sub

You should specify the type of barcode you want to create, barcodelib library supports many different types of barcode. such as Code 128, Code11, UPC-A, MSI, ISBN, ITF-14, JAN-13.

Add the click event handler to the Save button allows you to save image from picturebox in

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    If picBarcode.Image Is Nothing Then
    End If
    Dim saveFileDialog As SaveFileDialog = New SaveFileDialog()
    saveFileDialog.Filter = "PNG|*.png"
    If saveFileDialog.ShowDialog() = DialogResult.OK Then
    End If
End Sub

Through the example above you've learned how to create a barcode from a string using barcodelib.