Dev Direct Solution Center

For more information and to buy this product...

How to extract barcodes from images in VB.NET and C# using VintaSoftBarcode.NET Library.

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

Introduction

This example demonstrates how to read barcodes from images in your VB.NET, C# or ASP.NET application.

Detail

VintaSoftBarcode.NET Library has two examples:
  • WinForms application written on VB.NET and C# - allows to open an image file from disk, choose desired symbology and extract barcodes.
  • Web application written on VB.NET/ASP.NET - allows to upload an image file with barcodes to the server and recognize barcodes on the server.

Setup

Download and install the free evaluation version of VintaSoftBarcode.NET Library from this link: http://www.vintasoft.com/zip/VSBarcodeNet21.zip.

Running the solution

Open the sample project in Microsoft Visual Studio 2003 or 2005 and build it. You will see the following screen:



Now you can select an image file with barcode and select the type of barcode that you would like to analyze, for example - "Code 39".
Finally, you should press the "Read barcodes" button and get results in bottom-right window.

How the code works

First of all you should choose an image file with barcodes:
[VB.NET]
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then imageWithBarcode = Image.FromFile(OpenFileDialog1.FileName) PictureBox1.Image = New Bitmap(imageWithBarcode) End If
[C#]
if (OpenFileDialog1.ShowDialog() == DialogResult.OK) { imageWithBarcode = Image.FromFile(OpenFileDialog1.FileName); PictureBox1.Image = new Bitmap(imageWithBarcode); }
Next, you should create the object of the BarcodeReader class and initialize it:
[VB.NET]
' Filename is used as a source Dim barcodeReader As BarcodeReader = New BarcodeReader(OpenFileDialog1.FileName) ' Bitmap is used as a source (snippet from ASP.NET) ' Dim barcodeReader As BarcodeReader = New BarcodeReader(New System.Drawing.Bitmap(Request.Files(0).InputStream)) ' pixel of every 5 row and column will be analyzed barcodeReader.ScanInterval = 5 ' 10 first barcodes will be extracted barcodeReader.BarcodesToRead = 10 ' for black-white images only barcodeReader.Threshold = 500 ' only horizontal barcodes will be extracted barcodeReader.ScanDirection = ScanDirection.LeftToRight Or ScanDirection.RightToLeft ' Code 39 and EAN barcodes will be extracted barcodeReader.ScanBarcodeTypes = BarcodeType.Code39 Or BarcodeType.CodeEAN
[C#]
// Filename is used as a source BarcodeReader barcodeReader = new BarcodeReader(OpenFileDialog1.FileName); // Bitmap is used as a source (snippet from ASP.NET) // BarcodeReader barcodeReader = new BarcodeReader(new System.Drawing.Bitmap(Request.Files(0).InputStream)); // pixel of every 5 row and column will be analyzed barcodeReader.ScanInterval = 5; // 10 first barcodes will be extracted barcodeReader.BarcodesToRead = 10; // for black-white images only barcodeReader.Threshold = 500; // only horizontal barcodes will be extracted barcodeReader.ScanDirection = ScanDirection.LeftToRight || ScanDirection.RightToLeft; // Code 39 and EAN barcodes will be extracted barcodeReader.ScanBarcodeTypes = BarcodeType.Code39 || BarcodeType.CodeEAN;
Finally, you can extract barcodes and receive the result:
[VB.NET]
' extract barcodes Dim Infos As BarcodeInfoCollection = barcodeReader.ReadBarcodes() ' get the extraction time Dim sTime As String = "Total time: " + Infos.BarcodesReadTime.TotalSeconds.ToString("0.000") + " seconds." + Environment.NewLine ' if barcodes are not detected If Infos Is Nothing Or Infos.Count < 1 Then resultBarcodes.Text = sTime + "Codes are not detected." Exit Sub End If ' if barcodes are detected Dim Codes As String = "Codes detected[" + Infos.Count.ToString() + "]:" + Environment.NewLine ' get information about extracted barcodes Dim i As Integer For i = 0 To Infos.Count - 1 Codes += Infos(i).ToString() + Environment.NewLine Next i resultBarcodes.Text = sTime + Codes
[C#]
// extract barcodes BarcodeInfoCollection Infos = barcodeReader.ReadBarcodes(); // get the extraction time string sTime = "Total time: " + Infos.BarcodesReadTime.TotalSeconds.ToString("0.000") + " seconds." + Environment.NewLine; // if barcodes are detected if ((Infos == null) || (Infos.Count < 1)) { resultBarcodes.Text = sTime + "Codes are not detected."; return; } // if barcodes are detected string Codes = "Codes detected[" + Infos.Count.ToString() + "]:" + Environment.NewLine; // get information about extracted barcodes for (int i = 0; i < Infos.Count; i++) { Codes += Infos[i].ToString() + Environment.NewLine; } resultBarcodes.Text = sTime + Codes;

Conclusion

This example demonstrates how to read barcodes from images in your VB.NET, C# or ASP.NET application.
This shows just a small sample of the full capabilities of this product.
Visit VintaSoft Ltd for more information and more samples.