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,34 +1,40 @@
// Title: Custom Text Color for Barcode Generation
// Description: Demonstrates how to set a custom color for the human‑readable text of a barcode while keeping the bar and background colors at their default values.
// Title: Apply custom text color to a barcode (Code128)
// Description: Demonstrates how to set a custom color for the human‑readable text of a barcode while keeping bar and background colors at their defaults.
// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating the use of BarcodeGenerator, EncodeTypes, and CodeTextParameters to customize barcode appearance. Developers often need to modify text color for branding or visual integration while preserving standard bar colors. The snippet shows typical steps for creating, customizing, and saving a barcode image.
// Prompt: Apply a custom text color to a barcode while leaving bar and background colors at defaults.
// Tags: barcode, code128, textcolor, aspose.barcode, image generation
// Tags: barcode, code128, text color, png, aspose.barcode, generation, colortext

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

/// <summary>
/// Example program that generates a Code128 barcode with a custom text color.
/// </summary>
class Program
namespace BarcodeExample
{
/// <summary>
/// Entry point of the application. Generates a barcode image with red human‑readable text.
/// Generates a Code128 barcode with a custom text color while leaving bar and background colors at their defaults.
/// </summary>
static void Main()
class Program
{
// Initialize a barcode generator with default bar and background colors
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890"))
/// <summary>
/// Entry point of the example. Creates the barcode, applies the text color, saves the image, and writes the output path to the console.
/// </summary>
static void Main()
{
// Set the human‑readable text color to red (bars and background stay default)
generator.Parameters.Barcode.CodeTextParameters.Color = Aspose.Drawing.Color.Red;
// Define the output file name and location
string outputPath = "custom_text_color_barcode.png";

// Save the generated barcode as a PNG file
generator.Save("barcode.png");
}
// Initialize the barcode generator for Code128 with the desired data
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
{
// Set a custom color (blue) for the human‑readable text only
generator.Parameters.Barcode.CodeTextParameters.Color = Color.Blue;

// Save the barcode image; bar and background colors remain unchanged (defaults)
generator.Save(outputPath);
}

// Inform the user that the image has been saved
Console.WriteLine("Barcode image saved as barcode.png");
// Inform the user where the barcode image was saved
Console.WriteLine($"Barcode saved to {outputPath}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Title: Custom Colored Barcode Generation and Pixel Comparison
// Description: Demonstrates generating Code128 barcodes with different custom colors and programmatically comparing their visual differences.
// Title: Custom Color Comparison for Code128 Barcodes
// Description: Demonstrates how to generate a Code128 barcode with default and custom colors, then programmatically compare the visual differences.
// Category-Description: This example belongs to the Aspose.BarCode image customization and analysis category. It showcases the use of BarcodeGenerator, setting BarColor and BackColor, and performing pixel‑by‑pixel image comparison with Aspose.Drawing. Developers often need to customize barcode appearance for branding and verify visual changes automatically.
// Prompt: Apply different custom colors to the same barcode type and compare visual differences programmatically.
// Tags: barcode, code128, color, comparison, png, aspose.barcode, aspose.drawing
// Tags: barcode, code128, color customization, image comparison, aspose.barcode, aspose.drawing, png

using System;
using System.IO;
Expand All @@ -11,89 +12,81 @@
using Aspose.Drawing.Imaging;

/// <summary>
/// Generates two Code128 barcodes with distinct color schemes,
/// saves them as PNG files, and compares the images pixel by pixel
/// to quantify visual differences.
/// Demonstrates generating a Code128 barcode with default and custom colors,
/// saving them as PNG files, and comparing the images pixel by pixel.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Entry point of the example. Creates output folder, generates two barcodes,
/// and outputs the percentage of differing pixels.
/// </summary>
static void Main()
{
// --------------------------------------------------------------------
// Prepare output directory
// --------------------------------------------------------------------
string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes");
if (!Directory.Exists(outputDir))
// Define the folder where barcode images will be saved
string outputFolder = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes");
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputDir);
Directory.CreateDirectory(outputFolder);
}

// --------------------------------------------------------------------
// Define common barcode data and target file paths
// --------------------------------------------------------------------
string codeText = "Sample123";
string fileRed = Path.Combine(outputDir, "code_red.png");
string fileBlue = Path.Combine(outputDir, "code_blue.png");
// File paths for the default‑color and custom‑color barcode images
string defaultPath = Path.Combine(outputFolder, "barcode_default.png");
string customPath = Path.Combine(outputFolder, "barcode_custom.png");

// --------------------------------------------------------------------
// Generate barcode with red bars on a white background
// --------------------------------------------------------------------
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, codeText))
// ------------------------------------------------------------
// Generate barcode with default colors (black bars on white background)
// ------------------------------------------------------------
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
{
generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Red; // Set bar color to red
generator.Parameters.BackColor = Aspose.Drawing.Color.White; // Set background to white
generator.Save(fileRed, BarCodeImageFormat.Png); // Save as PNG
generator.Save(defaultPath, BarCodeImageFormat.Png);
}

