Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
// Title: Adjust Postnet barcode bar height while preserving automatic width
// Description: Demonstrates setting the BarHeight of a Postnet barcode to 40 points, letting the library compute the optimal width automatically.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, focusing on barcode parameter customization. It showcases the use of BarcodeGenerator, EncodeTypes, and the BarHeight property to modify visual dimensions. Developers often need to adjust size attributes while relying on automatic layout calculations for consistent rendering across formats.
// Title: Generate a Postnet barcode with custom bar height
// Description: Demonstrates how to set the BarHeight of a Postnet barcode to 40 points while allowing the library to calculate the optimal width automatically.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, focusing on symbology-specific parameter adjustments. It showcases the use of BarcodeGenerator, EncodeTypes, and the Parameters.Barcode properties to customize visual aspects of a barcode. Developers often need to modify dimensions such as bar height or module size for specific printing or scanning requirements, and this snippet illustrates the typical approach for Postnet barcodes.
// Prompt: Adjust the BarHeight of a Postnet barcode to 40 points while keeping automatic width calculation.
// Tags: postnet, barcode, barheight, size, generation, png, aspose.barcode
// Tags: postnet, barcode, barheight, dimension, aspnet, aspose.barcode, generation, png

using System;
using Aspose.BarCode;
using Aspose.BarCode.Generation;

/// <summary>
/// Generates a Postnet barcode with a custom bar height while allowing automatic width calculation.
/// Example program that creates a Postnet barcode with a custom bar height.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the example. Creates a Postnet barcode, sets its bar height, and saves it as a PNG image.
/// Entry point. Generates the barcode and saves it as a PNG file.
/// </summary>
static void Main()
{
// Initialize a barcode generator for the Postnet symbology with a sample ZIP code.
using (var generator = new BarcodeGenerator(EncodeTypes.Postnet, "12345"))
// Initialize a barcode generator for the Postnet symbology
using (var generator = new BarcodeGenerator(EncodeTypes.Postnet))
{
// Set the bar height to 40 points; width remains automatically calculated.
// Set the postal code to be encoded
generator.CodeText = "12345";

// Set the bar height to 40 points; width will be calculated automatically
generator.Parameters.Barcode.BarHeight.Point = 40f;

// Save the generated barcode image to a PNG file.
// Save the generated barcode image as PNG
generator.Save("postnet.png");
}

// Inform the user that the barcode has been generated.
// Inform the user that the barcode has been created
Console.WriteLine("Postnet barcode generated: postnet.png");
}
}
Original file line number Diff line number Diff line change
@@ -1,78 +1,57 @@
// Title: Generate Postnet barcode image and return as byte array via simulated REST endpoint
// Description: Demonstrates creating a Postnet postal barcode from input data and returning the PNG image as a byte array, suitable for a REST API response.
// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to use BarcodeGenerator with EncodeTypes.Postnet, configure image parameters, and obtain a bitmap image. Developers building web services that need to produce barcode images for mailing or shipping can reference this pattern for creating PNG byte arrays to embed in JSON responses or files.
// Title: Generate a postal barcode image and return it as a byte array
// Description: Demonstrates creating a Postnet barcode from input data and retrieving the PNG image as a byte array, suitable for returning from a REST API.
// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to use BarcodeGenerator with EncodeTypes.Postnet to produce barcode images. Developers often need to embed barcode creation in web services, returning image streams for client consumption. Key classes include BarcodeGenerator, EncodeTypes, and BarCodeImageFormat, which are commonly used for on‑the‑fly barcode rendering in ASP.NET Core or other API frameworks.
// Prompt: Build a REST API endpoint that receives postal barcode data and returns the image as a byte array.
// Tags: postnet, barcode generation, image output, png, byte array, aspose.barcode, rest api
// Tags: postnet, barcode generation, image output, png, byte array, aspose.barcode, aspnet core

using System;
using System.IO;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.Drawing;
using Aspose.Drawing.Imaging;

