Generate barcode in Linux using a .NET application

Blog category: Barcode.NETLinux

December 22, 2023

This article explains how to create a console .NET application and generate barcode in Ubuntu. For barcode generation is used VintaSoft Barcode .NET SDK.

Here are the steps to complete that task:
  1. Open Ubuntu desktop.

  2. Create a folder, which will store the files of .NET application. Let us create "Generate_Barcode" folder on current user's desktop and proceed to the folder.


  3. Open the console command terminal. This can be done choosing "Open in Terminal" item in context menu or pressing the key combination Ctrl+Alt+T.


  4. Call the command in terminal, which creates a project of new console .NET application:
    dotnet new console --framework net8.0
    



    The created project contains the project file "Generate_Barcode.csproj" and "Program.cs" file, which contains C# code of application. Close terminal.

  5. Open the project file "Generate_Barcode.csproj" in text editor and change the file text to the following text:
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <RootNamespace>ConsoleApp1</RootNamespace>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="SkiaSharp" Version="2.88.6" />
        <PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.6" />
        <PackageReference Include="Vintasoft.Barcode" Version="14.2.0.3" />
        <PackageReference Include="Vintasoft.Barcode.SkiaSharp" Version="14.2.0.3" />
        <PackageReference Include="Vintasoft.Shared" Version="4.1.0.3" />
      </ItemGroup>
    
    </Project>
    



    The changed project references nuget-packages for VintaSoft Barcode .NET SDK (Vintasoft.Shared.dll, Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll).

  6. Open the file "Program.cs" and change its code to the following C# code:
    namespace Generate_Barcode
    {
        class Program
        {
            static void Main(string[] args)
            {
                // register the evaluation license for VintaSoft Barcode .NET SDK
                Vintasoft.Barcode.BarcodeGlobalSettings.Register("%EVAL_LIC_USER_NAME%", "%EVAL_LIC_USER_EMAIL%", "%EVAL_LIC_DATE%", "%EVAL_LIC_REG_CODE%");
    
                // Vintasoft.Barcode.SkiaSharp.dll allows to load bitmap from BMP, DNG, GIF, HEIF, JPEG, PNG, WEBP files using image codecs from SkiaSharp library
                Vintasoft.Barcode.SkiaSharpAssembly.Init();
    
                // generate Code128 barcode as a raster image and save to a PNG file
                GenerateBarcodeAsRasterImage(Vintasoft.Barcode.BarcodeType.Code128, "VintaSoft Barcode .NET SDK", "rasterBarcodeImage.png");
    
                // generate QR barcode as vector image and save to a SVG file
                GenerateBarcodeAsVectorImage(Vintasoft.Barcode.BarcodeType.QR, "VintaSoft Barcode .NET SDK", "vectorBarcode.svg");
            }
    
            /// <summary>
            /// Generates barcode as raster image and saves to an image file.
            /// </summary>
            /// <param name="barcodeType">Barcode type.</param>
            /// <param name="barcodeValue">Barcode value.</param>
            /// <param name="pngFilename">The filename, where barcode image must be saved.</param>
            public static void GenerateBarcodeAsRasterImage(Vintasoft.Barcode.BarcodeType barcodeType, string barcodeValue, string pngFilename)
            {
                // create the barcode writer
                using (Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter())
                {
                    // set barcode writer settings
                    barcodeWriter.Settings.Barcode = barcodeType;
                    barcodeWriter.Settings.Value = barcodeValue;
    
                    // generate barcode as raster image and save to a PNG file
                    barcodeWriter.SaveBarcodeAsImage(pngFilename);
                }
            }
    
            /// <summary>
            /// Generates barcode in vector form and saves to a SVG file.
            /// </summary>
            /// <param name="barcodeType">Barcode type.</param>
            /// <param name="barcodeValue">Barcode value.</param>
            /// <param name="svgFilename">The filename, where SVG file must be saved.</param>
            public static void GenerateBarcodeAsVectorImage(Vintasoft.Barcode.BarcodeType barcodeType, string barcodeValue, string svgFilename)
            {
                // create the barcode writer
                using (Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter())
                {
                    // set barcode writer settings
                    barcodeWriter.Settings.Barcode = barcodeType;
                    barcodeWriter.Settings.Value = barcodeValue;
    
                    // generate SVG file
                    string svgFile = barcodeWriter.GetBarcodeAsSvgFile();
    
                    // save SVG file
                    System.IO.File.WriteAllText(svgFilename, svgFile);
                }
            }
    
        }
    }
    



    The application code will generate Code128 barcode as raster image and QR barcode in vector form, generated barcodes will be saved to files.

  7. Get the code for using evaluation version in Linux using the way described in documentation and insert the obtained code into C# code of "Program.cs" file.


  8. Open terminal and compile the .NET project using the following command:
    dotnet build Generate_Barcode.csproj
    



    Close the terminal.

  9. Go to "bin/Debug/net8.0/" folder.


  10. Open the terminal and run the .NET application using the following command:
    dotnet ./Generate_Barcode.dll
    



    Close the terminal and see generated PNG- and SVG-file.