This tutorial shows how to create a C# .NET Core 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# .NET Core application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET Core Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
Try it in another language |
|
Before working on the Write 1D and 2D Barcodes to an Image - .NET Core tutorial, get familiar with the basic steps of creating a project by reviewing the Add References and Set a License Tutorial tutorial.
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:
Leadtools.Barcode
For a complete list of which DLLs are required for specific barcode features, refer to Barcode Support.
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
How to properly add LEADTOOLS NuGet references is covered in the Add References and Set a License tutorial.
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
:
// Using block at the top
using System;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Barcode;
Add the below global BarcodeEngine
variable.
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.
static void Main(string[] args)
{
if (!SetLicense())
{
Console.WriteLine("Error setting license");
return;
}
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.
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));
}
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 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:
This tutorial showed how to create a simple .NET Core Barcode application that writes 1D and 2D barcodes to a given image using the BarcodeWriter class.