// --------------------------------------------------------------------
// Generate barcode with blue bars on a light gray background
// --------------------------------------------------------------------
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, codeText))
// ------------------------------------------------------------
// Generate barcode with custom colors (red bars on yellow background)
// ------------------------------------------------------------
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123"))
{
generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue; // Set bar color to blue
generator.Parameters.BackColor = Aspose.Drawing.Color.LightGray; // Set background to light gray
generator.Save(fileBlue, BarCodeImageFormat.Png); // Save as PNG
generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Red; // Set bar (foreground) color
generator.Parameters.BackColor = Aspose.Drawing.Color.Yellow; // Set background color
generator.Save(customPath, BarCodeImageFormat.Png);
}

// --------------------------------------------------------------------
// Load the generated images for pixel-by-pixel comparison
// --------------------------------------------------------------------
using (var bmpRed = new Bitmap(fileRed))
using (var bmpBlue = new Bitmap(fileBlue))
// ------------------------------------------------------------
// Load both images and compare them pixel by pixel
// ------------------------------------------------------------
using (var imgDefault = (Bitmap)Aspose.Drawing.Image.FromFile(defaultPath))
using (var imgCustom = (Bitmap)Aspose.Drawing.Image.FromFile(customPath))
{
// Verify that both images share the same dimensions
if (bmpRed.Width != bmpBlue.Width || bmpRed.Height != bmpBlue.Height)
// Verify that the images share the same dimensions before comparison
if (imgDefault.Width != imgCustom.Width || imgDefault.Height != imgCustom.Height)
{
Console.WriteLine("Images have different dimensions; cannot compare.");
Console.WriteLine("Images have different dimensions; cannot compare pixel by pixel.");
return;
}

int diffPixels = 0; // Counter for differing pixels
int width = imgDefault.Width;
int height = imgDefault.Height;
long diffPixelCount = 0;

// Iterate over each pixel coordinate
for (int y = 0; y < bmpRed.Height; y++)
// Iterate over each pixel and count differences
for (int y = 0; y < height; y++)
{
for (int x = 0; x < bmpRed.Width; x++)
for (int x = 0; x < width; x++)
{
// Compare pixel colors; increment counter if they differ
if (bmpRed.GetPixel(x, y) != bmpBlue.GetPixel(x, y))
int argbDefault = imgDefault.GetPixel(x, y).ToArgb();
int argbCustom = imgCustom.GetPixel(x, y).ToArgb();
if (argbDefault != argbCustom)
{
diffPixels++;
diffPixelCount++;
}
}
}

// Output the total number of differing pixels
Console.WriteLine($"Total differing pixels between red and blue barcodes: {diffPixels}");
// Output comparison results
Console.WriteLine($"Total pixels: {width * height}");
Console.WriteLine($"Different pixels: {diffPixelCount}");
Console.WriteLine($"Difference percentage: {diffPixelCount * 100.0 / (width * height):F2}%");
}

// --------------------------------------------------------------------
// Inform the user where the barcode images have been saved
// --------------------------------------------------------------------
Console.WriteLine($"Red barcode saved to: {fileRed}");
Console.WriteLine($"Blue barcode saved to: {fileBlue}");
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,45 @@
// Title: Independent Caption Colors in a Barcode
// Description: Demonstrates how to set different colors for top and bottom captions when generating a barcode image using Aspose.BarCode.
// Title: Independent Top and Bottom Caption Colors in a Barcode Image
// Description: Demonstrates how to set different colors for the top and bottom captions of a barcode using Aspose.BarCode.
// Category-Description: This example belongs to the Aspose.BarCode image generation category, illustrating the use of BarcodeGenerator, EncodeTypes, and caption parameters. Developers often need to customize barcode appearance, such as adding distinct captions with separate styling, for branding or informational purposes. The snippet shows typical API calls for configuring caption visibility, text, font, and color before saving the image.
// Prompt: Change the caption color independently for top and bottom captions in the same barcode image.
// Tags: barcode, caption, color, code128, aspose.barcode, image
// Tags: code128, caption-color, png, barcodegenerator, parameters

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

