Skip to content

Commit dde84ea

Browse files
Merge pull request #78 from aspose-barcode/agent/mailmark-two-dimensional-barcode/2026-07-07-000556
feat: Add Aspose.BarCode .NET C# Examples — Mailmark Two Dimensional Barcode
2 parents d0bfe74 + a782547 commit dde84ea

34 files changed

Lines changed: 1485 additions & 1396 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -1,49 +1,61 @@
1+
// Title: High‑Resolution Barcode Generation for Label Printing
2+
// Description: Demonstrates configuring Aspose.BarCode generator to create a high‑resolution PNG suitable for printing labels.
3+
// 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.
4+
// Prompt: Adjust generator settings to produce a barcode image suitable for high‑resolution printing on labels.
5+
// Tags: code128, highresolution, png, barcode generation, aspnet, aspose.barcode, image parameters
6+
17
using System;
28
using Aspose.BarCode;
39
using Aspose.BarCode.Generation;
10+
using Aspose.Drawing;
411

5-
namespace BarcodeHighResolution
12+
/// <summary>
13+
/// Generates a high‑resolution Code128 barcode image suitable for label printing.
14+
/// </summary>
15+
class Program
616
{
717
/// <summary>
8-
/// Demonstrates generating a high‑resolution Code128 barcode and saving it as a PNG file.
18+
/// Entry point. Configures barcode generator settings and saves the image.
919
/// </summary>
10-
class Program
20+
static void Main()
1121
{
12-
/// <summary>
13-
/// Entry point of the application. Creates a barcode with specific dimensions and resolution,
14-
/// then writes the image to disk.
15-
/// </summary>
16-
static void Main()
22+
// Initialize a barcode generator for Code128 with the sample text "HIGHRES12345"
23+
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "HIGHRES12345"))
1724
{
18-
// Path where the generated barcode image will be saved.
19-
string outputPath = "highres_label.png";
25+
// Set the image resolution to 300 DPI, which is appropriate for high‑quality label printing
26+
generator.Parameters.Resolution = 300;
2027

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

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

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

34-
// Apply uniform padding of 5 points on all sides of the barcode.
35-
generator.Parameters.Barcode.Padding.Left.Point = 5f;
36-
generator.Parameters.Barcode.Padding.Top.Point = 5f;
37-
generator.Parameters.Barcode.Padding.Right.Point = 5f;
38-
generator.Parameters.Barcode.Padding.Bottom.Point = 5f;
39+
// Set high‑contrast colors: black bars on a white background
40+
generator.Parameters.Barcode.BarColor = Color.Black;
41+
generator.Parameters.BackColor = Color.White;
3942

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

45-
// Inform the user that the image has been saved.
46-
Console.WriteLine($"Barcode image saved to: {outputPath}");
49+
// Add uniform padding of 5 points on all sides of the barcode
50+
generator.Parameters.Barcode.Padding.Left.Point = 5f;
51+
generator.Parameters.Barcode.Padding.Top.Point = 5f;
52+
generator.Parameters.Barcode.Padding.Right.Point = 5f;
53+
generator.Parameters.Barcode.Padding.Bottom.Point = 5f;
54+
55+
// Save the generated barcode as a PNG file
56+
string outputPath = "highres_label.png";
57+
generator.Save(outputPath);
58+
Console.WriteLine($"Barcode saved to {outputPath}");
4759
}
4860
}
4961
}
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
1+
// Title: Custom Foreground and Background Colors for Barcode Image
2+
// Description: Demonstrates how to apply custom bar (foreground) and background colors when generating a barcode with Aspose.BarCode.
3+
// 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.
4+
// Prompt: Apply custom foreground and background colors to the barcode image using generator settings.
5+
// Tags: barcode, color, generation, png, aspose.barcode, csharp
6+
17
using System;
28
using Aspose.BarCode.Generation;
39
using Aspose.Drawing;
410

