C# Barcode Reader library for .NET WinForms, WPF Windows Applications
Barcode Reader for .NET is the best .NET / C# barcode image recognition component in the market.
Barcode Reader C# library supports scan and recognize linear & 2d barcode images in all .NET Desktop projects:
Windows Forms App (.NET Core and .NET Framework)
WPF Application, Class Library, Custom Control Library, User Control Library (.NET Core and .NET Framework)
Windows Service (.NET Framework)
.NET Console Applications
The following tutorial will show you how to scan, read QR Code and barcodes from existing image files using C# in your .NET
Windows Forms desktop applications.
C# Barcode Reader Library
QR Code, Data Matrix, Code 128, GS1, EAN/UPC. Multi-thread reading in C# Windows application.
Start Visual Studio 2022 and choose Create a new project.
Choose Windows Forms App in the dialog, and click Next button.
Create a new project with name BarcodeLib.BarcodeReader.WinFormsDemo. Click Next button.
Choose Framework as .NET 8.0 (Long Term Support), and Click Create.
Now we have created a new C# Windows Forms project successfully.
2. Add barcode scanner library
Add the following Barcode Reader library dll and nuget package to the Windows Forms project.
Add barcode reader library .net standard dll BarcodeLib.BarcodeReader.Common.dll to the project reference
Search and install nuget System.Drawing.Common (latest stable version) from NuGet Package Manager in Visual Studio
3. Add, design controls on Form
Add the following Windows Forms controls to Form1.
One button with name btnFile. Text is Select File. Click on the button, the app will popup a dialog to select an image file to scan barcode.
One text box with name tbFilePath. The text box will display the selected image file path.
One label with default name. Text is Barcode Type : .
One combo box with name cbBarcodeType. The list shows all supported barcode types by the barcode reader.
One button with name btnScan. Text is Scan Image. Click the button to start scanning barcode image.
One picture box with name pbImage. It will display the scanning image on the form.
One text box with name tbResult. It will display the barcode image scanning result.
Design and align the controls on the form.
4. Add C# code to read barcode
Here will add C# code to scan barcodes from selected image file.
Righ click file Forms1.cs in Solution Explorer, and choose View Codefrom the menu to open the Form1.cs window.
Copy and replace the Form1.cs content with the following C# codes.
The C# codes below will set the form UI control initial states, and C# codes to handle the two buttons' click events.
usingBarcodeLib.BarcodeReader;
usingSystem;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceBarcodeLib.BarcodeReader.WinFormsDemo
{
publicpartialclassForm1 : Form
{
publicForm1()
{
InitializeComponent();
this.tbFilePath.ReadOnly=true;
this.cbBarcodeType.Items.AddRange(newString[] {
"Codabar", "Code 128 / GS1-128", "Code 39", "Code 39 Full ASCII",
"Interleaved 2 of 5", "EAN-13", "EAN-8",
"UPC-A", "UPC-E", "QR Code", "PDF-417",
"Data Matrix", "IDENTCODE", "Code 93", "Patch Code",
"POSTNET","PLANET","RM4SCC", "Australia Post",
"Intelligent Mail (OneCode)"
});
this.cbBarcodeType.SelectedIndex=1;
this.btnScan.Enabled=false;
this.tbResult.ReadOnly=true;
this.tbResult.Multiline=true;
}
privatevoidbtnFile_Click(objectsender, EventArgse)
{
OpenFileDialogopenFileDialog=newOpenFileDialog();
if (openFileDialog.ShowDialog() ==DialogResult.OK)
{
try
{
this.tbFilePath.Text=openFileDialog.FileName;
this.btnScan.Enabled=true;
this.pbImage.Image=Image.FromFile(openFileDialog.FileName);
}
catch (Exception) { }
}
}
privatevoidbtnScan_Click(objectsender, EventArgse)
{
try
{
StringfilePath=this.tbFilePath.Text;
if (String.IsNullOrEmpty(filePath))
thrownewException("File path is null or empty.");
if (!File.Exists(filePath))
thrownewException("File does not exist.");
OptimizeSettingoptions=newOptimizeSetting(this.cbBarcodeType.SelectedIndex);
BarCode[] barcodes=BarcodeReader.ReadBarcode(filePath, options);
// Show scan result in the scan result TextBox.StringBuildersb=newStringBuilder();
if (barcodes.Length>0)
{
sb.Append("Number of Barcode(s) : "+barcodes.Length+"\r\n\r\n");
intindex=1;
foreach (BarCodebarcodeDatainbarcodes)
{
sb.Append(index+". ["+barcodeData.GetTypeName() +"]: "+barcodeData.GetMessage() +"\r\n");
}
}
else
{
sb.Append("No Barcode Detected.");
}
this.tbResult.Text=sb.ToString();
}
catch (Exceptionex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
Go to Form1.cs [Design] in Visual Studio.
Select button btnFile, in Properties window, add Click event handler method btnFile_Click
Select button btnScan, in Properties window, add Click event handler method btnScan_Click
5. Run Windows Forms application
Now we run the Barcode Reader Windows Forms application from Visual Studio. Select a barcode image, scan and view the results.
Conclusion
Now you have learned how to quickly read barcodes from image files in C# Windows applications. You can further customize and optimze the BarcodeLib .NET Barcode Reader C# library to
parse advanved barcode data and improve the recognition speed.