Skip to content

Commit b7273b5

Browse files
Merge pull request #79 from aspose-barcode/agent/maxicode-barcode/2026-07-07-165635
feat: Add Aspose.BarCode .NET C# Examples — Maxicode Barcode
2 parents dde84ea + cd02fbc commit b7273b5

34 files changed

Lines changed: 1533 additions & 1406 deletions

File tree

maxicode-barcode/apply-custom-background-color-to-maxicode-barcode-and-verify-that-decoding-remains-successful.cs

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,95 @@
1+
// Title: Custom Background Color for MaxiCode Barcode
2+
// Description: Demonstrates applying a custom background color to a MaxiCode barcode and confirming that it can still be decoded correctly.
3+
// Category-Description: This example belongs to the Aspose.BarCode generation and recognition category, focusing on MaxiCode symbology. It showcases the use of ComplexBarcodeGenerator to create a MaxiCode with custom visual settings, and BarCodeReader with ComplexCodetextReader to decode the generated image. Developers working with shipping, logistics, or inventory systems often need to customize barcode appearance while ensuring reliable scanning.
4+
// Prompt: Apply a custom background color to a MaxiCode barcode and verify that decoding remains successful.
5+
// Tags: maxicode, background color, barcode generation, barcode recognition, aspose.barcode, c#
6+
17
using System;
28
using System.IO;
3-
using Aspose.BarCode;
49
using Aspose.BarCode.Generation;
510
using Aspose.BarCode.BarCodeRecognition;
611
using Aspose.BarCode.ComplexBarcode;
712
using Aspose.Drawing;
813

