Dev Direct Solution Center

For more information and to buy this product...

Using Barcode Xpress with VB.net and C#.Net to extract barcode values from image files

  For a copy of the sample project please click here to download and install the product evaluation.

Introduction

This sample illustrates how Barcode XPress provides a simple and accurate solution that enables you to write apps that can extract barcode values from scanned documents.

Detail

This sample proves a VB.NET & C#.Net Winforms app that can find and decode barcodes in image files. This could be a scanned document or the output of another program.

Setup

Start by downloading and installing the kit from the link above.

Following the installation, the samples files can be found in the folowing directories:

VB:  C:\Program Files\Pegasus Imaging\Barcode Xpress\V5.0\Samples\DotNet\VB.NET\ReadBarcodesFromDIB

C#:  C:\Program Files\Pegasus Imaging\Barcode Xpress\V5.0\Samples\DotNet\C#\ReadBarcodesFromDIB

Sample images files which contain suitable sample barcodes are located in:

C:\Program Files\Pegasus Imaging\Common\Images

Running the solution

When you run the sample solution you will see the following screen:

Select the type of barcode that you would like to analyze the docment for (for example "1D-Standard") and then use the "File" menu to select an image file to analyze. The sample defaults to the Pegasus Imaging sample images directory, try selecting "Barcode Realworld Code39 #2.tif".

Now hit the "Detect Barcodes" button and a message box containing the values of any barcodes will be displayed.

How the code works

The projects make references to:

PegasusImaging.WinForms.BarcodeXpress5
PegasusImaging.WinForms.ImagXpress8

And import the namespace:

PegasusImaging.WinForms.BarcodeXpress5

The object ImageXView1 is visible on the form MainForm as the control which displays the contents of the image file. The file dialog cd is displayed and the resultant FileName value is used to set the Image property of ImageXView1 via the static method PegasusImaging.WinForms.ImagXpress8.ImageX.FromFile. This value is also used to drive the barcode analysis later on.

VB.Net
Private Sub OpenMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenMenuItem.Click cd.Filter = "TIFF Files (*.TIF)|*.TIF|All Files (*.*)|*.*" cd.Title = "Open a 1-Bit Black and White Image File" cd.InitialDirectory = Application.ExecutablePath & "\..\..\..\..\..\..\..\..\Common\Images" cd.ShowDialog(Me) Try If Not (cd.FileName = "") Then ImageXView1.Image = PegasusImaging.WinForms.ImagXpress8.ImageX.FromFile(cd.FileName) End If . . End Sub
C#.Net
private void OpenMenuItem_Click(object sender, EventArgs e) { cd.Filter = "TIFF Files (*.TIF)|*.TIF|All Files (*.*)|*.*"; cd.Title = "Open a 1-Bit Black and White Image File"; cd.InitialDirectory = Application.ExecutablePath + "\\..\\..\\..\\..\\..\\..\\..\\..\\..\\Common\\Images"; cd.ShowDialog(this); try { imageXView1.Image = PegasusImaging.WinForms.ImagXpress8.ImageX.FromFile(cd.FileName); } . . }

The important code elements of the barcode analysis are located in the handler for the "Detect Barcodes" button cmdread_Click.

The code makes several references to barcodeXpress1 which is a control that has been added to the application's MainForm.

