Set Barcode Colorscheme

This documentation provides a comprehensive guide on how to set the colorscheme for barcodes using the Aspose.BarCode Cloud SDK for .NET. The API allows customization of barcode appearance, including foreground and background colors, ensuring your barcodes match your design needs.

Overview

The IGenerateApi interface includes methods to generate barcodes using different request formats:

  • GenerateAsync: GET request with parameters in route and query string.
  • GenerateBodyAsync: POST request with parameters in the body (JSON or XML).
  • GenerateMultipartAsync: POST request with parameters in multipart form.

Colorscheme Parameters

The ForegroundColor and BackgroundColor properties enable you to define the colors for the barcode:

  • ForegroundColor: Specifies the color of the bars and text.
  • BackgroundColor: Specifies the background color of the barcode image.

Both properties accept standard color names or ARGB values (e.g., AliceBlue or #FF000000).

Examples

Example 1: Setting Colors Using 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());

        Stream generated = await generateApi.GenerateAsync(EncodeBarcodeType.QR,
            "https://products.aspose.cloud/barcode/family/",
            foregroundColor: "DarkBlue",
            backgroundColor: "LightGray",
            imageFormat: BarcodeImageFormat.Png);

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

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

Result Image is: Result image

Example 2: Setting Colors Using POST Request with 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",
            "Pdf417.png"
        ));

        GenerateApi generateApi = new GenerateApi(MakeConfiguration());

        var generateParams = new GenerateParams
        {
            BarcodeType = EncodeBarcodeType.Pdf417,
            EncodeData = new EncodeData { Data = "Aspose.BarCode.Cloud", DataType = EncodeDataType.StringData },
            BarcodeImageParams = new BarcodeImageParams
            {
                ForegroundColor = "#FF5733",
                BackgroundColor = "#FFFFFF",
                ImageFormat = BarcodeImageFormat.Jpeg
            }
        };

        Stream 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: Setting Colors Using Multipart Form POST

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.png"
        ));

        GenerateApi generateApi = new GenerateApi(MakeConfiguration());

        Stream generated = await generateApi.GenerateMultipartAsync(EncodeBarcodeType.Code39, "Aspose",
                        foregroundColor: "Green",
                        backgroundColor: "Yellow",
                        imageFormat: BarcodeImageFormat.Gif);

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

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

Result Image is: Result image

Conclusion

With IGenerateApi in Aspose.BarCode Cloud SDK for .NET, you can easily generate barcodes with customized colors. Whether you prefer GET, POST with JSON, or multipart form POST, the flexibility of this API ensures seamless integration and customization of barcodes in your applications.