Detect and Extract Barcodes - WinForms C# .NET 6

This tutorial shows how to create a WinForms C# .NET 6 application that uses the BarcodeEngine and BarcodeReader classes to read barcodes from an image and display their data on the form.

Overview  
Summary This tutorial covers how to use the BarcodeReader Class in a C# WinForms application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform .NET 6 C# Windows WinForms Application
IDE Visual Studio 2022
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and Display Images in an Image Viewer tutorials, before working on the Detect and Extract Barcodes - WinForms C# tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Display Images in an Image Viewer tutorial. If the project is not available, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

If using NuGet references, this tutorial requires the following NuGet packages:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\net:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Barcode Reader Code

With the project created, the references added, and the license set, the Image Viewer initialized, and the load image code added, coding can begin.

Open Form1.cs in the Solution Explorer. Add a new drop down menu titled &Barcode, next to the &File menu drop down. Add a new menu item to the new drop down titled &Read Barcode. Keep the name of the new item as readBarcodeToolStripMenuItem. Double-click on the &Read Barcode menu item to create its click event handler.

Ensure the following using statements are in the using block at the top.

C#
using System; 
using System.Drawing; 
using System.Windows.Forms; 
using System.IO; 
using Leadtools; 
using Leadtools.Controls; 
using Leadtools.Codecs; 
using Leadtools.Barcode; 

Add the following code to Form1_Load to display a loaded image in the Fit zoom mode, this will make it easier to see the recognized barcodes.

C#
private void Form1_Load(object sender, EventArgs e) 
{ 
   // ... 
   // Add following line below existing code 
   _viewer.Zoom(ControlSizeMode.Fit, 1.0, new LeadPoint()); 
} 

add the following code to the readBarcodeToolStripMenuItem_Click method to detect and extract each barcode from the image in the Image Viewer:

C#
private void readBarcodeToolStripMenuItem_Click(object sender, EventArgs e) 
{ 
   try 
   { 
      if (_viewer.Image != null) 
      { 
         BarcodeEngine barcodeEngineInstance = new BarcodeEngine(); 
         BarcodeData[] dataArray = barcodeEngineInstance.Reader.ReadBarcodes(_viewer.Image, LeadRect.Empty, 0, null); 
 
         MessageBox.Show($"{dataArray.Length.ToString()} barcode(s) found"); 
 
         Graphics g = _viewer.CreateGraphics(); 
         for (int i = 0; i < dataArray.Length; i++) 
         { 
            BarcodeData data = dataArray[i]; 
 
            // Convert bounds coordinates from Image to Control 
            LeadRect barcodeLeadRect = _viewer.ConvertRect(_viewer.ActiveItem, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, data.Bounds); 
            // Draw blue rectangle for each on the viewer 
            g.DrawRectangle(new Pen(Color.Red, 4.0f), barcodeLeadRect.X, barcodeLeadRect.Y, barcodeLeadRect.Width, barcodeLeadRect.Height); 
            // Show barcode info 
            MessageBox.Show($"Symbology: {data.Symbology.ToString()}, Location: {data.Bounds.ToString()}, Data: {data.Value}"); 
            // Repaint viewer to remove rectangle 
            _viewer.Refresh(); 
         } 
      } 
      else 
         MessageBox.Show("Load an image first!"); 
   } 
   catch (Exception ex) 
   { 
      MessageBox.Show(ex.Message); 
   } 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application will be able to recognize all barcodes in the image loaded in the viewer. When Read Barcodes is pressed, the application will display a message box showing the number of barcodes detected and cycle through each one displaying a red rectangle and a message box containing the relevant barcode information.

You can test this using the barcode1.tif test file from C:\LEADTOOLS22\Resources\Images.

Wrap-up

This tutorial showed how to read barcode information and display the results in a WinForms application using the LEADTOOLS SDK.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.