/// <summary>
/// Provides a simulated REST API endpoint that generates a Postnet barcode image from postal data.
/// Demonstrates generating a Postnet postal barcode and obtaining the image as a byte array.
/// </summary>
class Program
{
/// <summary>
/// Simulated REST endpoint: receives postal barcode data and returns image bytes.
/// Entry point of the example. Simulates receiving barcode data, generates the barcode image, and writes the size to console.
/// </summary>
/// <param name="postalData">The postal data to encode (e.g., ZIP code).</param>
/// <returns>PNG image bytes representing the generated barcode.</returns>
static byte[] GeneratePostalBarcode(string postalData)
static void Main()
{
// Validate input
if (string.IsNullOrWhiteSpace(postalData))
throw new ArgumentException("Postal data must be provided.", nameof(postalData));
// Simulate receiving postal barcode data (e.g., Postnet code)
string postalData = "12345";

// Create a barcode generator for Postnet (postal) symbology
using (var generator = new BarcodeGenerator(EncodeTypes.Postnet, postalData))
{
// Optional: adjust image size or resolution if needed
generator.Parameters.AutoSizeMode = AutoSizeMode.Interpolation;
generator.Parameters.ImageWidth.Point = 300f;
generator.Parameters.ImageHeight.Point = 150f;
generator.Parameters.Resolution = 96;
// Generate barcode image as a byte array
byte[] imageBytes = GeneratePostalBarcode(postalData);

// Generate the barcode image as a Bitmap
using (Bitmap bitmap = generator.GenerateBarCodeImage())
{
// Save the bitmap to a memory stream in PNG format and return the byte array
using (var ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
}
}
// Output the size of the generated image
Console.WriteLine($"Generated barcode image size: {imageBytes.Length} bytes");
}

/// <summary>
/// Entry point for the console demonstration. Accepts postal data via command‑line argument,
/// generates the barcode, and writes the Base64 representation to the console.
/// </summary>
/// <param name="args">Command‑line arguments; first argument is optional postal data.</param>
static void Main(string[] args)
// Generates a postal barcode (Postnet) image and returns it as a byte array (PNG format)
static byte[] GeneratePostalBarcode(string codeText)
{
// In a real REST scenario the postal data would come from the request body.
// Here we use a sample value or a command‑line argument for demonstration.
string postalData = args.Length > 0 ? args[0] : "12345";
// Validate input
if (string.IsNullOrEmpty(codeText))
throw new ArgumentException("Code text must not be null or empty.", nameof(codeText));

try
// Create a memory stream to hold the image data
using (var memoryStream = new MemoryStream())
{
// Generate barcode image bytes
byte[] imageBytes = GeneratePostalBarcode(postalData);
// Initialize the barcode generator for Postnet symbology
using (var generator = new BarcodeGenerator(EncodeTypes.Postnet, codeText))
{
// Optional: adjust barcode parameters if needed
// e.g., generator.Parameters.Barcode.XDimension.Point = 2f;

// Output the result as a Base64 string (simulating a JSON response body)
string base64 = Convert.ToBase64String(imageBytes);
Console.WriteLine(base64);
}
catch (Exception ex)
{
// Write error details to the error stream
Console.Error.WriteLine($"Error: {ex.Message}");
// Save the barcode image to the memory stream in PNG format
generator.Save(memoryStream, BarCodeImageFormat.Png);
}

// Return the image bytes from the memory stream
return memoryStream.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
// Title: Generate Code128 Barcode with Custom Colors and Save as PNG
// Description: Demonstrates how to set foreground and background colors for a Code128 barcode and export it as a PNG image using Aspose.BarCode.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, illustrating how to customize visual appearance such as bar and background colors. It uses the BarcodeGenerator class and related Parameter objects to configure colors before saving. Developers often need to match branding or UI themes when generating barcodes, and this pattern shows the typical steps for color customization and image export.
// Title: Generate Code128 barcode with custom colors and PNG output
// Description: Demonstrates how to generate a Code128 barcode, apply specific bar and background colors, and export the result as a PNG image using Aspose.BarCode.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, illustrating how to customize visual appearance (foreground and background colors) of generated barcodes. It showcases key API classes such as BarcodeGenerator, EncodeTypes, and BarCodeImageFormat, which developers commonly use to create, style, and save barcodes in various image formats for integration into web, desktop, or mobile applications.
// Prompt: Configure barcode generation to use a specific color palette for bars and background, exporting as PNG.
// Tags: code128, barcode generation, png, color palette, aspose.barcode
// Tags: barcode, symbology, generation, color, png, aspose.barcode, code128

using System;
using System.IO;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.Drawing;

/// <summary>
/// Example program that creates a Code128 barcode with custom bar and background colors
/// and saves it as a PNG image.
/// Example program that creates a Code128 barcode with custom colors and saves it as a PNG file.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Accepts optional command‑line arguments for the barcode text and output file path.
/// </summary>
static void Main()
/// <param name="args">Command‑line arguments: [0] = barcode text, [1] = output file path.</param>
static void Main(string[] args)
{
// Define the output file path for the generated PNG image
string outputPath = "barcode.png";
// Determine the barcode text: use first argument if provided, otherwise default to "Sample123".
string codeText = args.Length > 0 ? args[0] : "Sample123";

// Initialize a BarcodeGenerator for Code128 symbology with sample data
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
// Determine the output file path: use second argument if provided, otherwise default to "barcode.png".
string outputPath = args.Length > 1 ? args[1] : "barcode.png";

// Initialize a BarcodeGenerator for the Code128 symbology with the specified text.
using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, codeText))
{
// Set the foreground (bar) color to red
generator.Parameters.Barcode.BarColor = Color.Red;
// Set the foreground (bar) color to blue.
generator.Parameters.Barcode.BarColor = Color.Blue;

// Set the background color of the image to light gray
// Set the background color of the image to light gray.
generator.Parameters.BackColor = Color.LightGray;

// Save the configured barcode as a PNG file at the specified path
// Save the generated barcode as a PNG file at the specified location.
generator.Save(outputPath, BarCodeImageFormat.Png);
}

// Inform the user where the barcode image has been saved
Console.WriteLine($"Barcode image saved to: {Path.GetFullPath(outputPath)}");
// Inform the user where the barcode image has been saved.
Console.WriteLine($"Barcode image saved to: {outputPath}");
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Title: Generate RM4SCC barcode with custom XDimension and export to byte array
// Description: Demonstrates creating an Aspose.BarCode generator for the RM4SCC symbology, customizing the XDimension, and exporting the resulting PNG image to a byte array.
// Category-Description: This example belongs to the barcode generation category of Aspose.BarCode, illustrating how to configure barcode parameters such as XDimension and retrieve the image as a byte array. It showcases the use of BarcodeGenerator, EncodeTypes, and image handling classes, which are common tasks for developers needing programmatic barcode creation for documents, labels, or web services.
// Title: Generate RM4SCC Barcode and Export as PNG Byte Array
// Description: This example creates an RM4SCC barcode, customizes its XDimension, and returns the barcode image as a PNG byte array.
// Category-Description: Demonstrates Aspose.BarCode generation for the RM4SCC symbology using BarcodeGenerator. Shows how to configure barcode parameters (e.g., XDimension), render the barcode to an Aspose.Drawing.Bitmap, and retrieve the image as a byte array. Ideal for developers needing to embed barcodes in memory, send them over networks, or store them without writing to disk.
// Prompt: Create a barcode generator instance for RM4SCC, set custom XDimension, and export image to a byte array.
// Tags: rm4scc, barcode generation, xdimension, byte array, png, aspnet, aspose.barcode, image export
// Tags: rm4scc, barcode, generation, xdimension, png, byte-array, aspose.barcode, aspose.drawing

using System;
using System.IO;
Expand All @@ -11,44 +11,47 @@
using Aspose.Drawing;
using Aspose.Drawing.Imaging;

namespace BarcodeExample
/// <summary>
/// Demonstrates generating an RM4SCC barcode, customizing its XDimension, and exporting the image to a PNG byte array.
/// </summary>
class Program
{
/// <summary>
/// Provides an example of generating an RM4SCC barcode, customizing its XDimension,
/// and exporting the resulting image to a byte array.
/// Entry point. Generates the barcode and writes the byte array length to the console.
/// </summary>
class Program
static void Main()
{
/// <summary>
/// Entry point of the example. Creates a barcode, sets a custom module width,
/// and writes the image size to the console.
/// </summary>
static void Main()
{
// Initialize a barcode generator for the RM4SCC symbology
using (var generator = new BarcodeGenerator(EncodeTypes.RM4SCC))
{
// Define the data to encode in the barcode
generator.CodeText = "A1234567890";
// Generate the barcode and obtain the image as a byte array
byte[] barcodeBytes = GenerateRm4sccBarcode();

// Set a custom XDimension (module width) measured in points
generator.Parameters.Barcode.XDimension.Point = 2f;
// Output the size of the generated byte array to verify success
Console.WriteLine($"Generated barcode image byte array length: {barcodeBytes.Length}");
}

// Generate the barcode image as a Bitmap object
using (Bitmap bitmap = generator.GenerateBarCodeImage())
{
// Prepare a memory stream to hold the PNG-encoded image
using (var memoryStream = new MemoryStream())
{
// Save the bitmap to the stream in PNG format
bitmap.Save(memoryStream, ImageFormat.Png);
/// <summary>
/// Creates a BarcodeGenerator for RM4SCC, sets a custom XDimension, and returns the barcode image as a PNG byte array.
/// </summary>
/// <returns>Byte array containing the PNG representation of the generated barcode.</returns>
static byte[] GenerateRm4sccBarcode()
{
// Initialize a BarcodeGenerator with RM4SCC symbology
using (var generator = new BarcodeGenerator(EncodeTypes.RM4SCC))
{
// Sample code text for RM4SCC (8 characters: 2 letters + 6 digits)
generator.CodeText = "AB123456";

// Retrieve the image bytes from the stream
byte[] barcodeBytes = memoryStream.ToArray();
// Set a custom XDimension (module size) in points
generator.Parameters.Barcode.XDimension.Point = 2.5f;

// Output the length of the generated byte array for verification
Console.WriteLine($"Generated barcode byte array length: {barcodeBytes.Length}");
}
// Generate the barcode image as an Aspose.Drawing.Bitmap
using (Bitmap bitmap = generator.GenerateBarCodeImage())
{
// Save the bitmap to a memory stream in PNG format
using (var memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, ImageFormat.Png);
// Return the image data as a byte array
return memoryStream.ToArray();
}
}
}
Expand Down
Loading
Loading