VB.Net
Private Sub cmdread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdread.Click Try . . Dim BarcodeTypes As System.Array Dim DIB As System.Int32 BarcodeTypes = SetBarcodeType() Dim results As PegasusImaging.WinForms.BarcodeXpress5.Result() BarcodeXpress1.reader.BarcodeTypes = BarcodeTypes Dim currentArea As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 0, 0, 0) BarcodeXpress1.reader.Area = currentArea
C#.Net
private void cmdread_Click(object sender, System.EventArgs e) { try { . . System.Array BarcodeTypes; System.Int32 DIB; BarcodeTypes = SetBarcodeType(); PegasusImaging.WinForms.BarcodeXpress5.Result[] results; barcodeXpress1.reader.BarcodeTypes = BarcodeTypes; System.Drawing.Rectangle currentArea = new System.Drawing.Rectangle(0, 0, 0, 0); barcodeXpress1.reader.Area = currentArea;

The above code sets up several variables which are used in the analysis of the file. DIB is used to hold a handle to the image in memory during the process. BarcodeTypes is an array which holds the type of barcode being analysed and currentArea is a System.Drawing.Rectangle which controls the area of the file to be analysed.

An array called results of type PegasusImaging.WinForms.BarcodeXpress5.Result[] is set up to receive the results of the analysis.

The barcode type is set up in the function SetBarcodeType. This sets a value of type PegasusImaging.WinForms.BarcodeXpress5.BarcodeType according to the users choice of barcode type.

VB.Net
Private Function SetBarcodeType() As System.Array . . Dim currentBarcodeTypes As System.Array = New BarcodeType(currentcount) {} If (read1d.Checked) Then currentBarcodeTypes.SetValue (PegasusImaging.WinForms.BarcodeXpress5.BarcodeType.UnknownBarcode, currentcount) currentcount = currentcount + 1 End If . . Return currentBarcodeTypes End Function
C#.Net
private System.Array SetBarcodeType() { . . System.Array currentBarcodeTypes = new BarcodeType[currentcount]; if (read1d.Checked) currentBarcodeTypes.SetValue(PegasusImaging.WinForms.BarcodeXpress5.BarcodeType.UnknownBarcode,currentcount++); . . return currentBarcodeTypes; }

In order to perform the actual analysis, the image that was loaded into the ImageXView1 object us converted to a DIB and the handle returned to the DIB Integer variable.

The DIB is then passed into the Analyze method of barcodeXpress1.reader which returns any barcode values into the results array set up earlier.

VB.Net
DIB = ImageXView1.Image.ToHdib(False).ToInt32() results = BarcodeXpress1.reader.Analyze(DIB)
C#.Net
DIB = imageXView1.Image.ToHdib(false).ToInt32(); results = barcodeXpress1.reader.Analyze(DIB);

Following the analysis, a loop is performed around the results array to access any barcode values that were read.

VB.Net
If (results.Length > 0) Then Dim strResult As String strResult = "" Dim i As Integer For i = 0 To results.Length - 1 Step 1 Dim curResult As PegasusImaging.WinForms.BarcodeXpress5.Result = CType(results.GetValue(i), PegasusImaging.WinForms.BarcodeXpress5.Result) strResult += "Symbol #" + i.ToString() + Chr(13) + "Type = " + curResult.BarcodeName + Chr(13) strResult += "Value=" + curResult.BarcodeValue + Chr(13) Next strResult += Chr(13) MessageBox.Show(strResult) Else MessageBox.Show("No Barcodes Found") End If 'Must free the DIB passed into the Analyze method Marshal.FreeHGlobal(New System.IntPtr(DIB))
C#.Net
if (results.Length > 0) { string strResult; strResult = ""; for (int i = 0; i < results.Length; i++) { PegasusImaging.WinForms.BarcodeXpress5.Result curResult = (PegasusImaging.WinForms.BarcodeXpress5.Result)results.GetValue(i); strResult += "Symbol #" + i + "\r" + "Type = " + curResult.BarcodeName + "\r"; strResult += "Value=" + curResult.BarcodeValue + "\r"; string strMsg = "Symbol #" + i + " - " + curResult.BarcodeData; } strResult += "\n"; MessageBox.Show(strResult, "Barcode Result", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("No Barcodes Found", "Barcode Result", MessageBoxButtons.OK, MessageBoxIcon.Warning); } //Must free the DIB that was passed into the Analyze method Marshal.FreeHGlobal(new System.IntPtr(DIB));

Each array element is cast to an object of type PegasusImaging.WinForms.BarcodeXpress5.Result which has the properties BarcodeName and BarcodeValue which contain the important values of interest to your application.

Finally the DIB is cleared to free the memory used.

Conculsion

This sample has shown how easily we can read the numeric barcode values from bitmaps in image files using Pegasus Image Xpress to handle the image file and Barcode Xpress to scan the barcodes.

This shows just a small sample of the full capabilities of these products.

Visit Pegasus Imaging Corporation for more information and more samples.