A barcode label printed from VB.NET

Barcode in VB.NET

In this example, we will print barcode labels on laser and thermal printers using the standard .NET's PrintDocument and COM Class-based version of StrokeScribe barcode generator.

Requirements

Creating a Basic Barcode VB.NET Application

First, create a new VB.NET console application in Visual Studio:

Creating a new console application in Visual Studio

Now, add a reference to the StrokeScribe Class. Execute Project->Add Reference from the IDE menu, switch to the COM->Type Libraries in the Reference Manager and check the StrokeScribe Class as shown on the picture.

Addng a COM reference to VB project

You will also need a reference to the System.Drawing namespace. Look for it in the Assemblies tab.

How to Save a Barcode Image into a File

1. In Visual Studio, create a new VB.NET console application project.

2. Add a reference to StrokeScribe Class.

3. Paste the following code into your project:

Imports StrokeScribeClass = STROKESCRIBECLSLib.StrokeScribeClass Imports STROKESCRIBECLSLib.enumAlphabet Imports STROKESCRIBECLSLib.enumQR_ECL Imports Formats = STROKESCRIBECLSLib.enumFormats Module Module1 Sub Main() Dim barcode As New StrokeScribeClass barcode.Alphabet = CODE128 barcode.Text = "123ABCD" Dim barcode_w As Integer = barcode.BitmapW * 3 Dim barcode_h As Integer = barcode.BitmapW * 2 Dim rc As Long rc = barcode.SavePicture("barcode.bmp", Formats.BMP, barcode_w, barcode_h) If rc <> 0 Then Debug.Print(barcode.ErrorDescription) End If End Sub End Module

4. Build and run your application.

A barcode picture created with the VB.NET app

5. Look for the barcode.bmp in the project folder.

The application creates a barcode with the width:height proportion of 3:2. The BitmapW property returns the minimum picture width in pixels that fits the barcode. For best looking BMP/JPG/PNG images, always set the width as a multiple of the value reported by the BitmapW.

2D Barcodes

For 2D barcodes (QR COde, Data Matrix, PDF417), use the following code:

barcode.Alphabet = PDF417 rc = barcode.SavePicture("barcode.bmp", Formats.BMP, barcode.BitmapW, barcode.BitmapH) A 2D barcode picture file created with in VB.NET

Saving Barcodes as Vector Drawings

For vector based barcode pictures in EMF/WMF formats, you may specify any values for width/height.

WMF format accepts TWIPs (1440 TWIPs per inch):

barcode.Alphabet = QRCODE rc = barcode.SavePicture("qrcode.wmf", Formats.WMF, 1440, 1440) ' 1x1 inch QR CODE

EMF accepts millimeters:

barcode.Alphabet = DATAMATRIX rc = barcode.SavePicture("datamatrix.emf", Formats.EMF, 10, 10) '10x10mm. DATA MATRIX

Printing Barcodes in VB.NET

Paste the following code into the project:

Imports System.Drawing Imports System.Drawing.Printing Imports StrokeScribeClass = STROKESCRIBECLSLib.StrokeScribeClass Imports STROKESCRIBECLSLib.enumAlphabet Imports STROKESCRIBECLSLib.enumQR_ECL Imports Formats = STROKESCRIBECLSLib.enumFormats Module Module1 Sub Main() Dim PrintDoc As New Printing.PrintDocument AddHandler PrintDoc.PrintPage, AddressOf PrintHandler PrintDoc.Print() End Sub Private Sub PrintHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs) Dim barcode As New StrokeScribeClass barcode.Alphabet = CODE128 barcode.Text = "BARCODE" Dim barcode_w As Integer = barcode.BitmapW * 3 Dim barcode_h As Integer = barcode.BitmapW * 2 Dim olepicture As stdole.StdPicture olepicture = barcode.GetPictureHandle(Formats.BMP, barcode_w, barcode_h) If barcode.Error Then MsgBox(barcode.ErrorDescription) Return End If Dim img As Image img = Image.FromHbitmap(olepicture.Handle) Dim ag As Graphics = args.Graphics ag.PageUnit = GraphicsUnit.Pixel Dim bounds = ag.VisibleClipBounds Dim pos_x As Integer = bounds.Left + bounds.Width / 2 - barcode_w / 2 ' Centering the barcode on the page Dim pos_y As Integer = bounds.Top + bounds.Height / 2 - barcode_h / 2 ag.DrawImage(img, pos_x, pos_y, barcode_w, barcode_h) Dim fnt As New Font("Arial", 10) ag.DrawString("A BARCODE LABEL", fnt, Brushes.Black, bounds.Left, bounds.Top + 10) args.HasMorePages = False ' Change this if you need to print multiple barcode labels at once End Sub End Module

To build the code, you need to add a reference to the System.Drawing namespace.

We tested the VB application with different barcode types on a Zebra thermal printer. Click the picture below to see the printout in high resolution (1Mb):

Printing barcodes on a thermal printer

Printing to a Non-default Printer

If you want to select a printer instead of using the system default printer, do the following:

1. Import the Forms namespace:

Imports System.Windows.Forms

2. Add a reference to the System.Windows.Forms in the Reference Manager.

3. Modify the Sub Main() as follows:

Sub Main() Dim PrintDoc As New Printing.PrintDocument Dim PrintDlg As New PrintDialog PrintDlg.Document = PrintDoc PrintDlg.PrinterSettings = PrintDoc.PrinterSettings If PrintDlg.ShowDialog <> DialogResult.OK Then Exit Sub End If AddHandler PrintDoc.PrintPage, AddressOf PrintHandler PrintDoc.Print() End Sub