Skip to content

Commit d0bfe74

Browse files
Merge pull request #77 from aspose-barcode/agent/mailmark-four-state-barcode/2026-07-06-212246
feat: Add Aspose.BarCode .NET C# Examples — Mailmark Four State Barcode
2 parents 2f30096 + 833763e commit d0bfe74

29 files changed

Lines changed: 1344 additions & 1360 deletions

File tree

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,68 @@
1+
// Title: Generate a compact Mailmark barcode for small labels
2+
// Description: Demonstrates how to adjust the module size and padding of a Mailmark barcode to create a compact image suitable for small label printing.
3+
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, focusing on complex barcode symbologies such as Mailmark. It showcases the use of ComplexBarcodeGenerator, MailmarkCodetext, and barcode parameter settings like XDimension, AutoSizeMode, and padding. Developers often need to create high‑density barcodes for limited space applications, and this snippet provides a concise reference for those scenarios.
4+
// Prompt: Adjust barcode module size to produce a compact Mailmark barcode suitable for small labels.
5+
// Tags: mailmark, barcode, module size, compact, small labels, generation, aspose.barcode
6+
17
using System;
8+
using System.IO;
29
using Aspose.BarCode;
310
using Aspose.BarCode.Generation;
411
using Aspose.BarCode.ComplexBarcode;
512
using Aspose.BarCode.BarCodeRecognition;
6-
using Aspose.Drawing.Imaging;
13+
using Aspose.Drawing;
714

815
/// <summary>
9-
/// Demonstrates generation of a Mailmark barcode using Aspose.BarCode library.
16+
/// Program demonstrating compact Mailmark barcode generation.
1017
/// </summary>
1118
class Program
1219
{
1320
/// <summary>
14-
/// Entry point of the application. Generates a Mailmark barcode image and saves it to disk.
21+
/// Entry point. Generates a Mailmark barcode with reduced module size and padding, then saves it as PNG.
1522
/// </summary>
1623
static void Main()
1724
{
18-
// Prepare Mailmark data (valid sample)
19-
var mailmark = new MailmarkCodetext
20-
{
21-
// 4‑state format identifier
22-
Format = 4,
23-
// Version identifier
24-
VersionID = 1,
25-
// Test class identifier
26-
Class = "0",
27-
// Supply chain identifier
28-
SupplychainID = 384224,
29-
// Item identifier
30-
ItemID = 16563762,
31-
// Destination postcode plus DP suffix (trailing space is intentional)
32-
DestinationPostCodePlusDPS = "EF61AH8T "
33-
};
34-
35-
// Create a generator for the Mailmark barcode using the prepared data
25+
// Prepare Mailmark codetext with required fields
26+
var mailmark = new MailmarkCodetext();
27+
mailmark.Format = 4; // unspecified/default format for compact barcode
28+
mailmark.VersionID = 1;
29+
mailmark.Class = "0";
30+
mailmark.SupplychainID = 384224;
31+
mailmark.ItemID = 16563762;
32+
mailmark.DestinationPostCodePlusDPS = "EF61AH8T ";
33+
34+
// Create ComplexBarcodeGenerator for Mailmark
3635
using (var generator = new ComplexBarcodeGenerator(mailmark))
3736
{
38-
// Reduce the module size (XDimension) for a tighter barcode appearance
39-
generator.Parameters.Barcode.XDimension.Point = 0.5f;
37+
// Use interpolation auto-size mode so that XDimension controls overall size
38+
generator.Parameters.AutoSizeMode = AutoSizeMode.Interpolation;
4039

41-
// Enable auto‑size mode to automatically fit the barcode within the target dimensions
42-
generator.Parameters.AutoSizeMode = AutoSizeMode.Nearest;
40+
// Set a small module (dot) size for compactness
41+
generator.Parameters.Barcode.XDimension.Point = 0.5f; // 0.5 point per module
4342

44-
// Define the target image dimensions (in points)
45-
generator.Parameters.ImageWidth.Point = 200f; // Desired image width
46-
generator.Parameters.ImageHeight.Point = 100f; // Desired image height
43+
// Reduce padding to keep the barcode tight on small labels
44+
generator.Parameters.Barcode.Padding.Left.Point = 1f;
45+
generator.Parameters.Barcode.Padding.Top.Point = 1f;
46+
generator.Parameters.Barcode.Padding.Right.Point = 1f;
47+
generator.Parameters.Barcode.Padding.Bottom.Point = 1f;
4748

48-
// Save the generated barcode as a PNG file
49-
string outputPath = "mailmark.png";
50-
generator.Save(outputPath, BarCodeImageFormat.Png);
49+
// Optional: set foreground and background colors
50+
generator.Parameters.Barcode.BarColor = Color.Black;
51+
generator.Parameters.BackColor = Color.White;
5152

52-
// Inform the user where the file was saved
53-
Console.WriteLine($"Mailmark barcode saved to {outputPath}");
53+
// Define output path
54+
string outputPath = "mailmark_compact.png";
55+
56+
// Ensure the directory exists
57+
string directory = Path.GetDirectoryName(Path.GetFullPath(outputPath));
58+
if (!Directory.Exists(directory))
59+
{
60+
Directory.CreateDirectory(directory);
61+
}
62+
63+
// Save the barcode image
64+
generator.Save(outputPath, BarCodeImageFormat.Png);
65+
Console.WriteLine($"Compact Mailmark barcode saved to: {outputPath}");
5466
}
5567
}
5668
}

