Skip to content

Commit ed2224d

Browse files
Merge pull request #86 from aspose-barcode/agent/two-dimensional-barcode-types/2026-07-16-200139
feat: Add Aspose.BarCode .NET C# Examples — Two Dimensional Barcode Types
2 parents c4e3999 + 624a275 commit ed2224d

214 files changed

Lines changed: 8090 additions & 7522 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,83 @@
1+
// Title: Generate GS1 Composite barcode with configurable linear component
2+
// Description: Demonstrates reading a linear component type from a configuration file and creating a GS1 Composite barcode using Aspose.BarCode.
3+
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, focusing on GS1 Composite symbology. It showcases the use of BarcodeGenerator, EncodeTypes, and GS1CompositeBar parameters to customize linear and 2D components. Developers often need to dynamically select symbologies based on configuration or user input, and this snippet illustrates that pattern.
4+
// Prompt: Allow users to select linear component type via configuration file and generate corresponding GS1 Composite barcode.
5+
// Tags: barcode symbology, gs1 composite, configuration, aspose.barcode, generation, png output
6+
17
using System;
28
using System.IO;
9+
using System.Reflection;
310
using Aspose.BarCode;
411
using Aspose.BarCode.Generation;
512

613
/// <summary>
7-
/// Demonstrates generating a GS1 Composite barcode using Aspose.BarCode,
8-
/// with the linear component type configurable via a text file.
14+
/// Example program that reads a linear component type from a configuration file
15+
/// and generates a GS1 Composite barcode using Aspose.BarCode.
916
/// </summary>
1017
class Program
1118
{
1219
/// <summary>
13-
/// Entry point of the application.
14-
/// Reads configuration, resolves the linear component type, and generates a GS1 Composite barcode image.
20+
/// Application entry point. Generates a GS1 Composite barcode based on the
21+
/// linear component type specified in a configuration file.
1522
/// </summary>
16-
static void Main()
23+
/// <param name="args">Command‑line arguments; the first argument can specify the config file path.</param>
24+
static void Main(string[] args)
1725
{
18-
// --------------------------------------------------------------------
19-
// Configuration: read the linear component type name from a file.
20-
// --------------------------------------------------------------------
21-
const string configPath = "config.txt"; // Path to the configuration file.
22-
BaseEncodeType defaultLinearType = EncodeTypes.GS1Code128; // Fallback type if config is missing/invalid.
26+
// Determine configuration file path (first argument or default)
27+
string configPath = args.Length > 0 ? args[0] : "config.txt";
28+
29+
// Default linear component type if configuration is missing or invalid
30+
BaseEncodeType linearComponent = EncodeTypes.GS1Code128;
2331

24-
string linearTypeName = null;
32+
// Attempt to read the configuration file and resolve the symbology name
2533
if (File.Exists(configPath))
2634
{
2735
try
2836
{
29-
// Read the entire file content and trim whitespace.
30-
linearTypeName = File.ReadAllText(configPath).Trim();
37+
string symbologyName = File.ReadAllText(configPath).Trim();
38+
39+
// Resolve symbology name to EncodeTypes field via reflection
40+
FieldInfo field = typeof(EncodeTypes).GetField(symbologyName);
41+
if (field != null && typeof(BaseEncodeType).IsAssignableFrom(field.FieldType))
42+
{
43+
linearComponent = (BaseEncodeType)field.GetValue(null);
44+
}
45+
else
46+
{
47+
Console.WriteLine($"Warning: Unknown symbology '{symbologyName}'. Using default GS1Code128.");
48+
}
3149
}
3250
catch (Exception ex)
3351
{
34-
// Report any I/O errors but continue with the default type.
35-
Console.WriteLine($"Error reading config file: {ex.Message}");
52+
Console.WriteLine($"Error reading configuration: {ex.Message}. Using default GS1Code128.");
3653
}
3754
}
3855
else
3956
{
40-
// Inform the user that the config file was not found.
41-
Console.WriteLine("Config file not found. Using default linear component type.");
57+
Console.WriteLine($"Configuration file '{configPath}' not found. Using default GS1Code128.");
4258
}
4359

44-
// --------------------------------------------------------------------
45-
// Resolve the type name to a BaseEncodeType using reflection.
46-
// --------------------------------------------------------------------
47-
BaseEncodeType linearComponentType = defaultLinearType;
48-
if (!string.IsNullOrEmpty(linearTypeName))
49-
{
50-
var field = typeof(EncodeTypes).GetField(linearTypeName);
51-
if (field != null && typeof(BaseEncodeType).IsAssignableFrom(field.FieldType))
52-
{
53-
// Successful resolution – assign the configured type.
54-
linearComponentType = (BaseEncodeType)field.GetValue(null);
55-
}
56-
else
57-
{
58-
// Unknown or unsupported type – fall back to default.
59-
Console.WriteLine($"Unknown or unsupported linear component type '{linearTypeName}'. Using default.");
60-
}
61-
}
60+
// Sample GS1 Composite barcode text (1D and 2D parts separated by '|')
61+
string codeText = "(01)03212345678906|(21)A1B2C3D4E5F6G7H8";
6262

63-
// --------------------------------------------------------------------
64-
// Barcode data and output settings.
65-
// --------------------------------------------------------------------
66-
const string codeText = "(01)03212345678906|(21)A12345678"; // GS1 Composite codetext (1D|2D parts).
67-
const string outputPath = "gs1composite.png"; // Destination image file.
68-
69-
// --------------------------------------------------------------------
70-
// Generate the barcode using Aspose.BarCode.
71-
// --------------------------------------------------------------------
63+
// Generate the barcode using the GS1 Composite symbology
7264
using (var generator = new BarcodeGenerator(EncodeTypes.GS1CompositeBar, codeText))
7365
{
74-
// Apply the linear component type resolved from configuration.
75-
generator.Parameters.Barcode.GS1CompositeBar.LinearComponentType = linearComponentType;
66+
// Apply the linear component type read from configuration
67+
generator.Parameters.Barcode.GS1CompositeBar.LinearComponentType = linearComponent;
7668

77-
// Choose a 2D component type (CC_A is a common choice for GS1 Composite).
69+
// Choose a 2D component type (CC_A is a common choice)
7870
generator.Parameters.Barcode.GS1CompositeBar.TwoDComponentType = TwoDComponentType.CC_A;
7971

80-
// Optional: adjust visual dimensions.
81-
generator.Parameters.Barcode.XDimension.Pixels = 3f; // Width of a single module.
82-
generator.Parameters.Barcode.BarHeight.Pixels = 100f; // Height of the linear component.
72+
// Example visual settings
73+
generator.Parameters.Barcode.Pdf417.AspectRatio = 3f;
74+
generator.Parameters.Barcode.XDimension.Pixels = 3f;
75+
generator.Parameters.Barcode.BarHeight.Pixels = 100f;
8376

84-
// Save the generated barcode image to the specified path.
77+
// Save the barcode image to a PNG file
78+
string outputPath = "gs1composite.png";
8579
generator.Save(outputPath);
80+
Console.WriteLine($"GS1 Composite barcode saved to '{outputPath}'.");
8681
}
87-
88-
// --------------------------------------------------------------------
89-
// Inform the user of successful generation.
90-
// --------------------------------------------------------------------
91-
Console.WriteLine($"GS1 Composite barcode generated with linear component '{linearComponentType.GetType().Name}'. Saved to '{outputPath}'.");
9282
}
9383
}
Lines changed: 57 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,92 @@
1+
// Title: Benchmark DotCode barcode generation speed across encoding modes in parallel
2+
// Description: Demonstrates measuring the time required to generate DotCode barcodes using various encoding modes, running each mode concurrently.
3+
// Category-Description: This example belongs to the Aspose.BarCode generation performance category. It showcases the use of BarcodeGenerator, EncodeTypes, and DotCodeEncodeMode to create DotCode symbols. Developers often need to benchmark different encoding settings to choose the optimal configuration for high‑throughput applications, such as bulk label printing or real‑time scanning systems.
4+
// Prompt: Benchmark generation speed of DotCode barcodes using different encoding modes in parallel.
5+
// Tags: dotcode, benchmark, parallel, generation, aspnet, aspose.barcode, png
6+
17
using System;
28
using System.Collections.Generic;
39
using System.Diagnostics;
410
using System.IO;
511
using System.Threading.Tasks;
612
using Aspose.BarCode;
713
using Aspose.BarCode.Generation;
8-
using Aspose.BarCode.BarCodeRecognition;
9-
using Aspose.Drawing.Imaging;
1014

