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,61 +1,48 @@
// 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.
// Title: High‑Resolution Code128 Barcode Generation for Label Printing
// Description: Demonstrates configuring Aspose.BarCode to generate a 300 DPI Code128 barcode image suitable for high‑resolution label printing.
// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to adjust resolution, module size, bar height, colors, and auto‑size settings using the BarcodeGenerator, EncodeTypes, and Parameters classes. Typical use cases include creating printable barcodes for product labels, shipping tags, and inventory stickers where precise dimensions and high image quality are required. Developers often need to fine‑tune these settings to meet printer specifications and label design guidelines.
// 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
// Tags: code128, generation, png, barcodegenerator, encodetypes, parameters

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

/// <summary>
/// Generates a high‑resolution Code128 barcode image suitable for label printing.
/// Entry point for the high‑resolution barcode generation example.
/// </summary>
class Program
{
/// <summary>
/// Entry point. Configures barcode generator settings and saves the image.
/// Generates a Code128 barcode image with settings optimized for high‑resolution label printing.
/// </summary>
static void Main()
{
// Initialize a barcode generator for Code128 with the sample text "HIGHRES12345"
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "HIGHRES12345"))
// Create a Code128 barcode generator with sample text
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890"))
{
// Set the image resolution to 300 DPI, which is appropriate for high‑quality label printing
// Set image resolution to 300 DPI for crisp printing
generator.Parameters.Resolution = 300;

// Use interpolation mode to allow explicit pixel dimensions for the output image
generator.Parameters.AutoSizeMode = AutoSizeMode.Interpolation;
// Define a small module size (XDimension) for fine detail (0.5 points)
generator.Parameters.Barcode.XDimension.Point = 0.5f;

// 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
// Set bar height appropriate for label size (50 points)
generator.Parameters.Barcode.BarHeight.Point = 50f;

// 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

// Set high‑contrast colors: black bars on a white background
// Use black bars on a white background for maximum contrast
generator.Parameters.Barcode.BarColor = Color.Black;
generator.Parameters.BackColor = Color.White;

// 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;

// 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}");
// Disable automatic resizing to preserve exact dimensions
generator.Parameters.AutoSizeMode = AutoSizeMode.None;

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

// Inform the user that the image has been created
Console.WriteLine("Barcode image generated: highres_label.png");
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
// 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.
// Title: Custom Foreground and Background Colors for a Code128 Barcode
// Description: Demonstrates how to set custom bar (foreground) and background colors when generating a Code128 barcode image using Aspose.BarCode.
// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to customize visual appearance of barcodes via the BarcodeGenerator and its Parameters API. Developers often need to match corporate branding or improve scan reliability by adjusting bar and background colors. Typical use cases include creating PNG, JPEG, or PDF barcode assets with specific color schemes.
// Prompt: Apply custom foreground and background colors to the barcode image using generator settings.
// Tags: barcode, color, generation, png, aspose.barcode, csharp
// Tags: code128, color, png, barcodegenerator, generation

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

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

// Initialize a BarcodeGenerator for the Code128 symbology with sample text.
using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
/// <summary>
/// Entry point of the example. Creates a barcode, applies color settings, and saves it as a PNG file.
/// </summary>
static void Main()
{
// Set the foreground color (the color of the bars) to blue.
generator.Parameters.Barcode.BarColor = Color.Blue;
// Output file path for the generated barcode image
const string outputFile = "barcode.png";

// Set the background color of the image to light gray.
generator.Parameters.BackColor = Color.LightGray;
// Initialize a BarcodeGenerator for the Code128 symbology with sample data
using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890"))
{
// Set the color of the barcode bars (foreground)
generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue;

// Save the generated barcode image to the specified file path (default format is PNG).
generator.Save(outputPath);
}
// Set the background color of the image
generator.Parameters.BackColor = Aspose.Drawing.Color.Yellow;

// Inform the user where the barcode image has been saved.
Console.WriteLine($"Barcode image saved to: {outputPath}");
// Render and save the barcode as a PNG image
generator.Save(outputFile, BarCodeImageFormat.Png);
}

// Output the location of the saved barcode image
Console.WriteLine($"Barcode image saved to: {outputFile}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,123 +1,112 @@
// 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.
// Title: Batch generate Mailmark barcodes from CSV data
// Description: Demonstrates reading a CSV file with Mailmark field values and creating a PNG barcode for each record using Aspose.BarCode.
// Category-Description: This example belongs to the Aspose.BarCode complex barcode generation category. It shows how to work with the Mailmark symbology by populating a MailmarkCodetext object and using ComplexBarcodeGenerator to produce barcodes. Typical scenarios include bulk creation of Mailmark barcodes for mailing, logistics, or inventory systems. Developers often need to parse input data, validate fields, and save barcodes in common image formats such as PNG.
// Prompt: Batch generate Mailmark barcodes from a CSV file containing multiple rows of field data.
// Tags: mailmark, barcode, csv, batch, generation, png, aspose.barcode
// Tags: mailmark, barcode, csv, batch, generation, aspose.barcode, complexbarcode, png

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

/// <summary>
/// Generates Mailmark barcodes in batch from a CSV file.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application. Reads Mailmark data from a CSV file,
/// creates a MailmarkCodetext for each record, and saves the resulting PNG images.
/// Entry point. Reads the CSV, validates each row, creates a MailmarkCodetext object,
/// generates a PNG barcode, and saves it to the output folder.
/// </summary>
static void Main()
{
// Path to the input CSV file containing Mailmark fields.
string csvPath = "mailmark_data.csv";
// Define input CSV and output folder paths
string csvPath = "mailmark_input.csv";
string outputFolder = "Output";

// Ensure a sample CSV exists if the file is missing.
// Ensure the output folder exists
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputFolder);
}

// If the CSV does not exist, create a sample file with a few records
if (!File.Exists(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[]
var sampleLines = new[]
{
"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 "
"Format,VersionID,Class,SupplychainID,ItemID,DestinationPostCodePlusDPS",
"4,1,0,384224,16563760,EF61AH8T ",
"4,1,0,384224,16563761,EF61AH8T ",
"4,1,0,384224,16563762,EF61AH8T "
};
File.WriteAllLines(csvPath, sampleLines);
}

// Output directory for generated barcode images.
string outputDir = "Barcodes";
if (!Directory.Exists(outputDir))
{
Directory.CreateDirectory(outputDir);
}

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

// Process each data row in the CSV.
// Process each data row
for (int i = 1; i < lines.Length; i++)
{
string line = lines[i];
if (string.IsNullOrWhiteSpace(line))
continue; // Skip empty lines.
continue; // Skip empty lines

// Split the line into individual fields.
// Split by comma (simple CSV, no quoted commas)
string[] parts = line.Split(',');

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

// Parse VersionID.
if (!int.TryParse(parts[0].Trim(), out int versionId))
// Parse numeric fields; if parsing fails, skip the row
if (!int.TryParse(parts[0].Trim(), out int format) ||
!int.TryParse(parts[1].Trim(), out int versionId) ||
!int.TryParse(parts[3].Trim(), out int supplyChainId) ||
!int.TryParse(parts[4].Trim(), out int itemId))
{
Console.WriteLine($"Invalid VersionID on line {i + 1}");
Console.WriteLine($"Line {i + 1}: Invalid numeric values. Skipping.");
continue;
}

// Class is a string value (e.g., "0", "1").
string classValue = parts[1].Trim();
// Remaining fields are strings; preserve trailing space for destination as required
string classValue = parts[2].Trim();
string destination = parts[5];

// Parse SupplychainID.
if (!int.TryParse(parts[2].Trim(), out int supplyChainId))
// Validate that the format corresponds to 4‑state Mailmark (value 4)
if (format != 4)
{
Console.WriteLine($"Invalid SupplychainID on line {i + 1}");
Console.WriteLine($"Line {i + 1}: Unsupported Format {format}. Only 4‑state (value 4) is supported. Skipping.");
continue;
}

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

// DestinationPostCodePlusDPS may contain trailing spaces, which are required.
string destination = parts[4];

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

// Generate the barcode using ComplexBarcodeGenerator.
// Generate the barcode and save it as a PNG file
string outputPath = Path.Combine(outputFolder, $"Mailmark_{i}.png");
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}");
generator.Save(outputPath, BarCodeImageFormat.Png);
}

Console.WriteLine($"Generated barcode for line {i + 1} -> {outputPath}");
}

Console.WriteLine("Batch generation completed.");
Expand Down
Loading
Loading