This step-by-step tutorial will guide you through how to build a complete C# console application that analyzes barcodes from an image and outputs the value, type, location and confidence to the terminal
using System;
using System.Drawing.Bitmap;
using Accusoft.BarcodeXpressSdk;
static void Main(string[] args)
{
using (BarcodeXpress barcodeXpress = new BarcodeXpress("."))
using (System.Drawing.Bitmap bitmap = new Bitmap("pathtoyourimage"))
{
// barcodeXpress.Licensing.SetSolutionName("YourSolutionName");
// barcodeXpress.Licensing.SetSolutionKey(1234, 1234, 1234, 1234);
// The call to SetOEMLicenseKey should only be made when using an OEM license key which begins with the value 2.0... If you do not have an OEM key this call should be skipped
// barcodeXpress.Licensing.SetOEMLicenseKey("2.0...");
// BarcodeType.UnknownBarcode is set to the default type for Barcode Xpress, this will detect all 1D barcodes, to detect 2D barcodes you will need to specifically set whichever BarcodeType best suits your project
barcodeXpress.reader.BarcodeTypes = new BarcodeType[] { BarcodeType.UnknownBarcode };
try
{
Result[] results = barcodeXpress.reader.Analyze(bitmap);
Console.WriteLine(results.Length + " barcodes detected\n");
for (int i = 0; i < results.Length; i++)
{
Console.WriteLine("#" + i);
Console.WriteLine("Barcode Type: " + results[i].BarcodeName);
Console.WriteLine("Barcode Value: " + results[i].BarcodeValue);
}
}
catch (BarcodeException bxEx)
{
Console.WriteLine(bxEx.Message);
}
}
}
Open Visual Studio and create a new .NET Framework Console Application (Ensure that this is a .NET Framework Console Application and not a .NET Core Console Application), Barcode Xpress supports .NET Framework 2.0+
Name your project and select a location to save the project
Add a reference to Barcode Xpress .NET by either referencing the Barcode Xpress .NET nuget package or by adding a project reference by right clicking Dependencies and clicking Add Project Reference
Insert using Accusoft.BarcodeXpressSdk; to your code
Inside the Main
method, initialize the barcodexpress
instance and the System.Drawing.Bitmap
instance and point the Bitmap constructor to point to the image you want to read barcodes on
using (BarcodeXpress barcodeXpress = new BarcodeXpress("."))
using (System.Drawing.Bitmap bitmap = new Bitmap("pathtoyourimage"))
{
}
If you're distributing your application and using a runtime license, calls to SetSolutionName
, SetSolutionKey
and SetOEMLicenseKey
will need to be added. If you're evaluating or using a development license this step should be skipped
using (BarcodeXpress barcodeXpress = new BarcodeXpress("."))
using (System.Drawing.Bitmap bitmap = new Bitmap("pathtoyourimage"))
{
barcodeXpress.Licensing.SetSolutionName("YourSolutionName");
barcodeXpress.Licensing.SetSolutionKey(1234, 1234, 1234, 1234);
// The call to SetOEMLicenseKey should only be made when using an OEM license key which begins with the value 2.0... If you do not have an OEM key this call should be skipped
barcodeXpress.Licensing.SetOEMLicenseKey("2.0...");
}
Setup the barcode reader and specify which barcode types you would like to read, see the list of supported barcode types for a list of all supported options. By default this value is set to Unknown which will read all 1D barcodes
// BarcodeType.UnknownBarcode is set to the default type for Barcode Xpress, this will detect all 1D barcodes, to detect 2D barcodes you will need to specifically set whichever BarcodeType best suits your project
barcodeXpress.reader.BarcodeTypes = new BarcodeType[] { BarcodeType.UnknownBarcode };
Call Analyze
in a try-catch block to catch any exceptions that Barcode Xpress throws
try
{
Result[] results = barcodeXpress.reader.Analyze(bitmap);
}
catch (BarcodeException bxEx)
{
Console.WriteLine(bxEx.Message);
}
Add a useful print message
Console.WriteLine(results.Length + " barcodes found\n");
for (int i = 0; i < results.Length; i++)
{
Console.WriteLine("#" + i);
Console.WriteLine("Barcode Type: " + results[i].BarcodeName);
Console.WriteLine("Barcode Value: " + results[i].BarcodeValue);
}
Run the sample and view your results