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,49 +1,61 @@
// Title: High‑Resolution Barcode Generation for Label Printing
// Description: Demonstrates configuring Aspose.BarCode generator to create a high‑resolution PNG suitable for printing labels.
// Category-Description: This example belongs to the Aspose.BarCode image generation category, illustrating how to set resolution, image size, module dimensions, colors, and text options using BarcodeGenerator and related parameter classes. Developers often need to produce crisp barcodes for packaging, shipping labels, or product tags, and this snippet shows the typical API usage for such scenarios.
// Prompt: Adjust generator settings to produce a barcode image suitable for high‑resolution printing on labels.
// Tags: code128, highresolution, png, barcode generation, aspnet, aspose.barcode, image parameters

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

namespace BarcodeHighResolution
/// <summary>
/// Generates a high‑resolution Code128 barcode image suitable for label printing.
/// </summary>
class Program
{
/// <summary>
/// Demonstrates generating a high‑resolution Code128 barcode and saving it as a PNG file.
/// Entry point. Configures barcode generator settings and saves the image.
/// </summary>
class Program
static void Main()
{
/// <summary>
/// Entry point of the application. Creates a barcode with specific dimensions and resolution,
/// then writes the image to disk.
/// </summary>
static void Main()
// Initialize a barcode generator for Code128 with the sample text "HIGHRES12345"
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "HIGHRES12345"))
{
// Path where the generated barcode image will be saved.
string outputPath = "highres_label.png";
// Set the image resolution to 300 DPI, which is appropriate for high‑quality label printing
generator.Parameters.Resolution = 300;

// Initialize a barcode generator for Code128 with the sample text "1234567890".
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890"))
{
// Set the image resolution to 300 DPI, suitable for high‑quality label printing.
generator.Parameters.Resolution = 300f;
// Use interpolation mode to allow explicit pixel dimensions for the output image
generator.Parameters.AutoSizeMode = AutoSizeMode.Interpolation;

// Turn off automatic sizing so we can manually define dimensions.
generator.Parameters.AutoSizeMode = AutoSizeMode.None;
// Define the exact image size in pixels (width x height) for the label
generator.Parameters.ImageWidth.Pixels = 1200f; // label width
generator.Parameters.ImageHeight.Pixels = 600f; // label height

// Configure the module (X) size and the bar height in points.
generator.Parameters.Barcode.XDimension.Point = 2f; // 2 points per module (narrow bar width)
generator.Parameters.Barcode.BarHeight.Point = 50f; // 50 points tall (overall bar height)
// Configure module (X‑dimension) size and bar height (height is ignored in interpolation mode but set for completeness)
generator.Parameters.Barcode.XDimension.Pixels = 2f; // each module ~2 pixels wide
generator.Parameters.Barcode.BarHeight.Pixels = 50f; // bar height for 1D barcodes

// Apply uniform padding of 5 points on all sides of the barcode.
generator.Parameters.Barcode.Padding.Left.Point = 5f;
generator.Parameters.Barcode.Padding.Top.Point = 5f;
generator.Parameters.Barcode.Padding.Right.Point = 5f;
generator.Parameters.Barcode.Padding.Bottom.Point = 5f;
// Set high‑contrast colors: black bars on a white background
generator.Parameters.Barcode.BarColor = Color.Black;
generator.Parameters.BackColor = Color.White;

// Save the generated barcode image to the specified file.
// The format (PNG) is inferred from the file extension.
generator.Save(outputPath);
}
// Customize human‑readable text appearance
generator.Parameters.Barcode.CodeTextParameters.Font.FamilyName = "Arial";
generator.Parameters.Barcode.CodeTextParameters.Font.Size.Point = 12f;
generator.Parameters.Barcode.CodeTextParameters.Alignment = TextAlignment.Center;
generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.Below;

// Inform the user that the image has been saved.
Console.WriteLine($"Barcode image saved to: {outputPath}");
// Add uniform padding of 5 points on all sides of the barcode
generator.Parameters.Barcode.Padding.Left.Point = 5f;
generator.Parameters.Barcode.Padding.Top.Point = 5f;
generator.Parameters.Barcode.Padding.Right.Point = 5f;
generator.Parameters.Barcode.Padding.Bottom.Point = 5f;

// Save the generated barcode as a PNG file
string outputPath = "highres_label.png";
generator.Save(outputPath);
Console.WriteLine($"Barcode saved to {outputPath}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
// Title: Custom Foreground and Background Colors for Barcode Image
// Description: Demonstrates how to apply custom bar (foreground) and background colors when generating a barcode with Aspose.BarCode.
// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating the use of BarcodeGenerator, EncodeTypes, and the Parameters property to customize visual aspects of barcodes. Typical scenarios include branding, UI integration, and printing where specific color schemes are required. Developers often need to adjust bar and background colors to match corporate identity or improve readability on various media.
// Prompt: Apply custom foreground and background colors to the barcode image using generator settings.
// Tags: barcode, color, generation, png, aspose.barcode, csharp

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

/// <summary>
/// Demonstrates generating a Code128 barcode with custom colors using Aspose.BarCode.
/// Shows how to set custom foreground (bar) and background colors for a generated barcode image.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application. Generates a barcode image with custom foreground and background colors,
/// saves it to a PNG file, and writes the output path to the console.
/// Entry point of the example. Generates a Code128 barcode with blue bars on a light‑gray background and saves it as a PNG file.
/// </summary>
static void Main()
{
// Define the output file path for the generated barcode image.
string outputPath = "custom_color_barcode.png";

// Initialize a BarcodeGenerator for Code128 symbology with the desired text.
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
// Initialize a BarcodeGenerator for the Code128 symbology with sample text.
using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
{
// Set the color of the barcode bars (foreground).
// Set the foreground color (the color of the bars) to blue.
generator.Parameters.Barcode.BarColor = Color.Blue;

// Set the background color of the image.
generator.Parameters.BackColor = Color.LightYellow;

// Increase the image resolution to 300 DPI for higher quality output.
generator.Parameters.Resolution = 300f;
// Set the background color of the image to light gray.
generator.Parameters.BackColor = Color.LightGray;

// Save the generated barcode as a PNG file to the specified path.
// Save the generated barcode image to the specified file path (default format is PNG).
generator.Save(outputPath);
}

// Inform the user where the barcode image has been saved.
Console.WriteLine($"Barcode image saved to {outputPath}");
Console.WriteLine($"Barcode image saved to: {outputPath}");
}
}
Original file line number Diff line number Diff line change
@@ -1,99 +1,125 @@
// Title: Batch Mailmark Barcode Generation from CSV
// Description: Demonstrates reading a CSV file with Mailmark data and generating a PNG barcode for each row using Aspose.BarCode.
// Category-Description: This example belongs to the Aspose.BarCode generation category, focusing on complex barcodes such as Mailmark. It showcases the use of MailmarkCodetext and ComplexBarcodeGenerator classes to create 4‑state Mailmark symbols, a common requirement for postal automation and tracking solutions. Developers often need to batch‑process data sources (e.g., CSV, databases) to produce barcodes for large volumes of items.
// Prompt: Batch generate Mailmark barcodes from a CSV file containing multiple rows of field data.
// Tags: mailmark, barcode, csv, batch, generation, png, aspose.barcode

using System;
using System.IO;
using System.Collections.Generic;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.ComplexBarcode;

/// <summary>
/// Generates Mailmark barcodes from a CSV file or sample data and saves them as PNG images.
/// Generates Mailmark barcodes in batch from a CSV file.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Reads records, constructs MailmarkCodetext objects, and creates barcode images.
/// Entry point of the application. Reads Mailmark data from a CSV file,
/// creates a MailmarkCodetext for each record, and saves the resulting PNG images.
/// </summary>
static void Main()
{
const string csvPath = "mailmark_data.csv";

// Collection to hold each CSV record as an array of fields
List<string[]> records = new List<string[]>();
// Path to the input CSV file containing Mailmark fields.
string csvPath = "mailmark_data.csv";

// Attempt to read data from the CSV file if it exists
if (File.Exists(csvPath))
// Ensure a sample CSV exists if the file is missing.
if (!File.Exists(csvPath))
{
// Read all lines and split each line by commas (simple parsing, no quoted fields)
foreach (var line in File.ReadAllLines(csvPath))
// Create a small safe sample (5 rows) with required fields.
// Format is fixed to 4 for 4‑state Mailmark.
// DestinationPostCodePlusDPS uses the required trailing space.
string[] sampleLines = new[]
{
// Skip empty or whitespace-only lines
if (string.IsNullOrWhiteSpace(line)) continue;

// Split the line into individual fields
var parts = line.Split(',');

// Ensure the line has at least the expected number of columns
if (parts.Length >= 6)
records.Add(parts);
}
"VersionID,Class,SupplychainID,ItemID,DestinationPostCodePlusDPS",
"1,0,384224,16563762,EF61AH8T ",
"1,1,384224,16563763,EF61AH8T ",
"1,2,384224,16563764,EF61AH8T ",
"1,3,384224,16563765,EF61AH8T ",
"1,0,384224,16563766,EF61AH8T "
};
File.WriteAllLines(csvPath, sampleLines);
}
else

// Output directory for generated barcode images.
string outputDir = "Barcodes";
if (!Directory.Exists(outputDir))
{
// CSV not found – use hard‑coded sample data (5 rows)
records.Add(new[] { "4", "1", "0", "384224", "16563762", "EF61AH8T " });
records.Add(new[] { "4", "1", "1", "384224", "16563763", "EF61AH8T " });
records.Add(new[] { "4", "1", "2", "384224", "16563764", "EF61AH8T " });
records.Add(new[] { "4", "1", "3", "384224", "16563765", "EF61AH8T " });
records.Add(new[] { "4", "1", "4", "384224", "16563766", "EF61AH8T " });
Directory.CreateDirectory(outputDir);
}

int index = 0; // Counter for generated files
// Read all lines from the CSV file and skip the header row.
string[] lines = File.ReadAllLines(csvPath);
if (lines.Length <= 1)
{
Console.WriteLine("CSV file contains no data rows.");
return;
}

// Process each record and generate a barcode
foreach (var fields in records)
// Process each data row in the CSV.
for (int i = 1; i < lines.Length; i++)
{
try
string line = lines[i];
if (string.IsNullOrWhiteSpace(line))
continue; // Skip empty lines.

// Split the line into individual fields.
string[] parts = line.Split(',');

// Validate the expected number of fields (5).
if (parts.Length != 5)
{
Console.WriteLine($"Skipping malformed line {i + 1}: {line}");
continue;
}

// Parse VersionID.
if (!int.TryParse(parts[0].Trim(), out int versionId))
{
Console.WriteLine($"Invalid VersionID on line {i + 1}");
continue;
}

// Class is a string value (e.g., "0", "1").
string classValue = parts[1].Trim();

// Parse SupplychainID.
if (!int.TryParse(parts[2].Trim(), out int supplyChainId))
{
// Parse numeric and string fields from the CSV record
int format = int.Parse(fields[0].Trim());
int versionId = int.Parse(fields[1].Trim());
string classValue = fields[2].Trim(); // Class is a string property
int supplyChainId = int.Parse(fields[3].Trim());
int itemId = int.Parse(fields[4].Trim());
string destinationPostCodePlusDps = fields[5].Trim();

// Populate a MailmarkCodetext object with the parsed values
var mailmark = new MailmarkCodetext
{
Format = format,
VersionID = versionId,
Class = classValue,
SupplychainID = supplyChainId,
ItemID = itemId,
DestinationPostCodePlusDPS = destinationPostCodePlusDps
};

// Generate the barcode image using Aspose ComplexBarcodeGenerator
using (var generator = new ComplexBarcodeGenerator(mailmark))
{
// Construct a unique filename for each barcode
string outputFile = $"Mailmark_{itemId}_{index}.png";

// Save the barcode as a PNG file
generator.Save(outputFile, BarCodeImageFormat.Png);

// Inform the user that the file was created
Console.WriteLine($"Generated: {outputFile}");
}
Console.WriteLine($"Invalid SupplychainID on line {i + 1}");
continue;
}
catch (Exception ex)

// Parse ItemID.
if (!int.TryParse(parts[3].Trim(), out int itemId))
{
// Report any errors encountered while processing the current record
Console.WriteLine($"Error processing record #{index}: {ex.Message}");
Console.WriteLine($"Invalid ItemID on line {i + 1}");
continue;
}

index++; // Increment the file counter
// DestinationPostCodePlusDPS may contain trailing spaces, which are required.
string destination = parts[4];

// Build the Mailmark codetext object with the parsed values.
var mailmark = new MailmarkCodetext
{
Format = 4, // 4‑state Mailmark.
VersionID = versionId,
Class = classValue,
SupplychainID = supplyChainId,
ItemID = itemId,
DestinationPostCodePlusDPS = destination
};

// Generate the barcode using ComplexBarcodeGenerator.
using (var generator = new ComplexBarcodeGenerator(mailmark))
{
string outPath = Path.Combine(outputDir, $"Mailmark_{itemId}.png");
generator.Save(outPath, BarCodeImageFormat.Png);
Console.WriteLine($"Generated barcode for ItemID {itemId} -> {outPath}");
}
}

Console.WriteLine("Batch generation completed.");
}
}
Loading
Loading