How to Create Barcode Image in VB.NET

By FoxLearn 7/16/2024 9:24:33 AM   11.26K
Creating a barcode image using BarcodeLib in a VB.NET Windows Forms Application is straightforward.

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

First, We will create a new Windows Forms Application project.

Next, Open your form designer, then drag and drop the 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 to your project. You can do that by right clicking on your project in Solution Explorer and select "Manage NuGet Packages...", then search for "BarcodeLib" and install it.

The BarcodeLib is a popular library that allows you to generate barcodes in various formats such as Code 128, Code 39, QR Code, etc. It 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.

'  vb.net barcode generator source code
Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
    ' vb.net barcode
    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 vb.net

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

When you run your application, the barcode will be generated and displayed in the PictureBox. Through the vb.net example above you've learned how to create a barcode vb.net from a string using barcodelib.

VIDEO TUTORIAL