914
/// <summary>
10-
/// Demonstrates generation and decoding of a MaxiCode barcode using Aspose.BarCode.
15+
/// Generates a MaxiCode barcode with a custom background color,
16+
/// saves it as an image, and verifies that the barcode can be decoded successfully.
1117
/// </summary>
1218
class Program
1319
{
1420
/// <summary>
15-
/// Entry point of the application.
16-
/// Generates a MaxiCode (Mode 2) with a custom background, saves it to a memory stream,
17-
/// and then reads/decodes the barcode from that stream.
21+
/// Entry point of the example. Creates a MaxiCode with a light‑yellow background,
22+
/// writes it to a PNG file, and then reads the file back to confirm decoding.
1823
/// </summary>
1924
static void Main()
2025
{
21-
// ------------------------------------------------------------
22-
// Prepare MaxiCode codetext (Mode 2 with a standard second message)
23-
// ------------------------------------------------------------
24-
var maxiCodeCodetext = new MaxiCodeCodetextMode2
26+
// Define the output file name.
27+
string outputPath = "maxicode.png";
28+
29+
// Prepare MaxiCode codetext (Mode 2 example) with postal code, country code, and service category.
30+
var maxiCode = new MaxiCodeCodetextMode2
2531
{
26-
PostalCode = "524032140", // 9‑digit US postal code
27-
CountryCode = 56, // USA
28-
ServiceCategory = 999
32+
PostalCode = "524032140", // 9‑digit US postal code
33+
CountryCode = 56, // USA
34+
ServiceCategory = 999 // Example service category
2935
};
3036

31-
// Create the optional second message for the MaxiCode
37+
// Add a second message to the MaxiCode.
3238
var secondMessage = new MaxiCodeStandardSecondMessage
3339
{
3440
Message = "Sample MaxiCode"
3541
};
36-
maxiCodeCodetext.SecondMessage = secondMessage;
42+
maxiCode.SecondMessage = secondMessage;
43+
44+
// Generate the MaxiCode barcode with a custom background color.
45+
using (var complexGenerator = new ComplexBarcodeGenerator(maxiCode))
46+
{
47+
// Set background to light yellow (RGB 255,255,224).
48+
complexGenerator.Parameters.BackColor = Aspose.Drawing.Color.FromArgb(255, 255, 224);
49+
50+
// Create the barcode image.
51+
using (var bitmap = complexGenerator.GenerateBarCodeImage())
52+
{
53+
// Save the image as PNG.
54+
bitmap.Save(outputPath, Aspose.Drawing.Imaging.ImageFormat.Png);
55+
}
56+
}
3757

38-
// ------------------------------------------------------------
39-
// Generate the barcode with a custom background color
40-
// ------------------------------------------------------------
41-
using (var generator = new ComplexBarcodeGenerator(maxiCodeCodetext))
58+
// Verify that the image file was created.
59+
if (!File.Exists(outputPath))
4260
{
43-
// Set a custom background (e.g., light yellow)
44-
generator.Parameters.BackColor = Color.FromArgb(255, 255, 224); // LightYellow
61+
Console.WriteLine("Failed to create barcode image.");
62+
return;
63+
}
4564

46-
// --------------------------------------------------------
47-
// Save the generated barcode to a memory stream in PNG format
48-
// --------------------------------------------------------
49-
using (var ms = new MemoryStream())
65+
// Read and decode the generated MaxiCode barcode.
66+
using (var reader = new BarCodeReader(outputPath, DecodeType.MaxiCode))
67+
{
68+
foreach (var result in reader.ReadBarCodes())
5069
{
51-
generator.Save(ms, BarCodeImageFormat.Png);
52-
ms.Position = 0; // Reset stream position for reading
70+
// Decode the raw codetext using the appropriate MaxiCode mode.
71+
var decoded = ComplexCodetextReader.TryDecodeMaxiCode(
72+
result.Extended.MaxiCode.MaxiCodeMode,
73+
result.CodeText);
5374

54-
// --------------------------------------------------------
55-
// Read and decode the barcode from the generated image stream
56-
// --------------------------------------------------------
57-
using (var reader = new BarCodeReader(ms, DecodeType.MaxiCode))
75+
// Check if decoding produced the expected Mode 2 codetext.
76+
if (decoded is MaxiCodeCodetextMode2 decodedMode2)
5877
{
59-
var results = reader.ReadBarCodes();
78+
Console.WriteLine("Decoding successful:");
79+
Console.WriteLine($"Postal Code: {decodedMode2.PostalCode}");
80+
Console.WriteLine($"Country Code: {decodedMode2.CountryCode}");
81+
Console.WriteLine($"Service Category: {decodedMode2.ServiceCategory}");
6082

61-
// Check if any barcodes were detected
62-
if (results.Length == 0)
83+
// Output the second message if present.
84+
if (decodedMode2.SecondMessage is MaxiCodeStandardSecondMessage stdMsg)
6385
{
64-
Console.WriteLine("No barcode detected.");
65-
}
66-
else
67-
{
68-
// Iterate through all detected barcodes
69-
foreach (var result in results)
70-
{
71-
// Verify that decoding succeeded (non‑empty CodeText)
72-
if (!string.IsNullOrEmpty(result.CodeText))
73-
{
74-
Console.WriteLine("Decoding successful.");
75-
Console.WriteLine("Decoded CodeText: " + result.CodeText);
76-
}
77-
else
78-
{
79-
Console.WriteLine("Decoding failed: empty CodeText.");
80-
}
81-
}
86+
Console.WriteLine($"Message: {stdMsg.Message}");
8287
}
8388
}
89+
else
90+
{
91+
Console.WriteLine("Decoding failed or unexpected codetext type.");
92+
}
8493
}
8594
}
8695
}
Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,57 @@
1+
// Title: Apply custom foreground color to a MaxiCode Mode 2 barcode
2+
// Description: This example creates a MaxiCode Mode 2 barcode, sets a custom bar (foreground) color, and saves it as a PNG image. It demonstrates how to customize the visual appearance of complex barcodes using Aspose.BarCode.
3+
// Category-Description: The sample belongs to the Aspose.BarCode complex barcode generation category, where developers work with multi‑message symbologies such as MaxiCode. It showcases the use of ComplexBarcodeGenerator, MaxiCodeCodetextMode2, and related parameter classes to configure barcode data and appearance. Typical scenarios include shipping labels, parcel tracking, and logistics applications that require colored MaxiCode symbols.
4+
// Prompt: Apply a custom foreground color to a MaxiCode Mode 2 barcode using the generator's ForeColor property.
5+
// Tags: maxicode, color, generation, png, aspose.barcode, complexbarcodegenerator, barcode
6+
17
using System;
28
using System.IO;
39
using Aspose.BarCode;
410
using Aspose.BarCode.Generation;
511
using Aspose.BarCode.ComplexBarcode;
612
using Aspose.Drawing;
13+
using Aspose.Drawing.Imaging;
714

815
/// <summary>
9-
/// Demonstrates generation of a MaxiCode Mode 2 barcode using Aspose.BarCode.
16+
/// Demonstrates applying a custom foreground color to a MaxiCode Mode 2 barcode and saving it as PNG.
1017
/// </summary>
1118
class Program
1219
{
1320
/// <summary>
14-
/// Entry point of the application. Creates a MaxiCode codetext, configures the generator,
15-
/// and saves the resulting barcode image to a file.
21+
/// Entry point that builds the MaxiCode data, configures the barcode color, generates the image, and writes it to disk.
1622
/// </summary>
1723
static void Main()
1824
{
19-
// Initialize MaxiCode codetext for Mode 2 with required fields.
20-
var maxiCode = new MaxiCodeCodetextMode2
25+
// Prepare MaxiCode Mode 2 codetext with required fields
26+
var maxiCodeCodetext = new MaxiCodeCodetextMode2
2127
{
22-
PostalCode = "524032140", // 9‑digit postal code
23-
CountryCode = 56, // Numeric country identifier
24-
ServiceCategory = 999 // Service category value
28+
PostalCode = "524032140", // 9‑digit US postal code
29+
CountryCode = 56, // USA numeric country code
30+
ServiceCategory = 999 // Example service category
2531
};
2632

27-
// Create the optional second message and assign it to the codetext.
33+
// Create and assign the standard second message
2834
var secondMessage = new MaxiCodeStandardSecondMessage
2935
{
30-
Message = "Test message"
36+
Message = "Sample MaxiCode"
3137
};
32-
maxiCode.SecondMessage = secondMessage;
38+
maxiCodeCodetext.SecondMessage = secondMessage;
3339

34-
// Use ComplexBarcodeGenerator to render the MaxiCode barcode.
35-
using (var generator = new ComplexBarcodeGenerator(maxiCode))
40+
// Initialize the generator with the prepared codetext
41+
using (var generator = new ComplexBarcodeGenerator(maxiCodeCodetext))
3642
{
37-
// Set the color of the barcode bars (foreground). No ForeColor property exists.
38-
generator.Parameters.Barcode.BarColor = Color.Red;
43+
// Set a custom foreground (bar) color for the barcode
44+
generator.Parameters.Barcode.BarColor = Color.Blue;
45+
46+
// Generate the barcode image in memory
47+
generator.GenerateBarCodeImage();
3948

40-
// Define output file name and format, then save the image.
41-
string outputPath = "maxicode_mode2.png";
49+
// Define output file path and save the image as PNG
50+
const string outputPath = "maxicode_mode2.png";
4251
generator.Save(outputPath, BarCodeImageFormat.Png);
4352

44-
// Output the full path of the saved file for verification.
45-
Console.WriteLine($"Barcode saved to: {Path.GetFullPath(outputPath)}");
53+
// Inform the user where the file was saved
54+
Console.WriteLine($"Barcode saved to {Path.GetFullPath(outputPath)}");
4655
}
4756
}
4857
}

0 commit comments

Comments
 (0)