Write 1D and 2D Barcodes to an Image - Console C#

This tutorial shows how to create a C# Windows Console application that uses the BarcodeEngine and BarcodeWriter classes to write barcodes onto an image and save the file.

Overview  
Summary This tutorial covers how to use the BarcodeWriter Class in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, and basic loading and saving with LEADTOOLS toolkits by reviewing the Load and Save Images tutorial, before working on the Write 1D and 2D Barcodes to an Image - 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 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 Writer Code

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

Open the Program.cs in the Solution Explorer and add the following statements to the using block at the top of Program.cs:

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

Add two new methods called WriteUPCABarcode(RasterImage image) and WriteQRBarcode(RasterImage image). Call them from inside the Main method. Add the below code to write UPCA and QR barcodes to a blank image and then save that image to file.

Note

This code saves the file to the C:\temp directory. Create the directory if it does not exist, or change the path to a valid folder.


C#
static void Main(string[] args) 
{ 
   SetLicense(); 
 
   using (RasterImage image = RasterImage.Create(2550, 3300, 24, 300, RasterColor.White)) 
   { 
      WriteUPCABarcode(image); 
      WriteQRBarcode(image); 
      SaveImage(image, @"C:\temp\Write1Dand2DBarcodes.png"); 
   } 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 
C#
static void WriteUPCABarcode(RasterImage image) 
{ 
   BarcodeData data = new BarcodeData 
   { 
      Symbology = BarcodeSymbology.UPCA, 
      Value = "01234567890", 
      Bounds = new LeadRect(10, 10, 600, 200) 
   }; 
 
   OneDBarcodeWriteOptions options = new OneDBarcodeWriteOptions 
   { 
      EnableErrorCheck = true, 
      TextPosition = BarcodeOutputTextPosition.Default 
   }; 
 
   barcodeEngine.Writer.WriteBarcode(image, data, options); 
} 
C#
static void WriteQRBarcode(RasterImage image) 
{ 
   QRBarcodeData data = new QRBarcodeData 
   { 
      SymbolModel = QRBarcodeSymbolModel.Model1AutoSize, 
      Symbology = BarcodeSymbology.QR, 
      Value = "QR Data Value", 
 
      Bounds = new LeadRect(10, 250, image.ImageWidth, image.ImageHeight) 
   }; 
 
   QRBarcodeWriteOptions options = new QRBarcodeWriteOptions 
   { 
      GroupNumber = 0, 
      GroupTotal = 0, 
      XModule = 30, 
      ECCLevel = QRBarcodeECCLevel.LevelL, 
      HorizontalAlignment = BarcodeAlignment.Near, 
      VerticalAlignment = BarcodeAlignment.Near 
   }; 
 
   barcodeEngine.Writer.WriteBarcode(image, data, options); 
} 
C#
static void SaveImage(RasterImage image, string outputFilename) 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
      codecs.Save(image, outputFilename, RasterImageFormat.Png, 0); 
   Console.WriteLine(string.Format("The barcodes have been written and saved to {0}", outputFilename)); 
} 

Run the Project

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

If the steps were followed correctly, the application will write the UPCA and QR barcodes to the blank RasterImage and save the image to the specified output file. The resulting RasterImage should look like the following image:

The application runs and writes the specified barcodes

Wrap-up

This tutorial showed how to create a simple console-based Barcode application that writes 1D and 2D barcodes to a given image using the BarcodeWriter class.

See Also

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