Read and Write Bytes in a Barcode - Console C#

This tutorial shows how to create a C# Windows Console application that creates a barcode by encoding raw binary data, then how to decode that data from the barcode using the LEADTOOLS SDK. This can be useful when using characters from extended character sets.

Overview  
Summary This tutorial covers how to use LEADTOOLS Barcode SDK technology in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform Windows Console C# Application
IDE Visual Studio 2017, 2019
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 tutorial, before working on the Read and Write Bytes in a Barcode - Console C# tutorial.

Create the Project and Add LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the following necessary LEADTOOLS references.

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 package:

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

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

This tutorial uses QR Barcodes. For a complete list of which DLLs are required for other barcode symbologies, refer to Barcode 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 Encoding and Decoding Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top.

C#
// Using block at the top 
using System; 
using System.Text; 
using System.IO; 
using Leadtools; 
using Leadtools.Barcode; 

Add two new methods called CreateBarcode(string data) that returns a RasterImage and ReadBarcode(RasterImage barcodeImage) that returns a string value. Add the below code to write and read bytes in a barcode.

C#
// Add these two methods 
private static RasterImage CreateBarcode(string data) 
{ 
   byte[] chars = Encoding.Unicode.GetBytes(data); 
 
   RasterImage image = RasterImage.Create(2200, 3300, 24, 300, RasterColor.White); 
 
   BarcodeEngine eng = new BarcodeEngine(); 
   QRBarcodeData qrBarcodeData = new QRBarcodeData(); 
 
   qrBarcodeData.SetData(chars); 
   eng.Writer.WriteBarcode(image, qrBarcodeData, null); 
 
   return image; 
} 
 
private static string ReadBarcode(RasterImage barcodeImage) 
{ 
   BarcodeEngine eng = new BarcodeEngine(); 
   BarcodeData[] data = eng.Reader.ReadBarcodes(barcodeImage, LeadRect.Empty, 0, eng.Reader.GetAvailableSymbologies()); 
 
   if (data.Length > 0) 
   { 
      byte[] chars = data[0].GetData(); 
      string value = Encoding.Unicode.GetString(chars); 
 
      return value; 
   } 
 
   return string.Empty; 
} 

Add the below code inside the Main method to call the CreateBarcode method and the ReadBarcode method.

C#
static void Main(string[] args) 
{ 
   SetLicense(); 
 
   // Create barcode and write barcode to image 
   RasterImage barcodeImage = CreateBarcode("Unicode-encoded data"); 
 
   // Read barcode on image and display to console 
   string decodedText = ReadBarcode(barcodeImage); 
 
   // Console output 
   Console.WriteLine(decodedText); 
} 

Run the Project

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

If the steps were followed correctly, the application runs and creates a new barcode using raw byte data, then decode that barcode and converts the raw byte data back to readable text, which is then displayed on the command line.

Decoded barcode data

Wrap-up

This tutorial showed how to create a barcode from raw byte data, then decode that barcode back into the raw byte data it was created from. This raw byte data can be either binary information or a string converted from a different character set. Also it covered how to use the BarcodeEngine and BarcodeData classes.

See Also

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

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