1115
/// <summary>
12-
/// Demonstrates benchmarking of different DotCode encoding modes using Aspose.BarCode.
13-
/// Generates a small number of barcodes per mode and measures execution time.
16+
/// Provides a benchmark for generating DotCode barcodes using various encoding modes in parallel.
1417
/// </summary>
1518
class Program
1619
{
17-
// Sample text to encode in each DotCode barcode.
18-
private const string SampleCodeText = "DOTCODE123";
19-
20-
// Number of barcodes generated per encoding mode (kept low for quick execution).
21-
private const int BarcodesPerMode = 5;
22-
2320
/// <summary>
24-
/// Entry point of the application. Sets up encoding modes and runs benchmarks in parallel.
21+
/// Entry point of the benchmark application.
2522
/// </summary>
2623
static void Main()
2724
{
28-
// Mapping of mode names to configuration actions for the BarcodeGenerator.
29-
var modes = new Dictionary<string, Action<BarcodeGenerator>>
25+
// Define the DotCode encoding modes to benchmark.
26+
var modes = new Dictionary<string, DotCodeEncodeMode>
3027
{
31-
{
32-
"Auto", generator =>
33-
{
34-
// Default mode; no additional configuration required.
35-
}
36-
},
37-
{
38-
"Binary", generator =>
39-
{
40-
// Configure generator for Binary encoding.
41-
generator.Parameters.Barcode.DotCode.DotCodeEncodeMode = DotCodeEncodeMode.Binary;
42-
}
43-
},
44-
{
45-
"ECI", generator =>
46-
{
47-
// Configure generator for ECI encoding with UTF-8 character set.
48-
generator.Parameters.Barcode.DotCode.DotCodeEncodeMode = DotCodeEncodeMode.ECI;
49-
generator.Parameters.Barcode.DotCode.ECIEncoding = ECIEncodings.UTF8;
50-
}
51-
},
52-
{
53-
"Extended", generator =>
54-
{
55-
// Configure generator for Extended encoding.
56-
generator.Parameters.Barcode.DotCode.DotCodeEncodeMode = DotCodeEncodeMode.Extended;
57-
}
58-
}
28+
{ "Auto", DotCodeEncodeMode.Auto },
29+
{ "Binary", DotCodeEncodeMode.Binary },
30+
{ "ECI", DotCodeEncodeMode.ECI },
31+
{ "Extended", DotCodeEncodeMode.Extended },
32+
{ "ExtendedCodetext", DotCodeEncodeMode.ExtendedCodetext }
5933
};
6034

61-
// Launch a benchmark task for each mode concurrently.
35+
// List to store benchmark results (mode name and elapsed time in milliseconds).
36+
var results = new List<(string Mode, long ElapsedMs)>();
37+
// Collection of tasks that will run each mode concurrently.
6238
var tasks = new List<Task>();
39+
40+
// Create a task for each encoding mode.
6341
foreach (var kvp in modes)
6442
{
6543
string modeName = kvp.Key;
66-
Action<BarcodeGenerator> configure = kvp.Value;
44+
DotCodeEncodeMode mode = kvp.Value;
6745

68-
tasks.Add(Task.Run(() => BenchmarkMode(modeName, configure)));
69-
}
46+
tasks.Add(Task.Run(() =>
47+
{
48+
var stopwatch = Stopwatch.StartNew();
7049

71-
// Wait for all benchmark tasks to complete.
72-
Task.WaitAll(tasks.ToArray());
50+
// Generate a small number of barcodes for the current mode.
51+
for (int i = 0; i < 5; i++)
52+
{
53+
using (var generator = new BarcodeGenerator(EncodeTypes.DotCode, "Sample"))
54+
{
55+
// Apply the specific DotCode encoding mode.
56+
generator.Parameters.Barcode.DotCode.DotCodeEncodeMode = mode;
7357

74-
Console.WriteLine("Benchmark completed.");
75-
}
58+
// Set ECI encoding when the mode requires it.
59+
if (mode == DotCodeEncodeMode.ECI)
60+
{
61+
generator.Parameters.Barcode.DotCode.ECIEncoding = ECIEncodings.UTF8;
62+
}
7663

77-
/// <summary>
78-
/// Generates a set of barcodes for a specific encoding mode and measures the time taken.
79-
/// </summary>
80-
/// <param name="modeName">Human‑readable name of the encoding mode.</param>
81-
/// <param name="configure">Action that applies mode‑specific settings to a BarcodeGenerator instance.</param>
82-
private static void BenchmarkMode(string modeName, Action<BarcodeGenerator> configure)
83-
{
84-
var stopwatch = new Stopwatch();
85-
stopwatch.Start();
64+
// Save the barcode image to a memory stream (no file I/O).
65+
using (var ms = new MemoryStream())
66+
{
67+
generator.Save(ms, BarCodeImageFormat.Png);
68+
}
69+
}
70+
}
8671

87-
// Generate the defined number of barcodes for the given mode.
88-
for (int i = 0; i < BarcodesPerMode; i++)
89-
{
90-
// Create a fresh generator for each barcode to avoid residual state.
91-
using (var generator = new BarcodeGenerator(EncodeTypes.DotCode, SampleCodeText))
92-
{
93-
// Apply the mode‑specific configuration.
94-
configure(generator);
72+
stopwatch.Stop();
9573

96-
// Save the barcode image to a memory stream (no disk I/O required for benchmarking).
97-
using (var ms = new MemoryStream())
74+
// Record the elapsed time for this mode in a thread‑safe manner.
75+
lock (results)
9876
{
99-
generator.Save(ms, BarCodeImageFormat.Png);
100-
// The memory stream is discarded after this point; it could be used for further processing if needed.
77+
results.Add((modeName, stopwatch.ElapsedMilliseconds));
10178
}
102-
}
79+
}));
10380
}
10481

105-
stopwatch.Stop();
106-
Console.WriteLine($"{modeName} mode: Generated {BarcodesPerMode} barcodes in {stopwatch.ElapsedMilliseconds} ms");
82+
// Wait for all parallel tasks to finish.
83+
Task.WaitAll(tasks.ToArray());
84+
85+
// Output the benchmark results to the console.
86+
Console.WriteLine("DotCode generation benchmark (5 barcodes per mode):");
87+
foreach (var result in results)
88+
{
89+
Console.WriteLine($"{result.Mode}: {result.ElapsedMs} ms");
90+
}
10791
}
10892
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
1+
// Title: Generate GS1 Composite barcode with CC_C component and custom column count
2+
// Description: Demonstrates configuring the PDF417 (CC_C) component of a GS1 Composite barcode to use 30 columns, then saving the image.
3+
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, focusing on GS1 Composite symbology. It shows how to set linear and 2D component types, adjust PDF417 parameters, and customize common barcode settings. Developers creating composite barcodes for packaging, logistics, or retail can use these APIs to meet specification requirements.
4+
// Prompt: Configure column count for CC_C 2D component to 30 columns when generating a GS1 Composite barcode.
5+
// Tags: gs1 composite, pdf417, column count, barcode generation, aspose.barcode, csharp
6+
17
using System;
28
using Aspose.BarCode;
39
using Aspose.BarCode.Generation;
410

511
/// <summary>
6-
/// Demonstrates generation of a GS1 Composite barcode with linear and 2D components using Aspose.BarCode.
12+
/// Demonstrates generating a GS1 Composite barcode with a CC_C (PDF417) 2D component
13+
/// configured to 30 columns, and saving it as an image file.
714
/// </summary>
815
class Program
916
{
1017
/// <summary>
11-
/// Entry point of the application. Generates and saves a GS1 Composite barcode image.
18+
/// Entry point of the example. Creates a BarcodeGenerator, configures parameters,
19+
/// and saves the resulting barcode image.
1220
/// </summary>
1321
static void Main()
1422
{
15-
// Define the GS1 Composite barcode text.
16-
// Linear part (GS1-128) and 2D part (PDF417) are separated by '|'.
23+
// Define the GS1 Composite code text: linear part and 2D part separated by '|'
1724
string codetext = "(01)03212345678906|(21)A12345678";
1825

19-
// Initialize the barcode generator for GS1 Composite Bar with the specified code text.
26+
// Initialize the generator for GS1 Composite barcode with the specified code text
2027
using (var generator = new BarcodeGenerator(EncodeTypes.GS1CompositeBar, codetext))
2128
{
22-
// Specify the linear component type (GS1 Code 128).
29+
// Set the linear component to GS1 Code128
2330
generator.Parameters.Barcode.GS1CompositeBar.LinearComponentType = EncodeTypes.GS1Code128;
2431

25-
// Specify the 2D component type (CC_C, which uses PDF417).
32+
// Set the 2D component type to CC_C (PDF417)
2633
generator.Parameters.Barcode.GS1CompositeBar.TwoDComponentType = TwoDComponentType.CC_C;
2734

28-
// Set the number of columns for the PDF417 component to 30.
35+
// Configure PDF417 to use 30 columns, as required for the CC_C component
2936
generator.Parameters.Barcode.Pdf417.Columns = 30;
3037

31-
// Save the generated barcode image to a PNG file.
32-
generator.Save("gs1_composite_ccc.png");
33-
}
38+
// Optional: improve visual quality by adjusting size and resolution
39+
generator.Parameters.Barcode.XDimension.Pixels = 2f; // module width
40+
generator.Parameters.Barcode.BarHeight.Pixels = 100f; // linear component height
41+
generator.Parameters.Resolution = 96; // image DPI
3442

35-
// Inform the user that the barcode has been generated.
36-
Console.WriteLine("Barcode generated and saved as gs1_composite_ccc.png");
43+
// Save the generated barcode to a PNG file
44+
string outputPath = "gs1_composite_cc_c.png";
45+
generator.Save(outputPath);
46+
Console.WriteLine($"Barcode saved to {outputPath}");
47+
}
3748
}
3849
}

0 commit comments

Comments
 (0)