/// <summary>
/// Example program that creates a Code128 barcode with separate top and bottom captions,
/// each having its own color and font settings.
/// Demonstrates setting independent colors for top and bottom captions in a barcode image.
/// </summary>
class Program
{
/// <summary>
/// Entry point. Generates the barcode image and saves it to disk.
/// Generates a Code128 barcode with distinct top and bottom caption colors and saves it as a PNG file.
/// </summary>
static void Main()
{
// Initialize a barcode generator for Code128 with the sample code text "123456"
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456"))
{
// ----- Configure the top caption (appears above the barcode) -----
generator.Parameters.CaptionAbove.Visible = true; // Make the top caption visible
generator.Parameters.CaptionAbove.Text = "Top Caption"; // Set caption text
generator.Parameters.CaptionAbove.TextColor = Color.Red; // Assign independent color (red)
generator.Parameters.CaptionAbove.Visible = true; // Make the top caption visible
generator.Parameters.CaptionAbove.Text = "Top Caption"; // Set caption text
generator.Parameters.CaptionAbove.TextColor = Color.Blue; // Set independent color for top caption
generator.Parameters.CaptionAbove.Font.FamilyName = "Arial"; // Choose font family
generator.Parameters.CaptionAbove.Font.Size.Point = 12f; // Set font size

// ----- Configure the bottom caption (appears below the barcode) -----
generator.Parameters.CaptionBelow.Visible = true; // Make the bottom caption visible
generator.Parameters.CaptionBelow.Text = "Bottom Caption"; // Set caption text
generator.Parameters.CaptionBelow.TextColor = Color.Blue; // Assign independent color (blue)
generator.Parameters.CaptionBelow.Visible = true; // Make the bottom caption visible
generator.Parameters.CaptionBelow.Text = "Bottom Caption"; // Set caption text
generator.Parameters.CaptionBelow.TextColor = Color.Green; // Set independent color for bottom caption
generator.Parameters.CaptionBelow.Font.FamilyName = "Arial"; // Choose font family
generator.Parameters.CaptionBelow.Font.Size.Point = 12f; // Set font size

// ----- Optional: set distinct fonts for each caption -----
generator.Parameters.CaptionAbove.Font.FamilyName = "Arial"; // Font family for top caption
generator.Parameters.CaptionAbove.Font.Size.Point = 12f; // Font size for top caption
generator.Parameters.CaptionBelow.Font.FamilyName = "Times New Roman"; // Font family for bottom caption
generator.Parameters.CaptionBelow.Font.Size.Point = 10f; // Font size for bottom caption

// Save the generated barcode image (including captions) to a PNG file
string outputPath = "barcode_with_captions.png";
// ----- Save the generated barcode image to a PNG file -----
string outputPath = "barcode.png";
generator.Save(outputPath);
Console.WriteLine($"Barcode image saved to: {outputPath}");
Console.WriteLine($"Barcode image saved to '{outputPath}'.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
// Title: Change DataMatrix barcode text color to orange
// Description: Demonstrates how to set the human‑readable text color of a DataMatrix barcode to orange while preserving the default background.
// Prompt: Change only the text color of a DataMatrix barcode to orange while keeping default background.
// Tags: datamatrix, color, textcolor, png, aspose.barcode, generation

// Description: Demonstrates how to set the human‑readable text color of a DataMatrix barcode to orange while keeping the default background.
// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to customize visual properties of barcodes. It uses the BarcodeGenerator class together with EncodeTypes, BarCodeImageFormat, and CodeTextParameters to modify text appearance. Developers often need to adjust colors for branding or UI integration, and this snippet shows the typical steps for such customizations in C#.
/// <summary>
/// Shows how to generate a DataMatrix barcode and change only its text color to orange.
/// </summary>
using System;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.Drawing;

/// <summary>
/// Example program that generates a DataMatrix barcode with orange text color.
/// </summary>
class Program
namespace BarcodeExample
{
/// <summary>
/// Entry point. Generates a DataMatrix barcode, sets the code text color to orange, and saves as PNG.
/// Entry point for the barcode generation example.
/// </summary>
static void Main()
class Program
{
// Define the output file path for the generated barcode image
string outputPath = "datamatrix.png";

// Initialize a DataMatrix barcode generator with sample text
using (var generator = new BarcodeGenerator(EncodeTypes.DataMatrix, "Sample Text"))
/// <summary>
/// Generates a DataMatrix barcode with orange human‑readable text and saves it as a PNG file.
/// </summary>
static void Main()
{
// Set only the human‑readable text (code text) color to orange
generator.Parameters.Barcode.CodeTextParameters.Color = Color.Orange;
// Define the output file path for the generated barcode image.
string outputPath = "datamatrix.png";

// Save the barcode image as PNG; background remains the default color
generator.Save(outputPath, BarCodeImageFormat.Png);
}
// Initialize the barcode generator for DataMatrix with sample content.
using (var generator = new BarcodeGenerator(EncodeTypes.DataMatrix, "Sample Text"))
{
// Set the color of the human‑readable text to orange.
generator.Parameters.Barcode.CodeTextParameters.Color = Color.Orange;

// Output a confirmation message with the saved file location
Console.WriteLine($"DataMatrix barcode saved to {outputPath}");
// Save the barcode image in PNG format to the specified path.
generator.Save(outputPath, BarCodeImageFormat.Png);
}

// Inform the user where the barcode image has been saved.
Console.WriteLine($"DataMatrix barcode saved to {outputPath}");
}
}
}
Loading
Loading