511
/// <summary>
6-
/// Demonstrates generating a Code128 barcode with custom colors using Aspose.BarCode.
12+
/// Shows how to set custom foreground (bar) and background colors for a generated barcode image.
713
/// </summary>
814
class Program
915
{
1016
/// <summary>
11-
/// Entry point of the application. Generates a barcode image with custom foreground and background colors,
12-
/// saves it to a PNG file, and writes the output path to the console.
17+
/// Entry point of the example. Generates a Code128 barcode with blue bars on a light‑gray background and saves it as a PNG file.
1318
/// </summary>
1419
static void Main()
1520
{
1621
// Define the output file path for the generated barcode image.
1722
string outputPath = "custom_color_barcode.png";
1823

19-
// Initialize a BarcodeGenerator for Code128 symbology with the desired text.
20-
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
24+
// Initialize a BarcodeGenerator for the Code128 symbology with sample text.
25+
using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
2126
{
22-
// Set the color of the barcode bars (foreground).
27+
// Set the foreground color (the color of the bars) to blue.
2328
generator.Parameters.Barcode.BarColor = Color.Blue;
2429

25-
// Set the background color of the image.
26-
generator.Parameters.BackColor = Color.LightYellow;
27-
28-
// Increase the image resolution to 300 DPI for higher quality output.
29-
generator.Parameters.Resolution = 300f;
30+
// Set the background color of the image to light gray.
31+
generator.Parameters.BackColor = Color.LightGray;
3032

31-
// Save the generated barcode as a PNG file to the specified path.
33+
// Save the generated barcode image to the specified file path (default format is PNG).
3234
generator.Save(outputPath);
3335
}
3436

3537
// Inform the user where the barcode image has been saved.
36-
Console.WriteLine($"Barcode image saved to {outputPath}");
38+
Console.WriteLine($"Barcode image saved to: {outputPath}");
3739
}
3840
}
Lines changed: 95 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,125 @@
1+
// Title: Batch Mailmark Barcode Generation from CSV
2+
// Description: Demonstrates reading a CSV file with Mailmark data and generating a PNG barcode for each row using Aspose.BarCode.
3+
// 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.
4+
// Prompt: Batch generate Mailmark barcodes from a CSV file containing multiple rows of field data.
5+
// Tags: mailmark, barcode, csv, batch, generation, png, aspose.barcode
6+
17
using System;
28
using System.IO;
3-
using System.Collections.Generic;
4-
using Aspose.BarCode;
59
using Aspose.BarCode.Generation;
610
using Aspose.BarCode.ComplexBarcode;
711

