Generate barcode image in console .NET application
In This Topic
This tutorial shows how to create a blank console .NET application in Visual Studio .NET 2022, generate barcode image and recognize barcode in generated image in console .NET application.
Here are steps, which must be done:
-
Create a blank console .NET application.
Open Visual Studio .NET 2022 and create a new project, of "Console application" type:
Configure the project to use .NET 8.0:
-
Add references to the Vintasoft assemblies to the console .NET application.
-
Add references to the Vintasoft assemblies from the installation of the SDK evaluation version.
If the evaluation version of VintaSoft Barcode .NET SDK is not installed on your Windows computer, please download the evaluation version of VintaSoft Barcode .NET SDK from here and install the evaluation version of VintaSoft Barcode .NET SDK to your Windows computer.
Add references to the Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll and Vintasoft.Shared.dll assemblies from folder "<InstallPath>\VintaSoft Barcode .NET 15.2\Bin\DotNet8\AnyCPU\" to your console .NET application:
- In Solution Explorer, locate the project.
- Right-click the "Dependencies" node. => Context menu will appear.
- Select "Add Project Reference..." menu in context menu. => The "Reference Manager" dialog will appear.
- In the "Reference Manager" dialog navigate to the "Browse" tab, click the "Browse..." button. => File selection dialog will appear.
- Select the desired .NET assembly files (Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll and Vintasoft.Shared.dll) in folder "<InstallPath>\VintaSoft Barcode .NET 15.2\Bin\DotNet8\AnyCPU\" and click "Add" button => File selection dialog will be closed.
- Click "OK" button. => The "Reference Manager" dialog will be closed. => References to Vintasoft assemblies will be added to the console .NET project.
-
Add references to the Vintasoft assemblies from Vintasoft nuget-packages.
Add references to the Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll and Vintasoft.Shared.dll assemblies from Vintasoft nuget-packages to your console .NET application:
- In Solution Explorer, locate the project.
- Right-click the "Dependencies" node. => Context menu will appear.
- Select "Manage Nuget Packages..." menu in context menu. => The "Nuget Package Manager" dialog will appear.
- In the "Nuget Package Manager" dialog navigate to the "Browse" tab, enter "vintasoft" text in search text field. => Vintasoft nuget-packages will be shown in the package list.
- Select "Vintasoft.Shared" package in the package list and click the "Install" button. => Reference to the "Vintasoft.Shared" nuget-package will be added to the console .NET project.
- Select "Vintasoft.Barcode" package in the package list and click the "Install" button. => Reference to the "Vintasoft.Barcode" nuget-package will be added to the console .NET project.
- Select "Vintasoft.Barcode.SkiaSharp" package in the package list and click the "Install" button. => Reference to the "Vintasoft.Barcode.SkiaSharp" nuget-package will be added to the console .NET project.
- Close the "Nuget Package Manager" dialog.
Comment: Reference to Vintasoft.Barcode.SkiaSharp.dll assembly is necessary only if SDK should draw text value of barcode on barcode image. Vintasoft.Barcode.ImageSharp.dll can be used instead of Vintasoft.Barcode.SkiaSharp.dll assembly.
Add references to the SkiaSharp assembly from SkiaSharp nuget-package to your console .NET application:
- In Solution Explorer, locate the project.
- Right-click the "Dependencies" node. => Context menu will appear.
- Select "Manage Nuget Packages..." menu in context menu. => The "Nuget Package Manager" dialog will appear.
- In the "Nuget Package Manager" dialog navigate to the "Browse" tab, enter "skiasharp" text in search text field. => SkiaSharp nuget-packages will be shown in the package list.
- Select "SkiaSharp" package version 3.119.0 in the package list and click the "Install" button. => Reference to the "SkiaSharp" nuget-package will be added to the console .NET project.
- Close the "Nuget Package Manager" dialog.
-
Add C# code that registers the evaluation version for VintaSoft Barcode .NET SDK.
Get C# code for using evaluation version in Windows using the way described in documentation and insert the obtained code in the Main method of "Program.cs" file.
Here is C# code that registers the evaluation version for VintaSoft Barcode .NET SDK:
static void Main(string[] args)
{
// register the evaluation license for VintaSoft Barcode .NET SDK
Vintasoft.Barcode.BarcodeGlobalSettings.Register("EVAL_USER", "EVAL_USER_EMAIL", "EVAL_END_DATE", "EVAL_REG_CODE");
}
-
Add C# code that generates barcode image and recognizes barcode in generated image.
Open file "Program.cs" and add C# code, which generates barcode image and recognizes barcode in generated image, to the Main method.
Here is C# code that generates barcode image and recognizes barcode in generated image:
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
// register the evaluation license for VintaSoft Barcode .NET SDK
Vintasoft.Barcode.BarcodeGlobalSettings.Register("EVAL_USER", "EVAL_USER_EMAIL", "EVAL_END_DATE", "EVAL_REG_CODE");
// create the barcode writer
using (Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter())
{
// specify that barcode writer must generate DataMatrix barcode
barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.DataMatrix;
// specify value for DataMatrix barcode
barcodeWriter.Settings.Value = "12345";
// create memory stream that will store generated barcode image as PNG file
using (MemoryStream stream = new MemoryStream())
{
// generate barcode image and save as PNG file to the memory stream
barcodeWriter.SaveBarcodeAsImage(stream, "png");
stream.Position = 0;
// create barcode reader
using (Vintasoft.Barcode.BarcodeReader barcodeReader = new Vintasoft.Barcode.BarcodeReader())
{
// specify that barcode reader must recognize DataMatrix barcodes
barcodeReader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.DataMatrix;
// recognize barcodes in PNG file that is stored in memory stream
Vintasoft.Barcode.IBarcodeInfo[] barcodeInfos = barcodeReader.ReadBarcodes(stream);
// if barcode is not recognized
if (barcodeInfos.Length == 0)
{
Console.WriteLine("Barcode Reader", "Barcode is not recognized.", "OK");
}
// if barcode is recognized
else
{
// display information about recognized DataMatrix barcode
Console.WriteLine(
string.Format("Barcode is recognized: Type='{0}', Value='{1}'",
barcodeInfos[0].BarcodeType,
barcodeInfos[0].Value));
}
}
}
}
}
}
}
-
Run console .NET application in Visual Studio and see the result.