Set Barcode Size

This documentation provides a guide on how to set barcode sizes using in the Aspose.BarCode Cloud SDK for .NET. This guide includes examples and descriptions for configuring the height, width, and resolution of barcode images.

Overview

The IGenerateApi interface offers several methods for generating barcodes through different types of HTTP requests:

  • 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.

Key Parameters for Setting Barcode Size

To control the size of a barcode, the following properties in the request objects can be configured:

  • ImageHeight: Specifies the height of the barcode image.
  • ImageWidth: Specifies the width of the barcode image.
  • Resolution: Defines the resolution (DPI) of the barcode image.
  • Units: Defines the measurement units (e.g., pixel, inch, millimeter).

Example Usage

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());

        Stream generated = await generateApi.GenerateAsync(EncodeBarcodeType.QR, "Aspose.BarCode.Cloud",
                        imageHeight: 200,
                        imageWidth: 200,
                        resolution: 300,
                        units: GraphicsUnit.Pixel
            );

        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",
            "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
            {
                ImageHeight = 2,
                ImageWidth = 3,
                Resolution = 96,
                Units = GraphicsUnit.Inch
            }
        };

        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: Generating a Barcode with POST Request (Form URL Encoded)

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

        GenerateApi generateApi = new GenerateApi(MakeConfiguration());


        Stream generated = await generateApi.GenerateMultipartAsync(EncodeBarcodeType.Aztec, "Aspose.BarCode.Cloud",
                    imageHeight: 200,
                    imageWidth: 200,
                    resolution: 150,
                    units: GraphicsUnit.Point);

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

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

Result Image is: Result image

Conclusion

Setting the correct barcode dimensions ensures optimal readability and compatibility with various scanning devices. With Aspose.BarCode Cloud SDK for .NET, you can customize the barcode’s height, width, and resolution to meet specific requirements, whether for product labeling, logistics, or QR code marketing.

Proper barcode sizing improves scanning accuracy and ensures a better user experience. By setting the right dimensions using Aspose.BarCode Cloud SDK for .NET, you can ensure your barcodes are perfectly tailored for their intended use.