How To Read a Barcode From an Image in C#

By FoxLearn 7/18/2024 8:19:24 AM   10.81K
To read barcodes from images in a C# Windows Forms application using the ZXing.Net library, you can follow these steps.

After you finish creating a new Windows Forms Application project. You need to open your form designer, then drag and drop TextBox, Label, Button and PictureBox controls from the Visual Studio Toolbox to your form designer.

You can design a simple user interface that allows you to select an image, then decode the image to barcode as shown below.

c# read barcode from image

Right-clicking on your project, then select Manage Nuget Packages from your Visual Studio.

Next, You need to search and install "ZXing.Net" for your project.

Read barcode from image c# free library

c# zxing.net

ZXing.Net is an open source library that allows you to create or read barcode, qr code...etc.

Open your form designer, then add a click event handler allows you to open a barcode image, then convert the barcode image to text.

C# Read barcode from image

// c# barcode in zxing
private void btnBarcode_Click(object sender, EventArgs e)
{
    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPG|*.jpg" })
    {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            pictureBox.Image = Image.FromFile(ofd.FileName);
            BarcodeReader reader = new BarcodeReader();
            var result = reader.Decode((Bitmap)pictureBox.Image);
            if (result != null)
                txtBarcode.Text = result.ToString();
        }
    }
}

Press F5 to run your project.

You can easily generate a free barcode online by using this tool: Link

Or you can view this post "How to create a barcode image in C#" to know how to geneate a barcode from a string in C#.