Write 1D and 2D Barcodes to an Image - C# .NET 6

This tutorial shows how to write barcodes onto an image and save the file in a C# .NET application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how write barcodes onto an image and save the file with the BarcodeEngine and BarcodeWriter classes in a C# .NET application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Write 1D and 2D Barcodes to an Image - .NET tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

This tutorial requires the following NuGet package:

For a complete list of which DLLs are required for specific barcode features, 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:

Add the Barcode Writer Code

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

In the Solution Explorer, open Program.cs 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 the below global BarcodeEngine variable.

C#
static BarcodeEngine barcodeEngine = new BarcodeEngine(); 

Add two new methods called WriteUPCABarcode(RasterImage image) and WriteQRBarcode(RasterImage image). Call them from inside the Main method as shown below.

C#
static void Main(string[] args) 
{ 
   InitLEAD(); 
 
   using (RasterImage image = RasterImage.Create(800, 800, 24, 300, RasterColor.White)) 
   { 
      WriteUPCABarcode(image); 
      WriteQRBarcode(image); 
      SaveImage(image, @"C:\temp\Write1Dand2DBarcodes.png"); 
   } 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

Add the below code to write UPCA and QR barcodes to a blank image and then save that image to file.

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)); 
} 

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.

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 .NET Barcode application that writes 1D and 2D barcodes to a given image using the BarcodeWriter class.

See Also

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

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