812
/// <summary>
9-
/// Generates Mailmark barcodes from a CSV file or sample data and saves them as PNG images.
13+
/// Generates Mailmark barcodes in batch from a CSV file.
1014
/// </summary>
1115
class Program
1216
{
1317
/// <summary>
14-
/// Entry point of the application.
15-
/// Reads records, constructs MailmarkCodetext objects, and creates barcode images.
18+
/// Entry point of the application. Reads Mailmark data from a CSV file,
19+
/// creates a MailmarkCodetext for each record, and saves the resulting PNG images.
1620
/// </summary>
1721
static void Main()
1822
{
19-
const string csvPath = "mailmark_data.csv";
20-
21-
// Collection to hold each CSV record as an array of fields
22-
List<string[]> records = new List<string[]>();
23+
// Path to the input CSV file containing Mailmark fields.
24+
string csvPath = "mailmark_data.csv";
2325

24-
// Attempt to read data from the CSV file if it exists
25-
if (File.Exists(csvPath))
26+
// Ensure a sample CSV exists if the file is missing.
27+
if (!File.Exists(csvPath))
2628
{
27-
// Read all lines and split each line by commas (simple parsing, no quoted fields)
28-
foreach (var line in File.ReadAllLines(csvPath))
29+
// Create a small safe sample (5 rows) with required fields.
30+
// Format is fixed to 4 for 4‑state Mailmark.
31+
// DestinationPostCodePlusDPS uses the required trailing space.
32+
string[] sampleLines = new[]
2933
{
30-
// Skip empty or whitespace-only lines
31-
if (string.IsNullOrWhiteSpace(line)) continue;
32-
33-
// Split the line into individual fields
34-
var parts = line.Split(',');
35-
36-
// Ensure the line has at least the expected number of columns
37-
if (parts.Length >= 6)
38-
records.Add(parts);
39-
}
34+
"VersionID,Class,SupplychainID,ItemID,DestinationPostCodePlusDPS",
35+
"1,0,384224,16563762,EF61AH8T ",
36+
"1,1,384224,16563763,EF61AH8T ",
37+
"1,2,384224,16563764,EF61AH8T ",
38+
"1,3,384224,16563765,EF61AH8T ",
39+
"1,0,384224,16563766,EF61AH8T "
40+
};
41+
File.WriteAllLines(csvPath, sampleLines);
4042
}
41-
else
43+
44+
// Output directory for generated barcode images.
45+
string outputDir = "Barcodes";
46+
if (!Directory.Exists(outputDir))
4247
{
43-
// CSV not found – use hard‑coded sample data (5 rows)
44-
records.Add(new[] { "4", "1", "0", "384224", "16563762", "EF61AH8T " });
45-
records.Add(new[] { "4", "1", "1", "384224", "16563763", "EF61AH8T " });
46-
records.Add(new[] { "4", "1", "2", "384224", "16563764", "EF61AH8T " });
47-
records.Add(new[] { "4", "1", "3", "384224", "16563765", "EF61AH8T " });
48-
records.Add(new[] { "4", "1", "4", "384224", "16563766", "EF61AH8T " });
48+
Directory.CreateDirectory(outputDir);
4949
}
5050

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

53-
// Process each record and generate a barcode
54-
foreach (var fields in records)
59+
// Process each data row in the CSV.
60+
for (int i = 1; i < lines.Length; i++)
5561
{
56-
try
62+
string line = lines[i];
63+
if (string.IsNullOrWhiteSpace(line))
64+
continue; // Skip empty lines.
65+
66+
// Split the line into individual fields.
67+
string[] parts = line.Split(',');
68+
69+
// Validate the expected number of fields (5).
70+
if (parts.Length != 5)
71+
{
72+
Console.WriteLine($"Skipping malformed line {i + 1}: {line}");
73+
continue;
74+
}
75+
76+
// Parse VersionID.
77+
if (!int.TryParse(parts[0].Trim(), out int versionId))
78+
{
79+
Console.WriteLine($"Invalid VersionID on line {i + 1}");
80+
continue;
81+
}
82+
83+
// Class is a string value (e.g., "0", "1").
84+
string classValue = parts[1].Trim();
85+
86+
// Parse SupplychainID.
87+
if (!int.TryParse(parts[2].Trim(), out int supplyChainId))
5788
{
58-
// Parse numeric and string fields from the CSV record
59-
int format = int.Parse(fields[0].Trim());
60-
int versionId = int.Parse(fields[1].Trim());
61-
string classValue = fields[2].Trim(); // Class is a string property
62-
int supplyChainId = int.Parse(fields[3].Trim());
63-
int itemId = int.Parse(fields[4].Trim());
64-
string destinationPostCodePlusDps = fields[5].Trim();
65-
66-
// Populate a MailmarkCodetext object with the parsed values
67-
var mailmark = new MailmarkCodetext
68-
{
69-
Format = format,
70-
VersionID = versionId,
71-
Class = classValue,
72-
SupplychainID = supplyChainId,
73-
ItemID = itemId,
74-
DestinationPostCodePlusDPS = destinationPostCodePlusDps
75-
};
76-
77-
// Generate the barcode image using Aspose ComplexBarcodeGenerator
78-
using (var generator = new ComplexBarcodeGenerator(mailmark))
79-
{
80-
// Construct a unique filename for each barcode
81-
string outputFile = $"Mailmark_{itemId}_{index}.png";
82-
83-
// Save the barcode as a PNG file
84-
generator.Save(outputFile, BarCodeImageFormat.Png);
85-
86-
// Inform the user that the file was created
87-
Console.WriteLine($"Generated: {outputFile}");
88-
}
89+
Console.WriteLine($"Invalid SupplychainID on line {i + 1}");
90+
continue;
8991
}
90-
catch (Exception ex)
92+
93+
// Parse ItemID.
94+
if (!int.TryParse(parts[3].Trim(), out int itemId))
9195
{
92-
// Report any errors encountered while processing the current record
93-
Console.WriteLine($"Error processing record #{index}: {ex.Message}");
96+
Console.WriteLine($"Invalid ItemID on line {i + 1}");
97+
continue;
9498
}
9599

96-
index++; // Increment the file counter
100+
// DestinationPostCodePlusDPS may contain trailing spaces, which are required.
101+
string destination = parts[4];
102+
103+
// Build the Mailmark codetext object with the parsed values.
104+
var mailmark = new MailmarkCodetext
105+
{
106+
Format = 4, // 4‑state Mailmark.
107+
VersionID = versionId,
108+
Class = classValue,
109+
SupplychainID = supplyChainId,
110+
ItemID = itemId,
111+
DestinationPostCodePlusDPS = destination
112+
};
113+
114+
// Generate the barcode using ComplexBarcodeGenerator.
115+
using (var generator = new ComplexBarcodeGenerator(mailmark))
116+
{
117+
string outPath = Path.Combine(outputDir, $"Mailmark_{itemId}.png");
118+
generator.Save(outPath, BarCodeImageFormat.Png);
119+
Console.WriteLine($"Generated barcode for ItemID {itemId} -> {outPath}");
120+
}
97121
}
122+
123+
Console.WriteLine("Batch generation completed.");
98124
}
99125
}

0 commit comments

Comments
 (0)