Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, viewers learn how to implement a barcode scanner in a C# Windows Form application using Iron Barcode. The tutorial is conducted in Visual Studio 2022 and begins with setting up the necessary components, including an image box for displaying barcode images, buttons for browsing and scanning, and a text box for displaying the scanned barcode text. The process involves using the NuGet Package Manager to install Iron Barcode and ensuring both the software and library are updated to their latest versions for optimal performance.
The Form.cs
file plays a crucial role, where a string variable stores the path of the barcode image file. The tutorial demonstrates setting up an open file dialog in the browse button function to select an image file, filtering for common image types. Once a file is chosen, it's loaded into the picture box, and its path is stored.
In the scan button function, the tutorial highlights using Iron Barcode's read method to extract the barcode information from the image. The resulting text is displayed in the text box. The tutorial concludes with a demonstration of the project in action, showing the selection of a barcode image and the display of its text output in the application.
C# Code Sample
using System;
using System.Windows.Forms;
using IronBarCode; // Import the IronBarcode library
namespace BarcodeScannerApp
{
public partial class Form1 : Form
{
// Variable to store the path of the barcode image
private string imagePath;
public Form1()
{
InitializeComponent();
}
// Event handler for the Browse button click event
private void btnBrowse_Click(object sender, EventArgs e)
{
// Create and configure an Open File Dialog
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp", // Allow only common image formats
Title = "Select a Barcode Image"
};
// Show the file dialog and check if the user selected a file
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Store the selected file path
imagePath = openFileDialog.FileName;
// Load the image into the PictureBox
pictureBox.Image = System.Drawing.Image.FromFile(imagePath);
}
}
// Event handler for the Scan button click event
private void btnScan_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(imagePath))
{
try
{
// Read the barcode from the image using Iron Barcode
BarcodeResult result = BarcodeReader.Read(imagePath);
// Display the barcode text in the TextBox
txtOutput.Text = result.Text;
}
catch (Exception ex)
{
// Handle exceptions and display an error message
MessageBox.Show("Error reading barcode: " + ex.Message);
}
}
else
{
// Inform the user to select an image if no path is set
MessageBox.Show("Please select an image first.");
}
}
}
}
using System;
using System.Windows.Forms;
using IronBarCode; // Import the IronBarcode library
namespace BarcodeScannerApp
{
public partial class Form1 : Form
{
// Variable to store the path of the barcode image
private string imagePath;
public Form1()
{
InitializeComponent();
}
// Event handler for the Browse button click event
private void btnBrowse_Click(object sender, EventArgs e)
{
// Create and configure an Open File Dialog
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp", // Allow only common image formats
Title = "Select a Barcode Image"
};
// Show the file dialog and check if the user selected a file
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Store the selected file path
imagePath = openFileDialog.FileName;
// Load the image into the PictureBox
pictureBox.Image = System.Drawing.Image.FromFile(imagePath);
}
}
// Event handler for the Scan button click event
private void btnScan_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(imagePath))
{
try
{
// Read the barcode from the image using Iron Barcode
BarcodeResult result = BarcodeReader.Read(imagePath);
// Display the barcode text in the TextBox
txtOutput.Text = result.Text;
}
catch (Exception ex)
{
// Handle exceptions and display an error message
MessageBox.Show("Error reading barcode: " + ex.Message);
}
}
else
{
// Inform the user to select an image if no path is set
MessageBox.Show("Please select an image first.");
}
}
}
}
Imports System
Imports System.Windows.Forms
Imports IronBarCode ' Import the IronBarcode library
Namespace BarcodeScannerApp
Partial Public Class Form1
Inherits Form
' Variable to store the path of the barcode image
Private imagePath As String
Public Sub New()
InitializeComponent()
End Sub
' Event handler for the Browse button click event
Private Sub btnBrowse_Click(ByVal sender As Object, ByVal e As EventArgs)
' Create and configure an Open File Dialog
Dim openFileDialog As New OpenFileDialog With {
.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp",
.Title = "Select a Barcode Image"
}
' Show the file dialog and check if the user selected a file
If openFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
' Store the selected file path
imagePath = openFileDialog.FileName
' Load the image into the PictureBox
pictureBox.Image = System.Drawing.Image.FromFile(imagePath)
End If
End Sub
' Event handler for the Scan button click event
Private Sub btnScan_Click(ByVal sender As Object, ByVal e As EventArgs)
If Not String.IsNullOrWhiteSpace(imagePath) Then
Try
' Read the barcode from the image using Iron Barcode
Dim result As BarcodeResult = BarcodeReader.Read(imagePath)
' Display the barcode text in the TextBox
txtOutput.Text = result.Text
Catch ex As Exception
' Handle exceptions and display an error message
MessageBox.Show("Error reading barcode: " & ex.Message)
End Try
Else
' Inform the user to select an image if no path is set
MessageBox.Show("Please select an image first.")
End If
End Sub
End Class
End Namespace
The video encourages viewers to subscribe for more tutorials and offers a 30-day trial license to experience Iron Software's full capabilities.
Further Reading: How to use a Barcode Scanner in a C# Windows Application