Generar código de barras en macOS usando una aplicación .NET
Categoría del blog: Barcode ; .NET ; macOS
25.12.2023
						
						dotnet new console --framework net8.0
						
<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.macOS" 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>
						
						
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);
            }
        }
    }
}
						
						
						dotnet build Generate_Barcode.csproj
						
						dotnet ./Generate_Barcode.dll