mailmark-four-state-barcode/batch-generate-mailmark-barcodes-from-customer-records-saving-each-as-separate-bmp-files.cs

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// Title: Batch Mailmark Barcode Generation to BMP Files
2+
// Description: Demonstrates generating Mailmark barcodes for a list of customer records and saving each barcode as an individual BMP image.
3+
// Category-Description: This example belongs to the Aspose.BarCode generation category, focusing on complex barcode types such as Mailmark. It showcases the use of Aspose.BarCode.Generation.ComplexBarcodeGenerator and Aspose.BarCode.ComplexBarcode.MailmarkCodetext to create barcodes programmatically. Developers often need to batch‑process records, embed specific data fields, and export barcodes to image formats for printing or archival purposes.
4+
// Prompt: Batch generate Mailmark barcodes from customer records, saving each as separate BMP files.
5+
// Tags: mailmark, barcode, generation, bmp, aspose.barcode, complexbarcode
6+
17
using System;
28
using System.Collections.Generic;
39
using System.IO;
@@ -6,62 +12,53 @@
612
using Aspose.BarCode.ComplexBarcode;
713

814
/// <summary>
9-
/// Demonstrates generation of Mailmark barcodes using Aspose.BarCode library.
15+
/// Generates Mailmark barcodes for a collection of customer records and saves each barcode as a BMP file.
1016
/// </summary>
1117
class Program
1218
{
1319
/// <summary>
14-
/// Entry point of the application. Generates a set of Mailmark barcodes and saves them as BMP files.
20+
/// Entry point of the application. Creates an output folder, iterates over sample records,
21+
/// builds Mailmark codetext, generates the barcode, and writes it to disk.
1522
/// </summary>
1623
static void Main()
1724
{
18-
// --------------------------------------------------------------------
19-
// Prepare output directory where barcode images will be saved.
20-
// --------------------------------------------------------------------
25+
// Define the output directory and ensure it exists
2126
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "MailmarkBarcodes");
2227
if (!Directory.Exists(outputDir))
2328
{
2429
Directory.CreateDirectory(outputDir);
2530
}
2631

27-
// --------------------------------------------------------------------
28-
// Sample customer records (limited to a safe batch size).
29-
// Each record is represented by a MailmarkCodetext instance.
30-
// --------------------------------------------------------------------
31-
var customers = new List<MailmarkCodetext>
32+
// Sample customer records (limited to 5 for safe execution)
33+
var records = new List<(int ItemID, string DestinationPostCodePlusDPS)>
3234
{
33-
new MailmarkCodetext { Format = 4, VersionID = 1, Class = "0", SupplychainID = 384224, ItemID = 16563762, DestinationPostCodePlusDPS = "EF61AH8T " },
34-
new MailmarkCodetext { Format = 4, VersionID = 1, Class = "1", SupplychainID = 384224, ItemID = 16563763, DestinationPostCodePlusDPS = "EF61AH8T " },
35-
new MailmarkCodetext { Format = 4, VersionID = 1, Class = "2", SupplychainID = 384224, ItemID = 16563764, DestinationPostCodePlusDPS = "EF61AH8T " },
36-
new MailmarkCodetext { Format = 4, VersionID = 1, Class = "3", SupplychainID = 384224, ItemID = 16563765, DestinationPostCodePlusDPS = "EF61AH8T " },
37-
new MailmarkCodetext { Format = 4, VersionID = 1, Class = "4", SupplychainID = 384224, ItemID = 16563766, DestinationPostCodePlusDPS = "EF61AH8T " }
35+
(16563762, "EF61AH8T "),
36+
(16563763, "EF61AH8T "),
37+
(16563764, "EF61AH8T "),
38+
(16563765, "EF61AH8T "),
39+
(16563766, "EF61AH8T ")
3840
};
3941

40-
// --------------------------------------------------------------------
41-
// Iterate over each customer record, generate the barcode, and save it.
42-
// --------------------------------------------------------------------
4342
int index = 1;
44-
foreach (var mailmark in customers)
43+
foreach (var record in records)
4544
{
46-
// Build a unique file name for each barcode image.
47-
string fileName = $"Mailmark_{index:D3}.bmp";
48-
string filePath = Path.Combine(outputDir, fileName);
49-
50-
try
45+
// Build the Mailmark codetext with required fields for each record
46+
var mailmark = new MailmarkCodetext
5147
{
52-
// Generate the barcode using the ComplexBarcodeGenerator.
53-
using (var generator = new ComplexBarcodeGenerator(mailmark))
54-
{
55-
// Save the generated barcode as a BMP file.
56-
generator.Save(filePath, BarCodeImageFormat.Bmp);
57-
}
48+
Format = 4, // 4‑state (unspecified/default)
49+
VersionID = 1,
50+
Class = "0", // Test/Null class
51+
SupplychainID = 384224,
52+
ItemID = record.ItemID,
53+
DestinationPostCodePlusDPS = record.DestinationPostCodePlusDPS
54+
};
5855

59-
Console.WriteLine($"Generated: {filePath}");
60-
}
61-
catch (Exception ex)
56+
// Generate the barcode and save it as a BMP image
57+
using (var generator = new ComplexBarcodeGenerator(mailmark))
6258
{
63-
// Log any errors that occur during barcode generation.
64-
Console.WriteLine($"Failed to generate barcode for record {index}: {ex.Message}");
59+
string filePath = Path.Combine(outputDir, $"Mailmark_{index}.bmp");
60+
generator.Save(filePath, BarCodeImageFormat.Bmp);
61+
Console.WriteLine($"Saved Mailmark barcode to: {filePath}");
6562
}
6663

6764
index++;
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,85 @@
1+
// Title: Mailmark Barcode Generation Performance: PNG vs JPEG
2+
// Description: Demonstrates measuring generation time and file size when creating Mailmark barcodes in PNG and JPEG formats.
3+
// Category-Description: This example belongs to the Aspose.BarCode generation category, showcasing the use of ComplexBarcodeGenerator to produce Mailmark symbology. It illustrates typical tasks such as configuring Mailmark codetext, rendering barcodes to different image formats, and profiling performance—common needs for developers integrating barcode creation into high‑throughput or size‑sensitive applications. The snippet highlights key API classes like MailmarkCodetext, ComplexBarcodeGenerator, and BarCodeImageFormat, serving as a reference for performance‑oriented barcode generation scenarios.
4+
/// Prompt: Compare performance of generating Mailmark barcodes as PNG versus JPEG by measuring file size and generation time.
5+
/// Tags: mailmark, barcode, performance, png, jpeg, generation, aspose.barcode, complexbarcodegenerator
6+
17
using System;
28
using System.Diagnostics;
39
using System.IO;
410
using Aspose.BarCode;
511
using Aspose.BarCode.Generation;
612
using Aspose.BarCode.ComplexBarcode;
13+
using Aspose.Drawing.Imaging;
714

815
/// <summary>
9-
/// Demonstrates generation of Mailmark barcodes in PNG and JPEG formats,
10-
/// measuring generation time and optionally saving the images to disk.
16+
/// Generates Mailmark barcodes in PNG and JPEG formats and compares their generation time and file size.
1117
/// </summary>
1218
class Program
1319
{
1420
/// <summary>
15-
/// Entry point of the application.
16-
/// Prepares a Mailmark codetext, generates barcodes in two image formats,
17-
/// measures performance, and writes the results to the console and files.
21+
/// Entry point of the example. Prepares Mailmark data, renders the barcode in two image formats,
22+
/// and outputs average generation time and file size for each format.
1823
/// </summary>
1924
static void Main()
2025
{
21-
// Prepare a valid Mailmark codetext object with required properties.
26+
// Prepare a valid Mailmark codetext with required fields.
2227
var mailmark = new MailmarkCodetext
2328
{
24-
Format = 4, // 4‑state format
29+
Format = 4, // 4‑state barcode
2530
VersionID = 1,
26-
Class = "0", // string property representing class
31+
Class = "0",
2732
SupplychainID = 384224,
2833
ItemID = 16563762,
29-
DestinationPostCodePlusDPS = "EF61AH8T " // known‑valid value (trailing space intentional)
34+
DestinationPostCodePlusDPS = "EF61AH8T "
35+
};
36+
37+
// Define the image formats to be tested: PNG and JPEG.
38+
var formats = new[]
39+
{
40+
BarCodeImageFormat.Png,
41+
BarCodeImageFormat.Jpeg
3042
};
3143

32-
// Define the image formats to compare: PNG and JPEG.
33-
var formats = new[] { BarCodeImageFormat.Png, BarCodeImageFormat.Jpeg };
44+
const int iterations = 5; // Number of repetitions for averaging results.
3445

35-
// Iterate over each format, generate the barcode, and record metrics.
36-
foreach (var fmt in formats)
46+
// Iterate over each format and measure performance.
47+
foreach (var format in formats)
3748
{
38-
// Start timing the generation process.
39-
var stopwatch = Stopwatch.StartNew();
49+
long totalBytes = 0; // Accumulator for total file size.
50+
long totalTicks = 0; // Accumulator for total elapsed ticks.
4051

41-
// Use a memory stream to hold the generated image data.
42-
using (var memory = new MemoryStream())
52+
// Run the generation multiple times to obtain average metrics.
53+
for (int i = 0; i < iterations; i++)
4354
{
44-
// Generate the Mailmark barcode and save it directly to the memory stream.
55+
// Create a generator for the Mailmark barcode.
4556
using (var generator = new ComplexBarcodeGenerator(mailmark))
4657
{
47-
generator.Save(memory, fmt);
48-
}
58+
// Use a memory stream to avoid disk I/O overhead.
59+
using (var ms = new MemoryStream())
60+
{
61+
// Start timing the Save operation.
62+
var sw = Stopwatch.StartNew();
63+
generator.Save(ms, format);
64+
sw.Stop();
4965

50-
// Stop the timer after generation completes.
51-
stopwatch.Stop();
66+
// Accumulate elapsed time and resulting byte size.
67+
totalTicks += sw.ElapsedTicks;
68+
totalBytes += ms.Length;
69+
}
70+
}
71+
}
5272

53-
// Determine the appropriate file extension based on the format.
54-
string extension = fmt == BarCodeImageFormat.Png ? ".png" : ".jpg";
55-
string fileName = "Mailmark" + extension;
73+
// Compute average generation time in milliseconds.
74+
double avgMs = (totalTicks * 1000.0) / Stopwatch.Frequency / iterations;
75+
// Compute average file size in kilobytes.
76+
double avgKb = (totalBytes / 1024.0) / iterations;
5677

57-
// Write the image bytes to a file for optional visual verification.
58-
File.WriteAllBytes(fileName, memory.ToArray());
78+
// Friendly format name for output.
79+
string formatName = format == BarCodeImageFormat.Png ? "PNG" : "JPEG";
5980

60-
// Output the size, generation time, and saved file name to the console.
61-
Console.WriteLine($"{fmt} -> Size: {memory.Length} bytes, Generation Time: {stopwatch.ElapsedMilliseconds} ms, Saved As: {fileName}");
62-
}
81+
// Display the averaged results.
82+
Console.WriteLine($"{formatName} - Average generation time: {avgMs:F2} ms, Average file size: {avgKb:F2} KB");
6383
}
6484
}
6585
}

0 commit comments

Comments
 (0)