Barcode Generator in VB.NET Tutorial and Source code

Barcode Generator in VB.NET Tutorial and Source code

Problem

Create a Windows Form Application program in Visual Basic.Net that will allow users to generate and save an image of a barcode.

Description

This tutorial will allow the user to generate a barcode by entering a string or number in the textbox and will be displayed at the picture box as a barcode. The user is able to select which type of barcode are to be generated on a combo box, once displayed, the user will be able to save the image of the barcode.

Before the tutorial the following are required to start:

  1. Microsoft Visual Studio 2008 – Above
  2. MessagingToolkit.Barcode.dll

The tutorial starts here:

  1. Download MessagingToolkit.Barcode.dll from the internet and save it to your preferred directory.
  2. Open Microsoft Visual Studio 2012
  3. Select a New Project on the File menu.
  4. Select Visual Basic, Windows Form Application then click OK.
  5. On the solution Panel, right click the “name of your project” then click add reference
Barcode Generator in VB.NET Tutorial and Source code - Step 5
Barcode Generator in VB.NET Tutorial and Source code – Step 5

solution explorer.png (image)

  1. On the reference manager, Click Browse on the left panel and check the MessagingToolkit.Barcode.dll, if it doesn’t appear, then click the Browse button on the bottom. Search and select for MessagingToolkit.Barcode.dll then click “Add”
Barcode Generator in VB.NET Tutorial and Source code - Step 6
Barcode Generator in VB.NET Tutorial and Source code – Step 6

­

reference manager.png (image)

  1. Click “Ok” and we are ready now to design our form
  2. We need to design our form by the following controls:
  • 2 labels – label for the Barcode Type combo box and Code text box
  • 1 text box – text box for the Code.
  • 1 Command Button – 1 button to save the barcode to a directory.
  • 1 Picture Box – picture box where the barcode is displayed.
  • 1 Combo Box – where you can select the type of the barcode
  1. We will also name our form controls in this way:
  • txtCode is the name of the textbox for textbox
  • cmdSave is the name of the button for save
  • pbBarcode is the name of the picture box where the barcode will be displayed.
  • cmbType is the name of the combo box where you choose the type of the barcode
  1. This is how we design the form. (Feel free to layout your own)
Barcode Generator in VB.NET Tutorial and Source code - Form Design
Barcode Generator in VB.NET Tutorial and Source code – Form Design

­­­

Form Design.png (image) ­­

Figure 1. Design of the Form

  1. Click the Combo Box, click the small triangle above and click “Add items…” then add the following:
  • ISBN
  • Code93
  • EAN13
  1. Double click the Text box and paste the following code.

Code here

If cmbType.text = “EAN13”
Dim Generator As New MessagingToolkit.Barcode.BarcodeEncoder
        Generator.BackColor = Color.White
        Generator.LabelFont = New Font("Arial", 7, FontStyle.Regular)
        Generator.IncludeLabel = True
        Generator.CustomLabel = "Code: " & txtCode.Text
        Try
            pbBarcode.Image = New Bitmap(Generator.Encode(MessagingToolkit.Barcode.BarcodeFormat.EAN13, txtCode.Text))
        Catch ex As Exception
        End Try
End If

If cmbType. text = “ISBN”
Dim Generator As New MessagingToolkit.Barcode.BarcodeEncoder
        Generator.BackColor = Color.White
        Generator.LabelFont = New Font("Arial", 7, FontStyle.Regular)
        Generator.IncludeLabel = True
        Generator.CustomLabel = "Code: " & txtCode.Text
        Try
            pbBarcode.Image = New Bitmap(Generator.Encode(MessagingToolkit.Barcode.BarcodeFormat.ISBN, txtCode.Text))
        Catch ex As Exception
        End Try
End If

If cmbType. text = “Code 93”
Dim Generator As New MessagingToolkit.Barcode.BarcodeEncoder
        Generator.BackColor = Color.White
        Generator.LabelFont = New Font("Arial", 7, FontStyle.Regular)
        Generator.IncludeLabel = True
        Generator.CustomLabel = "Code: " & txtCode.Text
        Try
            pbBarcode.Image = New Bitmap(Generator.Encode(MessagingToolkit.Barcode.BarcodeFormat.Code 93, txtCode.Text))
        Catch ex As Exception
        End Try
End If

End Code

Code Explanation:

The code above will recognize what type of barcode are going to be displayed in the Picture Box from the Combo box. The MessagingToolkit.Barcode.BarcodeEncoder.dll will load according to what type barcode is chosen in the combo box then reads the string from the text box and display it to the picture box. You can paste the code as much as you want then change/add any Barcode Format on the combo box.

  1. Double click the Command Buttton and paste the following code.

Code here

Dim SD As New SaveFileDialog
        SD.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        SD.FileName = txtCode.Text
        SD.SupportMultiDottedExtensions = True
        SD.AddExtension = True
        SD.Filter = "PNG File|*.png"
        If SD.ShowDialog() = DialogResult.OK Then
            Try
                pbBarcode.Image.Save(SD.FileName, Imaging.ImageFormat.Png)
            Catch ex As Exception
            End Try
        End If
End Code

Code Explanation:

The code will show a Save file Dialogue from an initial Directory and will save the image in a .png image file extension.

Barcode Generator in VB.NET Tutorial and Source code - Final Output
Barcode Generator in VB.NET Tutorial and Source code – Final Output

output.png (image)

Author:

Name: Charlie Devera
Email Address: charliedevera000@gmail.com

Free Download Source code (Barcode Generator in VB.NET)

You may visit our facebook page for more information, inquiries and comments.

Hire our team to do the project.

, , ,

Post navigation