Customize Barcode Appearance

This documentation explains how to customize the appearance of barcodes using the Aspose.BarCode Cloud SDK for .NET. It provides details on the available methods, customization options, and examples of usage.

Methods

  • GenerateAsync: Generates a barcode via GET request.
  • GenerateBodyAsync: Generates a barcode via POST request with JSON or XML body.
  • GenerateMultipartAsync: Generates a barcode via POST request with multipart form data.

Customization Options

When generating a barcode, you can customize various aspects of its appearance using the properties of the request classes.

Key Properties:

  • BarcodeType: Specifies the type of barcode to generate (e.g. QR, Aztec, Pdf417, Code39, etc).
  • Data: The data to encode in the barcode.
  • ImageFormat: Specifies the output format of the barcode image (e.g., Png, Jpeg, Svg).
  • ForegroundColor: Color of the barcode bars, supports standard color names or ARGB values (e.g., AliceBlue or #FF000000).
  • BackgroundColor: Background color of the barcode image.
  • TextLocation: Position of the code text relative to the barcode (Below, Above, None).
  • Units: Measurement units for image dimensions (Pixel, Inch, Millimeter).
  • Resolution: Resolution of the image in dpi.
  • ImageHeight and ImageWidth: Dimensions of the barcode image.
  • RotationAngle: Rotation angle of the barcode (e.g., 0, 90, 180).

Examples

Example 1: Generating a Barcode with GET Request

using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Interfaces;
using Aspose.BarCode.Cloud.Sdk.Model;

using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace GenerateSnippets;

internal static class Program
{
    private static Configuration MakeConfiguration()
    {
        var config = new Configuration();

        string? envToken = Environment.GetEnvironmentVariable("TEST_CONFIGURATION_ACCESS_TOKEN");
        if (string.IsNullOrEmpty(envToken))
        {
            config.ClientId = "Client Id from https://dashboard.aspose.cloud/applications";
            config.ClientSecret = "Client Secret from https://dashboard.aspose.cloud/applications";
        }
        else
        {
            config.JwtToken = envToken;
        }

        return config;
    }

    public static async Task Main(string[] args)
    {
        string fileName = Path.GetFullPath(Path.Join("Tests", "test_data",
            "qr.png"
        ));

        GenerateApi generateApi = new GenerateApi(MakeConfiguration());

        var generated = await generateApi.GenerateAsync( barcodeType: EncodeBarcodeType.QR,
            data: "Aspose.BarCode.Cloud",
            imageFormat: BarcodeImageFormat.Png,
            foregroundColor: "Black",
            backgroundColor: "White",
            textLocation: CodeLocation.Below,
            resolution: 300,
            imageHeight: 200,
            imageWidth: 200);

        await using FileStream stream = File.Create(fileName);
        await generated.CopyToAsync(stream);

        Console.WriteLine($"File '{fileName}' generated.");
    }
}

Result Image is: Result image

Example 2: Generating a Barcode with POST Request (JSON Body)

using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Interfaces;
using Aspose.BarCode.Cloud.Sdk.Model;

using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace GenerateSnippets;

internal static class Program
{
    private static Configuration MakeConfiguration()
    {
        var config = new Configuration();

        string? envToken = Environment.GetEnvironmentVariable("TEST_CONFIGURATION_ACCESS_TOKEN");
        if (string.IsNullOrEmpty(envToken))
        {
            config.ClientId = "Client Id from https://dashboard.aspose.cloud/applications";
            config.ClientSecret = "Client Secret from https://dashboard.aspose.cloud/applications";
        }
        else
        {
            config.JwtToken = envToken;
        }

        return config;
    }

    public static async Task Main(string[] args)
    {
        string fileName = Path.GetFullPath(Path.Join("Tests", "test_data",
            "Code39.jpeg"
        ));

        GenerateApi generateApi = new GenerateApi(MakeConfiguration());

        var generateParams = new GenerateParams
        {
            BarcodeType = EncodeBarcodeType.Code39,
            EncodeData = new EncodeData { Data = "Aspose", DataType = EncodeDataType.StringData },
            BarcodeImageParams = new BarcodeImageParams
            {
                ForegroundColor = "#FF0000",
                BackgroundColor = "#FFFF00",
                ImageFormat = BarcodeImageFormat.Jpeg,
                RotationAngle = 90
            }
        };

        var generated = await generateApi.GenerateBodyAsync(generateParams);

        await using FileStream stream = File.Create(fileName);
        await generated.CopyToAsync(stream);

        Console.WriteLine($"File '{fileName}' generated.");
    }
}

Result Image is: Result image

Example 3: Generating a Barcode with POST Request (Multipart Form)

using Aspose.BarCode.Cloud.Sdk.Api;
using Aspose.BarCode.Cloud.Sdk.Interfaces;
using Aspose.BarCode.Cloud.Sdk.Model;

using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace GenerateSnippets;

internal static class Program
{
    private static Configuration MakeConfiguration()
    {
        var config = new Configuration();

        string? envToken = Environment.GetEnvironmentVariable("TEST_CONFIGURATION_ACCESS_TOKEN");
        if (string.IsNullOrEmpty(envToken))
        {
            config.ClientId = "Client Id from https://dashboard.aspose.cloud/applications";
            config.ClientSecret = "Client Secret from https://dashboard.aspose.cloud/applications";
        }
        else
        {
            config.JwtToken = envToken;
        }

        return config;
    }

    public static async Task Main(string[] args)
    {
        string fileName = Path.GetFullPath(Path.Join("Tests", "test_data",
            "Pdf417.svg"
        ));

        GenerateApi generateApi = new GenerateApi(MakeConfiguration());

        var generated = await generateApi.GenerateMultipartAsync(barcodeType: EncodeBarcodeType.Pdf417,
            data: "Aspose.BarCode.Cloud",
            textLocation: CodeLocation.Above,
            imageFormat: BarcodeImageFormat.Svg
            );

        await using FileStream stream = File.Create(fileName);
        await generated.CopyToAsync(stream);

        Console.WriteLine($"File '{fileName}' generated.");
    }
}

Result Image is: Result image