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 saves 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 |
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.
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:
Leadtools.Barcode
If using local DLL references, the following DLLs are needed:
The local DLLs are installed at <INSTALL_DIR>\LEADTOOLS 20\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.Tif.dll
Leadtools.Codecs.Fax.dll
Leadtools.Barcode.dll
Leadtools.Barcode.OneD.dll
Leadtools.Barcode.QrWrite.dll
For a complete list of which Codec DLLs are required for specific formats, refer to Files To Be Included With Your Application.
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.
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
:
// 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.
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);
}
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);
}
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);
}
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 by pressing F5, or by selecting Debug, then 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:
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.