diff --git a/barcode-color-customization/apply-custom-text-color-to-barcode-while-leaving-bar-and-background-colors-at-defaults.cs b/barcode-color-customization/apply-custom-text-color-to-barcode-while-leaving-bar-and-background-colors-at-defaults.cs index 372de0a..2377087 100644 --- a/barcode-color-customization/apply-custom-text-color-to-barcode-while-leaving-bar-and-background-colors-at-defaults.cs +++ b/barcode-color-customization/apply-custom-text-color-to-barcode-while-leaving-bar-and-background-colors-at-defaults.cs @@ -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; -/// -/// Example program that generates a Code128 barcode with a custom text color. -/// -class Program +namespace BarcodeExample { /// - /// 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. /// - static void Main() + class Program { - // Initialize a barcode generator with default bar and background colors - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) + /// + /// Entry point of the example. Creates the barcode, applies the text color, saves the image, and writes the output path to the console. + /// + 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}"); + } } } \ No newline at end of file diff --git a/barcode-color-customization/apply-different-custom-colors-to-same-barcode-type-and-compare-visual-differences-programmatically.cs b/barcode-color-customization/apply-different-custom-colors-to-same-barcode-type-and-compare-visual-differences-programmatically.cs index c3166c0..55dbcb5 100644 --- a/barcode-color-customization/apply-different-custom-colors-to-same-barcode-type-and-compare-visual-differences-programmatically.cs +++ b/barcode-color-customization/apply-different-custom-colors-to-same-barcode-type-and-compare-visual-differences-programmatically.cs @@ -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; @@ -11,89 +12,81 @@ using Aspose.Drawing.Imaging; /// -/// 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. /// class Program { /// - /// Entry point of the application. + /// Entry point of the example. Creates output folder, generates two barcodes, + /// and outputs the percentage of differing pixels. /// 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}"); } } \ No newline at end of file diff --git a/barcode-color-customization/change-caption-color-independently-for-top-and-bottom-captions-in-same-barcode-image.cs b/barcode-color-customization/change-caption-color-independently-for-top-and-bottom-captions-in-same-barcode-image.cs index f237496..b952c8d 100644 --- a/barcode-color-customization/change-caption-color-independently-for-top-and-bottom-captions-in-same-barcode-image.cs +++ b/barcode-color-customization/change-caption-color-independently-for-top-and-bottom-captions-in-same-barcode-image.cs @@ -1,7 +1,8 @@ -// 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; @@ -9,13 +10,12 @@ using Aspose.Drawing; /// -/// 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. /// class Program { /// - /// 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. /// static void Main() { @@ -23,25 +23,23 @@ static void Main() 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}'."); } } } \ No newline at end of file diff --git a/barcode-color-customization/change-only-text-color-of-datamatrix-barcode-to-orange-while-keeping-default-background.cs b/barcode-color-customization/change-only-text-color-of-datamatrix-barcode-to-orange-while-keeping-default-background.cs index c918446..58e8f9b 100644 --- a/barcode-color-customization/change-only-text-color-of-datamatrix-barcode-to-orange-while-keeping-default-background.cs +++ b/barcode-color-customization/change-only-text-color-of-datamatrix-barcode-to-orange-while-keeping-default-background.cs @@ -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#. +/// +/// Shows how to generate a DataMatrix barcode and change only its text color to orange. +/// using System; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; -/// -/// Example program that generates a DataMatrix barcode with orange text color. -/// -class Program +namespace BarcodeExample { /// - /// Entry point. Generates a DataMatrix barcode, sets the code text color to orange, and saves as PNG. + /// Entry point for the barcode generation example. /// - 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")) + /// + /// Generates a DataMatrix barcode with orange human‑readable text and saves it as a PNG file. + /// + 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}"); + } } } \ No newline at end of file diff --git a/barcode-color-customization/create-barcode-with-custom-bar-background-text-and-caption-colors-in-single-generation-step.cs b/barcode-color-customization/create-barcode-with-custom-bar-background-text-and-caption-colors-in-single-generation-step.cs index 0d320b3..0b9d063 100644 --- a/barcode-color-customization/create-barcode-with-custom-bar-background-text-and-caption-colors-in-single-generation-step.cs +++ b/barcode-color-customization/create-barcode-with-custom-bar-background-text-and-caption-colors-in-single-generation-step.cs @@ -1,7 +1,8 @@ -// Title: Custom Barcode Generation with Colored Elements -// Description: Demonstrates creating a Code128 barcode with custom bar, background, text, and caption colors in a single generation step. +// Title: Generate a Code128 barcode with custom colors and caption +// Description: Demonstrates creating a barcode with custom bar, background, text, and caption colors in a single generation step. +// Category-Description: This example belongs to the Aspose.BarCode color customization category, showcasing how to use BarcodeGenerator, BarcodeParameters, and related classes to set bar, background, code text, and caption colors. Typical use cases include branding, UI integration, and printing where visual styling of barcodes is required. Developers often need to apply custom colors to match corporate design guidelines. // Prompt: Create a barcode with custom bar, background, text, and caption colors in a single generation step. -// Tags: barcode, code128, color, caption, aspose.barcode, png +// Tags: code128, color customization, png, barcodegenerator, parameters, caption using System; using Aspose.BarCode; @@ -9,39 +10,42 @@ using Aspose.Drawing; /// -/// Example program that generates a barcode with customized colors for the bars, -/// background, text, and an additional caption using Aspose.BarCode. +/// Demonstrates generating a Code128 barcode with custom colors and a caption. /// class Program { /// - /// Entry point of the application. Generates a barcode image with custom visual settings. + /// Entry point. Generates the barcode and saves it as a PNG file. /// static void Main() { - // Initialize the barcode generator with Code128 symbology and the desired data. - using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) + // Define the output file path for the generated barcode image. + string outputPath = "custom_barcode.png"; + + // Initialize a BarcodeGenerator for Code128 symbology with the sample text "123456". + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456")) { // Set the color of the barcode bars to blue. - generator.Parameters.Barcode.BarColor = Color.Blue; + generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue; // Set the background color of the image to yellow. - generator.Parameters.BackColor = Color.Yellow; + generator.Parameters.BackColor = Aspose.Drawing.Color.Yellow; - // Set the color of the human‑readable text (code text) to green. - generator.Parameters.Barcode.CodeTextParameters.Color = Color.Green; + // Set the human‑readable text (code text) color to green. + generator.Parameters.Barcode.CodeTextParameters.Color = Aspose.Drawing.Color.Green; // Enable and configure a caption displayed above the barcode. generator.Parameters.CaptionAbove.Visible = true; - generator.Parameters.CaptionAbove.Text = "Custom Caption"; + generator.Parameters.CaptionAbove.Text = "Sample Caption"; generator.Parameters.CaptionAbove.Font.FamilyName = "Arial"; generator.Parameters.CaptionAbove.Font.Size.Point = 12f; + generator.Parameters.CaptionAbove.TextColor = Aspose.Drawing.Color.Purple; - // Save the generated barcode as a PNG file. - generator.Save("custom_barcode.png"); + // Save the generated barcode image as a PNG file at the specified path. + generator.Save(outputPath, BarCodeImageFormat.Png); } - // Inform the user that the barcode has been generated. - Console.WriteLine("Barcode generated successfully."); + // Inform the user that the barcode has been successfully generated. + Console.WriteLine($"Barcode generated and saved to '{outputPath}'."); } } \ No newline at end of file diff --git a/barcode-color-customization/create-barcode-with-gradient-effect-by-alternating-bar-colors-across-multiple-saves.cs b/barcode-color-customization/create-barcode-with-gradient-effect-by-alternating-bar-colors-across-multiple-saves.cs index e6e8dc6..2e714dd 100644 --- a/barcode-color-customization/create-barcode-with-gradient-effect-by-alternating-bar-colors-across-multiple-saves.cs +++ b/barcode-color-customization/create-barcode-with-gradient-effect-by-alternating-bar-colors-across-multiple-saves.cs @@ -1,62 +1,62 @@ -// Title: Gradient Barcode with Alternating Bar Colors -// Description: Demonstrates creating a Code128 barcode and saving multiple images, each with a different bar color to achieve a gradient effect. -// Prompt: Create a barcode with a gradient effect by alternating bar colors across multiple saves. -// Tags: barcode, code128, gradient, alternating colors, png, aspose.barcode, aspose.drawing +// Title: Create Gradient Barcode with Multiple Saves +// Description: Generates a series of Code128 barcode images where the bar color transitions from red to blue, demonstrating a gradient effect across multiple files. +// Category-Description: This example belongs to the Aspose.BarCode generation category, showcasing how to use the BarcodeGenerator class with EncodeTypes, set barcode parameters, and apply custom colors. Typical use cases include creating visually distinct barcodes for branding or UI themes. Developers often need to adjust bar colors, dimensions, and export formats, which this snippet illustrates. +/// Prompt: Create a barcode with a gradient effect by alternating bar colors across multiple saves. +/// Tags: barcode, gradient, code128, image, png, aspose.barcode, generation using System; +using System.IO; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; /// -/// Demonstrates generating a Code128 barcode with alternating bar colors and saving each variation as a PNG image. +/// Demonstrates creating a series of barcode images with a color gradient effect. /// class Program { /// - /// Entry point. Generates barcode images with different bar colors to simulate a gradient effect. + /// Entry point that generates barcode images with interpolated colors and saves them as PNG files. /// static void Main() { - // Define the barcode text to encode - const string codeText = "1234567890"; - - // Define the set of colors that will be applied to the barcode bars - Color[] barColors = new Color[] + // Define output directory for generated images + string outputDir = "output"; + if (!Directory.Exists(outputDir)) { - Color.Red, - Color.Green, - Color.Blue, - Color.Orange, - Color.Purple - }; + Directory.CreateDirectory(outputDir); + } - // Initialize a barcode generator for the Code128 symbology - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, codeText)) - { - // Configure image size using interpolation mode for better scaling - generator.Parameters.AutoSizeMode = AutoSizeMode.Interpolation; - generator.Parameters.ImageWidth.Point = 300f; - generator.Parameters.ImageHeight.Point = 150f; + // Number of gradient steps (images) to create + int steps = 5; - // Loop through each color, apply it, and save the resulting image - for (int i = 0; i < barColors.Length; i++) + // Loop through each step to calculate color and generate barcode + for (int i = 0; i < steps; i++) + { + // Linear interpolation of RGB components from red (255,0,0) to blue (0,0,255) + int r = (int)(255 - (255.0 * i / (steps - 1))); + int g = 0; + int b = (int)(255.0 * i / (steps - 1)); + Aspose.Drawing.Color barColor = Aspose.Drawing.Color.FromArgb(r, g, b); + + // Initialize barcode generator for Code128 symbology with the text "Gradient" + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Gradient")) { - // Set the current bar color - generator.Parameters.Barcode.BarColor = barColors[i]; - - // Build a unique file name for each image - string fileName = $"barcode_{i + 1}.png"; - - // Save the barcode image in PNG format - generator.Save(fileName, BarCodeImageFormat.Png); - - // Output status to the console - Console.WriteLine($"Saved {fileName} with bar color {barColors[i].Name}"); + // Apply the interpolated bar color for this step + generator.Parameters.Barcode.BarColor = barColor; + + // Configure optional sizing parameters + generator.Parameters.AutoSizeMode = AutoSizeMode.None; + generator.Parameters.ImageWidth.Point = 300f; + generator.Parameters.ImageHeight.Point = 150f; + generator.Parameters.Barcode.XDimension.Point = 2f; + generator.Parameters.Barcode.BarHeight.Point = 50f; + + // Build file path and save the barcode image as PNG + string filePath = Path.Combine(outputDir, $"barcode_step_{i + 1}.png"); + generator.Save(filePath); + Console.WriteLine($"Saved: {filePath}"); } } - - // Indicate that all barcode images have been generated - Console.WriteLine("Barcode generation with alternating colors completed."); } } \ No newline at end of file diff --git a/barcode-color-customization/create-batch-process-that-reads-list-of-strings-and-outputs-png-barcodes-with-alternating-colors.cs b/barcode-color-customization/create-batch-process-that-reads-list-of-strings-and-outputs-png-barcodes-with-alternating-colors.cs index ff439f5..2bf509a 100644 --- a/barcode-color-customization/create-batch-process-that-reads-list-of-strings-and-outputs-png-barcodes-with-alternating-colors.cs +++ b/barcode-color-customization/create-batch-process-that-reads-list-of-strings-and-outputs-png-barcodes-with-alternating-colors.cs @@ -1,26 +1,28 @@ -// Title: Batch barcode generation with alternating colors -// Description: Demonstrates creating PNG barcodes from a list of strings, alternating bar colors for visual distinction. +// Title: Batch Generation of PNG Barcodes with Alternating Colors +// Description: Demonstrates how to generate a series of Code128 barcodes from a list of strings, saving each as a PNG file with alternating black and red colors. +// Category-Description: This example belongs to the Aspose.BarCode generation category, showcasing the use of BarcodeGenerator, EncodeTypes, and barcode parameter settings. Typical use cases include bulk barcode creation for inventory, shipping labels, or product catalogs where visual differentiation (e.g., alternating colors) is desired. Developers often need to automate barcode output to image files while customizing appearance via the Parameters API. // Prompt: Create a batch process that reads a list of strings and outputs PNG barcodes with alternating colors. -// Tags: barcode symbology, batch processing, png output, aspose.barcode, aspose.drawing +// Tags: barcode symbology, generation, png, color, aspose.barcode, batch processing using System; +using System.Collections.Generic; using System.IO; -using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; /// -/// Generates PNG barcodes for a predefined list of strings, alternating bar colors between blue and red. +/// Demonstrates batch creation of PNG barcodes with alternating colors using Aspose.BarCode. /// class Program { /// - /// Entry point. Iterates over sample values, creates a barcode for each, and saves it as a PNG file. + /// Entry point of the example. Generates Code128 barcodes from a predefined list, + /// saves each as a PNG file, and alternates the bar color between black and red. /// static void Main() { - // Define a sample list of strings to encode as barcodes - string[] values = new string[] + // Define a sample list of strings to encode as barcodes. + List codes = new List { "Sample001", "Sample002", @@ -29,35 +31,32 @@ static void Main() "Sample005" }; - // Determine the output directory (current working folder) - string outputDir = Directory.GetCurrentDirectory(); - - // Process each string in the list - for (int i = 0; i < values.Length; i++) + // Set up the output directory for generated barcode images. + string outputDir = "Barcodes"; + if (!Directory.Exists(outputDir)) { - // Choose bar color: even index -> Blue, odd index -> Red - Aspose.Drawing.Color barColor = (i % 2 == 0) ? Aspose.Drawing.Color.Blue : Aspose.Drawing.Color.Red; - - // Initialize the barcode generator for Code128 symbology with the current value - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, values[i])) - { - // Apply the selected bar color to the barcode - generator.Parameters.Barcode.BarColor = barColor; + Directory.CreateDirectory(outputDir); + } - // Optional: customize image dimensions if required - // generator.Parameters.ImageWidth.Point = 300f; - // generator.Parameters.ImageHeight.Point = 150f; + // Iterate over each code, generate a barcode, and save it with the appropriate color. + for (int i = 0; i < codes.Count; i++) + { + string codeText = codes[i]; + string filePath = Path.Combine(outputDir, $"barcode_{i + 1}.png"); - // Construct a unique file name for the output PNG - string fileName = $"barcode_{i + 1}.png"; - string filePath = Path.Combine(outputDir, fileName); + // Determine bar color: even index → Black, odd index → Red. + Aspose.Drawing.Color barColor = (i % 2 == 0) ? Aspose.Drawing.Color.Black : Aspose.Drawing.Color.Red; - // Save the generated barcode image as a PNG file - generator.Save(filePath); + // Create and configure the barcode generator. + using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Code128)) + { + generator.CodeText = codeText; // Set the text to encode. + generator.Parameters.Barcode.BarColor = barColor; // Apply the selected bar color. + generator.Save(filePath); // Save the barcode as a PNG file. } - // Inform the user that the barcode has been generated - Console.WriteLine($"Generated barcode for \"{values[i]}\" as {i + 1}.png"); + // Log the generation result to the console. + Console.WriteLine($"Generated barcode for \"{codeText}\" at \"{filePath}\" with color {(barColor == Aspose.Drawing.Color.Black ? "Black" : "Red")}"); } } } \ No newline at end of file diff --git a/barcode-color-customization/create-qr-code-with-green-background-and-black-bars-then-save-as-png.cs b/barcode-color-customization/create-qr-code-with-green-background-and-black-bars-then-save-as-png.cs index b93bf6e..d8565cf 100644 --- a/barcode-color-customization/create-qr-code-with-green-background-and-black-bars-then-save-as-png.cs +++ b/barcode-color-customization/create-qr-code-with-green-background-and-black-bars-then-save-as-png.cs @@ -1,35 +1,37 @@ -// Title: Generate QR Code with Green Background -// Description: Demonstrates creating a QR code with a green background and black bars, then saving it as a PNG file. +// Title: Generate QR Code with Green Background and Black Bars +// Description: Demonstrates creating a QR code with a green background and black foreground bars, then saving it as a PNG image. +// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to customize QR code appearance using BarcodeGenerator, EncodeTypes, and drawing parameters. Developers often need to adjust colors, sizes, and output formats for branding or UI integration, and this snippet shows the essential API calls for such tasks. // Prompt: Create a QR code with a green background and black bars, then save as PNG. -// Tags: qr, barcode, generation, png, aspose.barcode, aspose.drawing +// Tags: qr code, barcode generation, color customization, png output, aspose.barcode, aspose.drawing using System; -using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; /// -/// Example program that generates a QR code with a green background and black bars using Aspose.BarCode. +/// Demonstrates generating a QR code with custom colors and saving it as a PNG file. /// class Program { /// - /// Entry point. Generates the QR code and saves it as a PNG file. + /// Entry point of the example. Creates a QR code with a green background and black bars, then writes it to "qr_green.png". /// static void Main() { - // Initialize a QR code generator with the QR symbology. - using (var generator = new BarcodeGenerator(EncodeTypes.QR)) + // Initialize a QR code generator using the QR symbology + using (BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR)) { - // Define the data to encode in the QR code. - generator.CodeText = "Hello World"; + // Set the data to be encoded in the QR code + generator.CodeText = "Hello, Aspose!"; - // Configure visual appearance: black bars on a green background. - generator.Parameters.Barcode.BarColor = Color.Black; + // Apply a green background color to the image generator.Parameters.BackColor = Color.Green; - // Persist the generated QR code image as a PNG file. - generator.Save("qr_green_background.png"); + // Set the barcode (foreground) color to black + generator.Parameters.Barcode.BarColor = Color.Black; + + // Save the generated QR code as a PNG file in the current directory + generator.Save("qr_green.png"); } } } \ No newline at end of file diff --git a/barcode-color-customization/demonstrate-that-modifying-color-properties-after-calling-save-does-not-alter-already-saved-image.cs b/barcode-color-customization/demonstrate-that-modifying-color-properties-after-calling-save-does-not-alter-already-saved-image.cs index 7aed675..a194df1 100644 --- a/barcode-color-customization/demonstrate-that-modifying-color-properties-after-calling-save-does-not-alter-already-saved-image.cs +++ b/barcode-color-customization/demonstrate-that-modifying-color-properties-after-calling-save-does-not-alter-already-saved-image.cs @@ -1,55 +1,67 @@ -// Title: Demonstrate color change after saving barcode does not affect saved image -// Description: Shows that modifying the barcode bar color after calling Save creates a new image file with the new color while the previously saved file remains unchanged. +// Title: Demonstrate color immutability after saving barcode image +// Description: Shows that changing barcode colors after calling Save does not affect the previously saved image file. +// Category-Description: This example belongs to the Aspose.BarCode image generation category, illustrating how the BarcodeGenerator and related drawing classes (Aspose.Drawing, Aspose.Drawing.Imaging) are used to create, customize, and persist barcode images. Developers often need to generate multiple variants of a barcode with different visual styles while ensuring earlier files remain unchanged. The snippet demonstrates best‑practice handling of color properties and file output, a common requirement when producing batch‑processed barcodes for packaging, labeling, or inventory systems. // Prompt: Demonstrate that modifying color properties after calling Save does not alter the already saved image. -// Tags: barcode, color, save, aspose.barcode, png, generation +// Tags: code128, color, save, bitmap, aspose.barcode, aspose.drawing using System; +using System.IO; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; using Aspose.Drawing.Imaging; /// -/// Demonstrates that changing barcode color after saving does not modify the already saved image. +/// Example program that generates a Code128 barcode, saves it, changes its colors, +/// saves a second image, and then verifies that the first saved file remains unchanged. /// class Program { /// - /// Entry point that generates two barcode images with different colors and verifies they differ. + /// Entry point of the example. Executes the barcode generation, saves two images with + /// different color settings, and compares a pixel from each file to confirm independence. /// static void Main() { - // Define file paths for the two output images - const string firstImagePath = "barcode_red.png"; - const string secondImagePath = "barcode_blue.png"; + // Define absolute file paths for the original and modified barcode images. + string originalPath = Path.Combine(Directory.GetCurrentDirectory(), "barcode_original.png"); + string modifiedPath = Path.Combine(Directory.GetCurrentDirectory(), "barcode_modified.png"); - // Create a barcode generator for Code128 with sample text - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Demo123")) + // Create a BarcodeGenerator for Code128, set initial colors, and save the first image. + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Test123")) { - // Set the barcode bar color to Red and save the first image - generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Red; - generator.Save(firstImagePath); + // Set the barcode (foreground) color to red and the background to white. + generator.Parameters.Barcode.BarColor = Color.Red; + generator.Parameters.BackColor = Color.White; - // Change the bar color to Blue after the first save and save the second image - generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue; - generator.Save(secondImagePath); + // Persist the first image to disk. + generator.Save(originalPath, BarCodeImageFormat.Png); + + // Change colors after the first save: barcode to blue, background to yellow. + generator.Parameters.Barcode.BarColor = Color.Blue; + generator.Parameters.BackColor = Color.Yellow; + + // Persist the second image to disk. + generator.Save(modifiedPath, BarCodeImageFormat.Png); } - // Load both saved images to verify they are different - using (var bmpRed = new Bitmap(firstImagePath)) - using (var bmpBlue = new Bitmap(secondImagePath)) + // Load both saved images to compare a single pixel and demonstrate that they are independent. + using (var originalImg = (Bitmap)Image.FromFile(originalPath)) + using (var modifiedImg = (Bitmap)Image.FromFile(modifiedPath)) { - // Compare a pixel that is likely part of the barcode (e.g., at (10,10)) - Color pixelRed = bmpRed.GetPixel(10, 10); - Color pixelBlue = bmpBlue.GetPixel(10, 10); + // Sample the top‑left pixel (0,0) from each image. + var origPixel = originalImg.GetPixel(0, 0); + var modPixel = modifiedImg.GetPixel(0, 0); - // Determine if the colors differ - bool colorsAreDifferent = !pixelRed.Equals(pixelBlue); + // Output ARGB values for visual verification. + Console.WriteLine($"Original top‑left pixel ARGB: 0x{origPixel.ToArgb():X8}"); + Console.WriteLine($"Modified top‑left pixel ARGB: 0x{modPixel.ToArgb():X8}"); - // Output pixel color information and comparison result - Console.WriteLine($"Pixel at (10,10) in first image: R={pixelRed.R}, G={pixelRed.G}, B={pixelRed.B}"); - Console.WriteLine($"Pixel at (10,10) in second image: R={pixelBlue.R}, G={pixelBlue.G}, B={pixelBlue.B}"); - Console.WriteLine($"Images differ after color change: {colorsAreDifferent}"); + // Compare the pixel values to confirm that the images differ as expected. + if (origPixel.ToArgb() != modPixel.ToArgb()) + Console.WriteLine("The images have different colors as expected – changes after Save do not affect the already saved file."); + else + Console.WriteLine("The images appear identical – unexpected behavior."); } } } \ No newline at end of file diff --git a/barcode-color-customization/generate-barcode-with-black-bars-white-background-and-green-caption-positioned-at-bottom.cs b/barcode-color-customization/generate-barcode-with-black-bars-white-background-and-green-caption-positioned-at-bottom.cs index b3ada53..006c009 100644 --- a/barcode-color-customization/generate-barcode-with-black-bars-white-background-and-green-caption-positioned-at-bottom.cs +++ b/barcode-color-customization/generate-barcode-with-black-bars-white-background-and-green-caption-positioned-at-bottom.cs @@ -1,45 +1,47 @@ -// Title: Generate Code128 barcode with custom colors and caption +// Title: Generate Code128 barcode with custom colors and bottom caption // Description: Demonstrates creating a Code128 barcode with black bars, white background, and a green caption placed below the barcode. +// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to customize barcode appearance using BarcodeGenerator, EncodeTypes, and rendering parameters such as BarColor, BackColor, and CaptionBelow. Typical use cases include branding, product labeling, and adding descriptive text to barcodes. Developers often need to adjust colors, fonts, and caption placement to match visual design requirements. // Prompt: Generate a barcode with black bars, white background, and green caption positioned at the bottom. -// Tags: code128, barcode, caption, png, aspose.barcode, aspose.drawing +// Tags: code128, barcode generation, color customization, caption, png, aspose.barcode, aspnet using System; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; -using Aspose.Drawing.Imaging; /// -/// Example program that creates a barcode image using Aspose.BarCode. +/// Demonstrates generating a Code128 barcode with custom colors and a bottom caption using Aspose.BarCode. /// class Program { /// - /// Entry point. Generates a Code128 barcode with black bars, white background, - /// and a green caption displayed beneath the barcode, then saves it as PNG. + /// Entry point. Creates the barcode, configures colors and caption, and saves it as PNG. /// static void Main() { - // Initialize a barcode generator for Code128 with the sample text "123456" - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456")) + // Initialize a BarcodeGenerator for Code128 with the sample text "1234567890" + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) { - // Set the barcode's bar color to black and the background to white - generator.Parameters.Barcode.BarColor = Color.Black; - generator.Parameters.BackColor = Color.White; + // Set the barcode bars (foreground) to black + generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Black; - // Enable and configure the caption that appears below the barcode - generator.Parameters.CaptionBelow.Visible = true; - generator.Parameters.CaptionBelow.Text = "Sample Caption"; - generator.Parameters.CaptionBelow.Font.FamilyName = "Arial"; - generator.Parameters.CaptionBelow.Font.Size.Point = 12f; - generator.Parameters.CaptionBelow.Alignment = TextAlignment.Center; - generator.Parameters.CaptionBelow.TextColor = Color.Green; + // Set the image background to white + generator.Parameters.BackColor = Aspose.Drawing.Color.White; - // Save the generated barcode image to a PNG file named "barcode.png" - generator.Save("barcode.png"); - } + // Configure the caption that appears below the barcode + generator.Parameters.CaptionBelow.Text = "Sample Caption"; // Caption text + generator.Parameters.CaptionBelow.TextColor = Aspose.Drawing.Color.Green; // Caption color + generator.Parameters.CaptionBelow.Font.FamilyName = "Arial"; // Font family + generator.Parameters.CaptionBelow.Font.Size.Point = 12f; // Font size + generator.Parameters.CaptionBelow.Alignment = TextAlignment.Center; // Center alignment + generator.Parameters.CaptionBelow.Visible = true; // Make caption visible + + // Define the output file path and save the barcode as PNG + string outputPath = "barcode.png"; + generator.Save(outputPath); - // Inform the user that the barcode has been successfully created - Console.WriteLine("Barcode generated and saved as 'barcode.png'."); + // Inform the user where the file was saved + Console.WriteLine($"Barcode saved to {outputPath}"); + } } } \ No newline at end of file diff --git a/barcode-color-customization/generate-barcode-with-custom-colors-and-then-read-back-image-to-confirm-color-values.cs b/barcode-color-customization/generate-barcode-with-custom-colors-and-then-read-back-image-to-confirm-color-values.cs index bbae251..de883ab 100644 --- a/barcode-color-customization/generate-barcode-with-custom-colors-and-then-read-back-image-to-confirm-color-values.cs +++ b/barcode-color-customization/generate-barcode-with-custom-colors-and-then-read-back-image-to-confirm-color-values.cs @@ -1,7 +1,8 @@ -// Title: Generate a Code128 barcode with custom foreground and background colors -// Description: Demonstrates creating a barcode image with blue bars on a yellow background, then verifies the colors and reads the barcode. +// Title: Generate and Verify Barcode with Custom Colors +// Description: Creates a Code128 barcode image with blue bars on a yellow background, then reads the image to confirm color values and decode the encoded text. +// Category-Description: This example belongs to the Aspose.BarCode generation and recognition category, demonstrating how to customize barcode appearance using the BarcodeGenerator class and verify the result with BarCodeReader. Typical use cases include branding, UI integration, and quality checks where specific colors are required. Developers often need to set bar and background colors, save to common image formats, and ensure the barcode remains readable. // Prompt: Generate a barcode with custom colors and then read back the image to confirm color values. -// Tags: code128, barcode generation, custom colors, png, aspose.barcode, aspose.drawing, barcode recognition +// Tags: code128, barcode generation, barcode recognition, custom colors, png, aspose.barcode using System; using System.IO; @@ -11,73 +12,59 @@ using Aspose.Drawing.Imaging; /// -/// Example program that creates a barcode with custom colors, verifies pixel colors, and reads the barcode back. +/// Demonstrates creating a barcode with custom colors, saving it as an image, +/// inspecting pixel colors, and reading the barcode back to verify the encoded data. /// class Program { /// - /// Entry point. Generates the barcode, checks colors, and decodes the barcode. + /// Entry point of the example. Generates a colored barcode, checks pixel colors, + /// and decodes the barcode from the saved image. /// static void Main() { - // Define the output file path for the barcode image - string filePath = "custom_color_barcode.png"; + // Define the output file path for the barcode image. + string imagePath = "custom_color_barcode.png"; - // Create a barcode generator using Code128 symbology and the desired code text - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) + // Create a barcode generator for Code128 symbology with the desired text. + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123ABC")) { - // Set custom foreground (bar) color to blue + // Apply custom colors: blue bars on a yellow background. generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue; - // Set custom background color to yellow generator.Parameters.BackColor = Aspose.Drawing.Color.Yellow; - // Adjust image size to ensure bars are clearly visible - generator.Parameters.AutoSizeMode = AutoSizeMode.Interpolation; - generator.Parameters.ImageWidth.Point = 300f; - generator.Parameters.ImageHeight.Point = 150f; - - // Save the generated barcode as a PNG file - generator.Save(filePath, BarCodeImageFormat.Png); + // Save the generated barcode as a PNG image. + generator.Save(imagePath, BarCodeImageFormat.Png); } - // Verify that the barcode image file was successfully created - if (!File.Exists(filePath)) + // Verify that the image file was successfully created. + if (!File.Exists(imagePath)) { Console.WriteLine("Failed to create barcode image."); return; } - // Load the saved image for pixel color verification - using (var bitmap = new Bitmap(filePath)) + // Load the saved image to inspect specific pixel colors. + using (var bitmap = (Bitmap)Image.FromFile(imagePath)) { - // Retrieve the background color from the top-left pixel (expected to be background) - Aspose.Drawing.Color bgPixel = bitmap.GetPixel(0, 0); + // Sample the top-left pixel, which should represent the background color. + Color topLeft = bitmap.GetPixel(0, 0); + Console.WriteLine($"Top-left pixel color: {topLeft}"); - // Retrieve the bar color from the center of the image (likely a bar) + // Sample a pixel near the image center, likely part of a barcode bar. int centerX = bitmap.Width / 2; int centerY = bitmap.Height / 2; - Aspose.Drawing.Color barPixel = bitmap.GetPixel(centerX, centerY); - - // Define the expected colors for comparison - Aspose.Drawing.Color expectedBg = Aspose.Drawing.Color.Yellow; - Aspose.Drawing.Color expectedBar = Aspose.Drawing.Color.Blue; - - // Output the actual and expected background colors with match status - Console.WriteLine($"Background pixel at (0,0): {bgPixel}"); - Console.WriteLine($"Expected background: {expectedBg} - {(bgPixel.ToArgb() == expectedBg.ToArgb() ? "Match" : "Mismatch")}"); - - // Output the actual and expected bar colors with match status - Console.WriteLine($"Bar pixel at ({centerX},{centerY}): {barPixel}"); - Console.WriteLine($"Expected bar color: {expectedBar} - {(barPixel.ToArgb() == expectedBar.ToArgb() ? "Match" : "Mismatch")}"); + Color centerPixel = bitmap.GetPixel(centerX, centerY); + Console.WriteLine($"Center pixel color: {centerPixel}"); } - // Read the barcode from the image to confirm the encoded text - using (var reader = new BarCodeReader(filePath, DecodeType.Code128)) + // Use BarCodeReader to decode the barcode from the saved image. + using (var reader = new BarCodeReader(imagePath, DecodeType.Code128)) { foreach (var result in reader.ReadBarCodes()) { Console.WriteLine($"Detected barcode type: {result.CodeTypeName}"); - Console.WriteLine($"Detected codetext: {result.CodeText}"); + Console.WriteLine($"Decoded text: {result.CodeText}"); } } } diff --git a/barcode-color-customization/generate-barcode-with-default-colors-and-then-change-background-to-white-to-confirm-default-behavior.cs b/barcode-color-customization/generate-barcode-with-default-colors-and-then-change-background-to-white-to-confirm-default-behavior.cs index 138140c..cc14c83 100644 --- a/barcode-color-customization/generate-barcode-with-default-colors-and-then-change-background-to-white-to-confirm-default-behavior.cs +++ b/barcode-color-customization/generate-barcode-with-default-colors-and-then-change-background-to-white-to-confirm-default-behavior.cs @@ -1,36 +1,48 @@ -// Title: Generate Code128 Barcode and Verify Default Background Color -// Description: Creates a Code128 barcode using default colors, saves it, then explicitly sets the background to white and saves again to confirm the default behavior. +// Title: Generate Code128 barcode with default colors and explicit white background +// Description: Demonstrates creating a barcode using default color settings and then overriding the background to white to verify default behavior. +// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to use BarcodeGenerator, set encoding type, and customize visual parameters such as background color. Typical use cases include creating barcodes for product labeling, inventory, and shipping where default styling may need verification or adjustment. Developers often need to generate images in PNG format and control colors for branding or readability. // Prompt: Generate a barcode with default colors and then change background to white to confirm default behavior. -// Tags: barcode, code128, default colors, background, aspnet, aspose.barcode, png +// Tags: code128, barcode generation, default colors, background color, png, aspose.barcode, aspnet using System; +using System.IO; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; /// -/// Demonstrates generating a Code128 barcode with default colors, -/// then explicitly setting the background to white to verify the default behavior. +/// Demonstrates generating a Code128 barcode with default colors and then with an explicit white background. /// class Program { /// - /// Entry point of the example. Generates and saves two barcode images. + /// Entry point. Creates output folder, generates two barcode images, and writes their paths to the console. /// static void Main() { - // Initialize a BarcodeGenerator for Code128 with the data "123456" - // The generator uses default settings where the background is white. - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456")) - { - // Save the barcode image using the default color scheme (white background) - generator.Save("barcode_default.png"); + // Ensure the output directory exists + string outputFolder = "output"; + Directory.CreateDirectory(outputFolder); + + // Define file paths for the two generated images + string defaultPath = Path.Combine(outputFolder, "barcode_default.png"); + string whiteBgPath = Path.Combine(outputFolder, "barcode_whitebg.png"); - // Explicitly set the background color to white (same as the default) to confirm behavior - generator.Parameters.BackColor = Color.White; + // Generate barcode using default colors (BarColor = Black, BackColor = White) + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) + { + generator.Save(defaultPath); + } - // Save the barcode image after setting the background to white - generator.Save("barcode_white.png"); + // Generate barcode with background explicitly set to white + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) + { + generator.Parameters.BackColor = Aspose.Drawing.Color.White; // explicit white background + generator.Save(whiteBgPath); } + + // Output the locations of the saved barcode images + Console.WriteLine($"Default barcode saved to: {defaultPath}"); + Console.WriteLine($"White background barcode saved to: {whiteBgPath}"); } } \ No newline at end of file diff --git a/barcode-color-customization/generate-code128-barcode-with-blue-bars-white-background-and-red-caption-saving-to-png.cs b/barcode-color-customization/generate-code128-barcode-with-blue-bars-white-background-and-red-caption-saving-to-png.cs index 1b6a1f8..e26496b 100644 --- a/barcode-color-customization/generate-code128-barcode-with-blue-bars-white-background-and-red-caption-saving-to-png.cs +++ b/barcode-color-customization/generate-code128-barcode-with-blue-bars-white-background-and-red-caption-saving-to-png.cs @@ -1,47 +1,46 @@ -// Title: Code128 Barcode Generation with Custom Colors and Caption +// Title: Generate Code128 barcode with custom colors and caption // Description: Demonstrates creating a Code128 barcode with blue bars, white background, and a red caption, then saving it as a PNG file. +// Category-Description: This example belongs to the Aspose.BarCode generation category, showcasing how to customize barcode appearance using the BarcodeGenerator class. It covers setting bar colors, background, and caption properties—common tasks for developers needing branded or readable barcodes in images, PDFs, or reports. // Prompt: Generate a Code128 barcode with blue bars, white background, and red caption, saving to PNG. -// Tags: barcode, code128, generation, png, color, caption, aspose.barcode +// Tags: barcode symbology, generation, png, aspose.barcode, aspose.drawing using System; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; -namespace BarcodeSample +/// +/// Example program that creates a Code128 barcode with customized colors and a caption, +/// then saves the result as a PNG image. +/// +class Program { /// - /// Sample program that generates a Code128 barcode with custom colors and a caption. + /// Entry point of the application. Generates the barcode and writes a confirmation message to the console. /// - class Program + static void Main() { - /// - /// Entry point. Creates a barcode, customizes its appearance, and saves it as a PNG image. - /// - static void Main() + // Initialize a BarcodeGenerator for Code128 with the sample text "1234567890" + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) { - // Initialize a barcode generator for the Code128 symbology - using (var generator = new BarcodeGenerator(EncodeTypes.Code128)) - { - // Set the data to be encoded in the barcode - generator.CodeText = "1234567890"; + // Set the color of the barcode bars to blue + generator.Parameters.Barcode.BarColor = Color.Blue; - // Apply a blue color to the barcode bars - generator.Parameters.Barcode.BarColor = Color.Blue; + // Set the image background color to white + generator.Parameters.BackColor = Color.White; - // Set the background of the image to white - generator.Parameters.BackColor = Color.White; + // Enable and configure a caption displayed above the barcode + generator.Parameters.CaptionAbove.Visible = true; + generator.Parameters.CaptionAbove.Text = "Code128 Barcode"; + generator.Parameters.CaptionAbove.TextColor = Color.Red; + generator.Parameters.CaptionAbove.Font.Size.Point = 12f; // Font size in points + generator.Parameters.CaptionAbove.Alignment = TextAlignment.Center; - // Enable and configure a red caption displayed above the barcode - generator.Parameters.CaptionAbove.Visible = true; - generator.Parameters.CaptionAbove.Text = "Sample Caption"; - generator.Parameters.CaptionAbove.TextColor = Color.Red; - generator.Parameters.CaptionAbove.Font.FamilyName = "Arial"; - generator.Parameters.CaptionAbove.Font.Size.Point = 12f; - - // Save the generated barcode as a PNG file in the current directory - generator.Save("code128.png"); - } + // Save the generated barcode image as a PNG file + generator.Save("code128.png"); } + + // Inform the user that the barcode has been generated + Console.WriteLine("Barcode generated and saved as code128.png"); } } \ No newline at end of file diff --git a/barcode-color-customization/produce-multiple-barcode-images-with-varying-color-schemes-using-single-barcodegenerator-instance.cs b/barcode-color-customization/produce-multiple-barcode-images-with-varying-color-schemes-using-single-barcodegenerator-instance.cs index 57487ab..05d82d3 100644 --- a/barcode-color-customization/produce-multiple-barcode-images-with-varying-color-schemes-using-single-barcodegenerator-instance.cs +++ b/barcode-color-customization/produce-multiple-barcode-images-with-varying-color-schemes-using-single-barcodegenerator-instance.cs @@ -1,7 +1,8 @@ -// Title: Generate barcode images with multiple color schemes -// Description: Demonstrates using a single BarcodeGenerator to create several barcode PNG files, each with a different foreground and background color. +// Title: Generate barcode images with different color schemes using a single generator +// Description: Demonstrates how to produce multiple PNG barcode files, each with distinct bar and background colors, by reusing one BarcodeGenerator instance. +// Category-Description: This example belongs to the Aspose.BarCode image generation category, illustrating the use of BarcodeGenerator, EncodeTypes, and color customization. Developers often need to create batches of barcodes with varying visual styles for branding or UI integration, and this snippet shows the typical workflow for setting bar color, background color, and saving images. // Prompt: Produce multiple barcode images with varying color schemes using a single BarcodeGenerator instance. -// Tags: barcode, code128, color, png, aspose.barcode, generation +// Tags: code128, barcode, color, png, aspose.barcode, generation, generator using System; using Aspose.BarCode; @@ -9,42 +10,54 @@ using Aspose.Drawing; /// -/// Example program that creates several barcode images, each using a distinct color scheme, -/// while reusing a single instance. +/// Demonstrates generating multiple barcode images with different color schemes using a single instance. /// class Program { /// - /// Entry point of the application. Generates barcode PNG files with different foreground - /// and background colors and saves them to disk. + /// Application entry point. Creates the output directory, configures a barcode generator, iterates through predefined color schemes, + /// and saves each barcode as a PNG file. /// static void Main() { - // Define a set of color schemes (foreground bar color, background color) and corresponding output file names. - var colorSchemes = new (Color BarColor, Color BackColor, string FileName)[] + // Define the folder where barcode images will be saved. + string outputFolder = "Barcodes"; + + // Ensure the output directory exists. + if (!System.IO.Directory.Exists(outputFolder)) { - (Color.Blue, Color.White, "barcode_blue_white.png"), - (Color.Red, Color.Yellow, "barcode_red_yellow.png"), - (Color.Green, Color.LightGray, "barcode_green_lightgray.png"), - (Color.Black, Color.Pink, "barcode_black_pink.png"), - (Color.Orange, Color.LightBlue, "barcode_orange_lightblue.png") - }; - - // Create a single BarcodeGenerator instance for Code128 with a sample codetext. - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Demo123")) + System.IO.Directory.CreateDirectory(outputFolder); + } + + // Initialize a BarcodeGenerator for Code128 with sample text. + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) { - // Optional: increase resolution for better quality output. - generator.Parameters.Resolution = 300; + // Optional common setting: fill the bars completely. + generator.Parameters.Barcode.FilledBars = true; + + // Array of color schemes: bar color, background color, and target file name. + var colorSchemes = new (Color BarColor, Color BackColor, string FileName)[] + { + (Color.Black, Color.White, "barcode_black_on_white.png"), + (Color.Red, Color.White, "barcode_red_on_white.png"), + (Color.White, Color.Black, "barcode_white_on_black.png"), + (Color.Blue, Color.Yellow, "barcode_blue_on_yellow.png") + }; - // Iterate through each color scheme, apply the colors, and save the barcode image. + // Generate and save a barcode for each color scheme using the same generator instance. foreach (var scheme in colorSchemes) { - generator.Parameters.Barcode.BarColor = scheme.BarColor; // Set foreground bar color. - generator.Parameters.BackColor = scheme.BackColor; // Set background color. - generator.Save(scheme.FileName); // Save image; file extension determines format (PNG). + // Apply the current scheme's bar and background colors. + generator.Parameters.Barcode.BarColor = scheme.BarColor; + generator.Parameters.BackColor = scheme.BackColor; + + // Build the full file path and save the image. + string filePath = System.IO.Path.Combine(outputFolder, scheme.FileName); + generator.Save(filePath); + + // Inform the user about the saved file. + Console.WriteLine($"Saved: {filePath}"); } } - - Console.WriteLine("Barcode images generated with varying color schemes."); } } \ No newline at end of file diff --git a/barcode-color-customization/reset-barcode-background-to-default-white-after-previously-setting-it-to-gray.cs b/barcode-color-customization/reset-barcode-background-to-default-white-after-previously-setting-it-to-gray.cs index 7005e79..cf1dad2 100644 --- a/barcode-color-customization/reset-barcode-background-to-default-white-after-previously-setting-it-to-gray.cs +++ b/barcode-color-customization/reset-barcode-background-to-default-white-after-previously-setting-it-to-gray.cs @@ -1,36 +1,45 @@ -// Title: Resetting Barcode Background Color -// Description: Demonstrates how to change a barcode's background color to gray, save it, then reset the background to the default white and save again. +// Title: Reset barcode background color from gray to default white +// Description: Demonstrates how to change a barcode's background color to gray and then revert it back to white using Aspose.BarCode. +// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating the use of BarcodeGenerator and its Parameters property to customize visual aspects such as background color. Developers often need to adjust barcode appearance for branding or printing requirements, and this snippet shows the typical workflow for setting and resetting colors before saving images. // Prompt: Reset the barcode background to default white after previously setting it to gray. -// Tags: barcode symbology, background reset, png output, aspose.barcode, aspose.drawing +// Tags: barcode generation, background color, reset, code128, png, aspose.barcode, aspose.drawing using System; -using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; /// -/// Example program that shows how to modify and then reset a barcode's background color. +/// Example program that creates a Code128 barcode, first with a gray background, +/// then resets the background to the default white and saves both images. /// class Program { /// - /// Generates two barcode images: one with a gray background and another with the default white background. + /// Entry point of the example. Generates two barcode images demonstrating background color reset. /// static void Main() { - // Create a barcode generator for a Code128 barcode with the value "12345" - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "12345")) + // Define output file names for the two barcode images. + const string grayPath = "barcode_gray.png"; + const string whitePath = "barcode_white.png"; + + // Initialize a BarcodeGenerator for Code128 with sample text. + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456789")) { - // Set the background color to gray and save the first image + // Set the background color to gray. generator.Parameters.BackColor = Color.Gray; - generator.Save("barcode_gray.png"); - // Reset the background color to the default white and save the second image + // Save the barcode image with a gray background. + generator.Save(grayPath); + + // Reset the background color to the default white. generator.Parameters.BackColor = Color.White; - generator.Save("barcode_white.png"); + + // Save the barcode image with a white background. + generator.Save(whitePath); } - // Inform the user that the barcode images have been created - Console.WriteLine("Barcodes generated: barcode_gray.png (gray background), barcode_white.png (white background)."); + // Inform the user about the generated files. + Console.WriteLine("Barcodes generated: gray -> {0}, white -> {1}", grayPath, whitePath); } } \ No newline at end of file diff --git a/barcode-color-customization/retrieve-and-display-default-bar-and-background-colors-before-applying-any-customizations.cs b/barcode-color-customization/retrieve-and-display-default-bar-and-background-colors-before-applying-any-customizations.cs index 11d1076..659dd1e 100644 --- a/barcode-color-customization/retrieve-and-display-default-bar-and-background-colors-before-applying-any-customizations.cs +++ b/barcode-color-customization/retrieve-and-display-default-bar-and-background-colors-before-applying-any-customizations.cs @@ -1,7 +1,8 @@ -// Title: Retrieve Default Barcode Colors -// Description: Demonstrates how to obtain the default foreground (bar) and background colors of a barcode before any customizations are applied. +// Title: Retrieve Default Barcode Bar and Background Colors +// Description: Demonstrates how to obtain the default bar and background colors from an Aspose.BarCode generator before any customizations are applied. +// Category-Description: This example belongs to the Aspose.BarCode generation category, showcasing the use of BarcodeGenerator, its Parameters, and color properties. Developers often need to query default visual settings (bar color, background color) to ensure consistency or to base custom themes on them. Typical use cases include preparing barcode images for reports, labels, or UI previews where default styling information is required. // Prompt: Retrieve and display the default bar and background colors before applying any customizations. -// Tags: barcode, colors, default, aspose.barcode, c# +// Tags: barcode, symbology, color, default, generation, png, aspose.barcode, aspose.drawing using System; using Aspose.BarCode; @@ -9,29 +10,33 @@ using Aspose.Drawing; /// -/// Example program that shows how to read the default bar and background colors -/// from an Aspose.BarCode instance. +/// Example program that retrieves and displays the default bar and background colors +/// of a barcode generator before any customizations are applied. /// class Program { /// - /// Entry point of the application. - /// Retrieves and prints the default barcode colors before any changes are made. + /// Entry point of the example. Creates a BarcodeGenerator, reads default colors, + /// prints them to the console, and saves a sample barcode image. /// static void Main() { - // Initialize a barcode generator with the default symbology (Code128) + // Initialize a barcode generator with the Code128 symbology. using (var generator = new BarcodeGenerator(EncodeTypes.Code128)) { - // Get the default foreground (bar) color - Color defaultBarColor = generator.Parameters.Barcode.BarColor; + // Retrieve the generator's default bar (foreground) color. + Aspose.Drawing.Color defaultBarColor = generator.Parameters.Barcode.BarColor; - // Get the default background color - Color defaultBackColor = generator.Parameters.BackColor; + // Retrieve the generator's default background color. + Aspose.Drawing.Color defaultBackColor = generator.Parameters.BackColor; - // Output the retrieved default colors to the console + // Output the default colors to the console. Console.WriteLine($"Default Bar Color: {defaultBarColor}"); Console.WriteLine($"Default Background Color: {defaultBackColor}"); + + // Optionally generate a sample barcode image to verify the defaults. + // The image is saved as "default_barcode.png" in the working directory. + generator.Save("default_barcode.png"); } } } \ No newline at end of file diff --git a/barcode-color-customization/save-barcode-with-custom-colors-to-file-path-that-includes-guid-for-uniqueness.cs b/barcode-color-customization/save-barcode-with-custom-colors-to-file-path-that-includes-guid-for-uniqueness.cs index 8c977b2..21b3d15 100644 --- a/barcode-color-customization/save-barcode-with-custom-colors-to-file-path-that-includes-guid-for-uniqueness.cs +++ b/barcode-color-customization/save-barcode-with-custom-colors-to-file-path-that-includes-guid-for-uniqueness.cs @@ -1,7 +1,8 @@ -// Title: Save Barcode with Custom Colors and Unique File Name +// Title: Save barcode with custom colors and unique GUID filename // Description: Demonstrates generating a Code128 barcode, applying custom foreground and background colors, and saving it to a uniquely named PNG file using a GUID. +// Category-Description: This example belongs to the Aspose.BarCode image generation category, illustrating how to customize barcode appearance with the BarcodeGenerator class, set color properties, and output to common image formats. Developers often need to create distinct barcode files for batch processing, reporting, or inventory systems, and this pattern shows the typical steps for color customization and unique file naming. // Prompt: Save a barcode with custom colors to a file path that includes a GUID for uniqueness. -// Tags: code128, barcode generation, custom colors, file output, guid, aspose.barcode, aspose.drawing +// Tags: barcode, code128, custom colors, png, guid, aspose.barcode, generation using System; using System.IO; @@ -11,35 +12,37 @@ /// /// Example program that creates a Code128 barcode with custom colors -/// and saves it to a uniquely named PNG file. +/// and saves it to a uniquely named PNG file using a GUID. /// class Program { /// /// Entry point of the application. - /// Generates a barcode, applies custom colors, and writes the image to disk. + /// Generates the barcode, applies color settings, and writes the image to disk. /// static void Main() { - // Generate a unique file name using a GUID to avoid collisions - string guid = Guid.NewGuid().ToString(); - string fileName = $"barcode_{guid}.png"; - string outputPath = Path.Combine(Environment.CurrentDirectory, fileName); + // Define the output directory relative to the current working directory + string outputDir = Path.Combine(Directory.GetCurrentDirectory(), "Barcodes"); + Directory.CreateDirectory(outputDir); // Ensure the directory exists - // Initialize a barcode generator for Code128 with the desired text + // Build a unique file name using a new GUID and combine it with the output path + string filePath = Path.Combine(outputDir, $"{Guid.NewGuid()}.png"); + + // Initialize the barcode generator for Code128 with sample data using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123")) { - // Set the foreground (bars) color to blue - generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue; + // Set the barcode (foreground) color to blue + generator.Parameters.Barcode.BarColor = Color.Blue; - // Set the background color to yellow - generator.Parameters.BackColor = Aspose.Drawing.Color.Yellow; + // Set the background color of the image to yellow + generator.Parameters.BackColor = Color.Yellow; - // Save the generated barcode image to the unique file path - generator.Save(outputPath); + // Save the generated barcode as a PNG file at the specified path + generator.Save(filePath, BarCodeImageFormat.Png); } // Inform the user where the barcode image was saved - Console.WriteLine($"Barcode saved to: {outputPath}"); + Console.WriteLine($"Barcode saved to: {filePath}"); } } \ No newline at end of file diff --git a/barcode-color-customization/save-barcode-with-custom-colors-to-file-path-that-includes-timestamp-for-uniqueness.cs b/barcode-color-customization/save-barcode-with-custom-colors-to-file-path-that-includes-timestamp-for-uniqueness.cs index b7b7be5..e20242b 100644 --- a/barcode-color-customization/save-barcode-with-custom-colors-to-file-path-that-includes-timestamp-for-uniqueness.cs +++ b/barcode-color-customization/save-barcode-with-custom-colors-to-file-path-that-includes-timestamp-for-uniqueness.cs @@ -1,7 +1,8 @@ -// Title: Save Barcode with Custom Colors and Timestamped Filename -// Description: Demonstrates generating a Code128 barcode, applying custom foreground and background colors, and saving it to a uniquely named PNG file using a timestamp. +// Title: Save barcode with custom colors and timestamped filename +// Description: Demonstrates generating a Code128 barcode with custom foreground and background colors, saving it to a uniquely named PNG file using a timestamp. +// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to customize barcode appearance with color properties and persist the image. It uses BarcodeGenerator, EncodeTypes, and drawing color classes, common tasks for developers needing branded or visually distinct barcodes in applications. // Prompt: Save a barcode with custom colors to a file path that includes a timestamp for uniqueness. -// Tags: code128, barcode, custom colors, file output, timestamp, aspose.barcode, aspose.drawing +// Tags: code128, barcode, color, save, png, aspose.barcode, generation using System; using System.IO; @@ -9,42 +10,40 @@ using Aspose.BarCode.Generation; using Aspose.Drawing; -namespace BarcodeSample +/// +/// Generates a Code128 barcode with custom colors and saves it to a uniquely named PNG file. +/// +class Program { /// - /// Sample program that creates a Code128 barcode with custom colors and saves it to a timestamped file. + /// Entry point of the example. Creates a barcode, applies custom colors, and writes the image to a timestamped file. /// - class Program + static void Main() { - /// - /// Entry point of the application. - /// Generates the barcode, applies colors, and writes the image to a uniquely named file. - /// - /// Command‑line arguments (not used). - static void Main(string[] args) - { - // Create a unique file name using the current date and time down to milliseconds. - string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmssfff"); - string fileName = $"barcode_{timestamp}.png"; + // Define the text to encode in the barcode. + const string codeText = "Sample12345"; - // Combine the current directory with the file name to get the full output path. - string outputPath = Path.Combine(Environment.CurrentDirectory, fileName); + // Build a timestamp string for a unique file name (e.g., 20231127_154530123). + string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmssfff"); - // Initialize the barcode generator for Code128 with the desired data. - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) - { - // Set the color of the barcode bars (foreground). - generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue; + // Compose the full file name and path in the current working directory. + string fileName = $"barcode_{timestamp}.png"; + string outputPath = Path.Combine(Directory.GetCurrentDirectory(), fileName); - // Set the background color of the image. - generator.Parameters.BackColor = Aspose.Drawing.Color.Yellow; + // Initialize the barcode generator with Code128 symbology and the desired text. + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, codeText)) + { + // Set the color of the barcode bars (foreground). + generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue; - // Save the generated barcode image to the specified path. - generator.Save(outputPath); - } + // Set the background color of the image. + generator.Parameters.BackColor = Aspose.Drawing.Color.Yellow; - // Inform the user where the barcode image was saved. - Console.WriteLine($"Barcode saved to: {outputPath}"); + // Save the generated barcode image to the specified path. + generator.Save(outputPath); } + + // Inform the user where the barcode image was saved. + Console.WriteLine($"Barcode saved to: {outputPath}"); } } \ No newline at end of file diff --git a/barcode-color-customization/set-background-color-to-light-gray-and-bar-color-to-dark-gray-for-pdf417-barcode.cs b/barcode-color-customization/set-background-color-to-light-gray-and-bar-color-to-dark-gray-for-pdf417-barcode.cs index 57ec123..8681e3c 100644 --- a/barcode-color-customization/set-background-color-to-light-gray-and-bar-color-to-dark-gray-for-pdf417-barcode.cs +++ b/barcode-color-customization/set-background-color-to-light-gray-and-bar-color-to-dark-gray-for-pdf417-barcode.cs @@ -1,7 +1,8 @@ -// Title: PDF417 Barcode with Custom Colors -// Description: Demonstrates setting background and bar colors for a PDF417 barcode and saving it as a PNG image. +// Title: Set PDF417 Barcode Colors (Background Light Gray, Bar Dark Gray) +// Description: Demonstrates how to change the background and bar colors of a PDF417 barcode using Aspose.BarCode and save it as a PNG image. +// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to customize visual appearance of barcodes. It uses the BarcodeGenerator class with EncodeTypes.Pdf417, showing typical use cases such as setting background and foreground colors before exporting to an image format. Developers often need to adjust colors to match branding or design requirements. // Prompt: Set the background color to light gray and bar color to dark gray for a PDF417 barcode. -// Tags: pdf417, barcode, color, png, aspose.barcode, aspose.drawing +// Tags: pdf417, barcode, color, background, foreground, aspose.barcode, image, png using System; using Aspose.BarCode; @@ -9,26 +10,29 @@ using Aspose.Drawing; /// -/// Example program that generates a PDF417 barcode with custom background and bar colors. +/// Generates a PDF417 barcode with custom background and bar colors and saves it as a PNG file. /// class Program { /// - /// Entry point. Creates a PDF417 barcode, applies custom colors, and saves it as a PNG file. + /// Entry point of the example. Creates a PDF417 barcode, applies color settings, and writes the image to disk. /// static void Main() { - // Initialize a barcode generator for PDF417 with sample text + // Initialize the barcode generator with PDF417 symbology and sample text using (var generator = new BarcodeGenerator(EncodeTypes.Pdf417, "Sample PDF417 Text")) { - // Apply a light gray background to the barcode image + // Apply a light gray background color to the entire image generator.Parameters.BackColor = Color.LightGray; // Apply a dark gray color to the barcode bars (foreground) generator.Parameters.Barcode.BarColor = Color.DarkGray; - // Persist the generated barcode to a PNG file - generator.Save("pdf417_custom_colors.png"); + // Save the generated barcode as a PNG file + generator.Save("pdf417.png"); } + + // Inform the user that the barcode image has been created + Console.WriteLine("PDF417 barcode generated: pdf417.png"); } } \ No newline at end of file diff --git a/barcode-color-customization/set-background-color-to-transparent-and-generate-png-with-alpha-channel-for-overlay-use.cs b/barcode-color-customization/set-background-color-to-transparent-and-generate-png-with-alpha-channel-for-overlay-use.cs index cf2f4a7..747d651 100644 --- a/barcode-color-customization/set-background-color-to-transparent-and-generate-png-with-alpha-channel-for-overlay-use.cs +++ b/barcode-color-customization/set-background-color-to-transparent-and-generate-png-with-alpha-channel-for-overlay-use.cs @@ -1,32 +1,40 @@ -// Title: Generate Transparent PNG Barcode -// Description: Creates a Code128 barcode with a transparent background and saves it as a PNG file preserving the alpha channel for overlay use. +// Title: Generate transparent PNG barcode for overlay +// Description: Demonstrates setting a barcode's background to transparent and saving it as a PNG with an alpha channel, suitable for overlay scenarios. +// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to customize barcode appearance using the BarcodeGenerator class. It shows configuring background transparency and saving to PNG, a common requirement when integrating barcodes into UI overlays or composite images. Developers often need to adjust colors, formats, and image properties for seamless visual integration. // Prompt: Set the background color to transparent and generate a PNG with alpha channel for overlay use. -// Tags: code128, barcode, transparent background, png, alpha channel, aspose.barcode, aspose.drawing +// Tags: barcode, generation, transparent background, png, alpha channel, code128, aspose.barcode, aspose.drawing using System; -using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; /// -/// Demonstrates how to generate a barcode with a transparent background and save it as a PNG image. +/// Demonstrates creating a Code128 barcode with a transparent background and saving it as a PNG image. /// class Program { /// - /// Entry point of the application. Generates a Code128 barcode with a transparent background and saves it as a PNG. + /// Entry point of the example. Generates the barcode and writes the output path to the console. /// static void Main() { - // Initialize a barcode generator for Code128 symbology with the sample text "Sample". - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample")) + // Define the output file name and location. + string outputPath = "transparent_barcode.png"; + + // Initialize the barcode generator with the desired symbology (Code128) and data. + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123")) { - // Configure the barcode to have a transparent background. - generator.Parameters.BackColor = Color.Transparent; + // Set the background color to transparent so the PNG will contain an alpha channel. + generator.Parameters.BackColor = Aspose.Drawing.Color.Transparent; + + // Optionally set the foreground (bar) color to black. + generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Black; - // Save the generated barcode as a PNG file. - // PNG format retains the alpha channel, making the background truly transparent. - generator.Save("transparent_barcode.png"); + // Save the generated barcode as a PNG file; PNG format preserves transparency. + generator.Save(outputPath, BarCodeImageFormat.Png); } + + // Inform the user where the barcode image has been saved. + Console.WriteLine($"Barcode saved to {outputPath}"); } } \ No newline at end of file diff --git a/barcode-color-customization/set-caption-position-to-top-and-apply-custom-caption-color-before-saving-as-png.cs b/barcode-color-customization/set-caption-position-to-top-and-apply-custom-caption-color-before-saving-as-png.cs index 9b96926..533fee1 100644 --- a/barcode-color-customization/set-caption-position-to-top-and-apply-custom-caption-color-before-saving-as-png.cs +++ b/barcode-color-customization/set-caption-position-to-top-and-apply-custom-caption-color-before-saving-as-png.cs @@ -1,7 +1,8 @@ -// Title: Barcode Generation with Top Caption and Custom Color -// Description: Demonstrates creating a Code128 barcode, placing a caption above it, applying a custom color, and saving as PNG. +// Title: Set Caption Position to Top with Custom Color and Save as PNG +// Description: Demonstrates how to place a caption above a Code128 barcode, apply a custom color, and save the result as a PNG image. +// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating the use of BarcodeGenerator, EncodeTypes, and caption parameters. Developers commonly need to customize barcode appearance—such as adding captions, adjusting fonts, and setting colors—before exporting to image formats like PNG for web or print use. The snippet serves as a reference for creating branded or annotated barcodes in .NET applications. // Prompt: Set the caption position to top and apply a custom caption color before saving as PNG. -// Tags: barcode, code128, caption, top, color, png, aspose.barcode, aspose.drawing +// Tags: code128, caption, png, generation, aspose.barcode, aspose.drawing using System; using Aspose.BarCode; @@ -9,8 +10,8 @@ using Aspose.Drawing; /// -/// Example program that generates a Code128 barcode with a top caption -/// and a custom caption color, then saves the image as a PNG file. +/// Example program that generates a Code128 barcode with a top caption, +/// applies a custom caption color, and saves the image as a PNG file. /// class Program { @@ -19,22 +20,26 @@ class Program /// static void Main() { - // Initialize a barcode generator for Code128 with the sample text "123456" - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456")) + // Initialize a barcode generator for Code128 with the sample text "1234567890". + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "1234567890")) { - // Enable and configure a caption positioned above the barcode (top) + // Enable the caption above the barcode and set its text. generator.Parameters.CaptionAbove.Visible = true; generator.Parameters.CaptionAbove.Text = "Top Caption"; - // Apply a custom color to the caption text (Blue) + // Apply a custom color (blue) to the caption text. generator.Parameters.CaptionAbove.TextColor = Color.Blue; - // Optional: customize the caption font (Arial, 12pt) + // Optional: customize the caption font and alignment. generator.Parameters.CaptionAbove.Font.FamilyName = "Arial"; generator.Parameters.CaptionAbove.Font.Size.Point = 12f; + generator.Parameters.CaptionAbove.Alignment = TextAlignment.Center; - // Save the generated barcode image as a PNG file - generator.Save("barcode_with_top_caption.png"); + // Save the generated barcode image as a PNG file. + generator.Save("barcode_with_caption.png"); } + + // Inform the user that the image has been saved. + Console.WriteLine("Barcode image saved as 'barcode_with_caption.png'."); } } \ No newline at end of file diff --git a/barcode-color-customization/use-colorfromname-to-set-caption-color-to-purple-for-upca-barcode.cs b/barcode-color-customization/use-colorfromname-to-set-caption-color-to-purple-for-upca-barcode.cs index 0392c45..23c5880 100644 --- a/barcode-color-customization/use-colorfromname-to-set-caption-color-to-purple-for-upca-barcode.cs +++ b/barcode-color-customization/use-colorfromname-to-set-caption-color-to-purple-for-upca-barcode.cs @@ -1,32 +1,42 @@ // Title: Set Caption Color to Purple for UPC-A Barcode -// Description: Demonstrates generating a UPC-A barcode with Aspose.BarCode and setting the caption color using Color.FromName. +// Description: Demonstrates how to generate a UPC‑A barcode and set its caption color to purple using Color.FromName. +// Category-Description: This example belongs to the Aspose.BarCode generation category, illustrating how to customize barcode appearance. It showcases the BarcodeGenerator class, its Parameters property, and the use of System.Drawing.Color for styling captions. Developers often need to modify caption text and colors when integrating barcodes into product packaging, labels, or UI displays. // Prompt: Use Color.FromName to set caption color to "Purple" for a UPC-A barcode. -// Tags: barcode, upc-a, caption, color, png, aspose.barcode, aspose.drawing +// Tags: upc-a, set-caption-color, png, barcodegenerator, parameters, color using System; +using System.IO; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; /// -/// Generates a UPC-A barcode image and sets the caption color to purple. +/// Generates a UPC‑A barcode, sets a custom caption with a purple color, and saves it as a PNG image. /// class Program { /// - /// Entry point of the application. Creates a barcode, configures caption color, and saves the image. + /// Entry point of the example. Creates the barcode, applies caption styling, and writes the image to disk. /// static void Main() { - // Initialize a UPC-A barcode generator with a valid 12‑digit code + // Define the output file name + string outputPath = "upc_a.png"; + + // Initialize the barcode generator for UPC‑A with a valid 12‑digit value (including check digit) using (var generator = new BarcodeGenerator(EncodeTypes.UPCA, "012345678905")) { - // Set the caption text (optional) and apply purple color using Color.FromName - generator.Parameters.CaptionAbove.Text = "UPC‑A Sample"; + // Set the caption text that appears above the barcode + generator.Parameters.CaptionAbove.Text = "Sample UPC‑A"; + + // Apply the purple color to the caption using Color.FromName generator.Parameters.CaptionAbove.TextColor = Color.FromName("Purple"); // Save the generated barcode as a PNG file - generator.Save("upc_a.png"); + generator.Save(outputPath, BarCodeImageFormat.Png); } + + // Output the full path of the saved barcode image + Console.WriteLine($"Barcode saved to: {Path.GetFullPath(outputPath)}"); } } \ No newline at end of file diff --git a/barcode-color-customization/use-custom-systemdrawingcolorfromargb-value-for-semi-transparent-bar-color-and-generate-png.cs b/barcode-color-customization/use-custom-systemdrawingcolorfromargb-value-for-semi-transparent-bar-color-and-generate-png.cs index 9a70bd0..14b5bc0 100644 --- a/barcode-color-customization/use-custom-systemdrawingcolorfromargb-value-for-semi-transparent-bar-color-and-generate-png.cs +++ b/barcode-color-customization/use-custom-systemdrawingcolorfromargb-value-for-semi-transparent-bar-color-and-generate-png.cs @@ -1,7 +1,8 @@ -// Title: Generate a semi‑transparent Code128 barcode and save as PNG -// Description: Demonstrates setting a custom semi‑transparent bar color using Color.FromArgb and exporting the barcode to a PNG file. +// Title: Generate semi‑transparent barcode with custom ARGB color and save as PNG +// Description: Demonstrates setting a semi‑transparent bar color using System.Drawing.Color.FromArgb and exporting the barcode to a PNG file. +// Category-Description: This example belongs to the Aspose.BarCode image generation category, illustrating how to customize barcode appearance with the BarcodeGenerator class. It shows typical use cases such as applying custom colors, adjusting transparency, and saving to common image formats. Developers often need these techniques when integrating barcodes into UI designs or reports that require visual styling. // Prompt: Use a custom System.Drawing.Color.FromArgb value for semi‑transparent bar color and generate PNG. -// Tags: barcode, code128, color, transparency, png, aspose.barcode, aspose.drawing, generation +// Tags: barcode symbology, color customization, png output, aspose.barcode, aspose.drawing using System; using Aspose.BarCode; @@ -9,29 +10,32 @@ using Aspose.Drawing; /// -/// Example program that creates a Code128 barcode with a semi‑transparent bar color and saves it as a PNG image. +/// Program demonstrating semi‑transparent barcode generation. /// class Program { /// - /// Entry point of the application. Generates the barcode and writes it to disk. + /// Entry point. Generates a Code128 barcode with a semi‑transparent red bar color and saves it as a PNG file. /// static void Main() { // Define the output file path for the generated PNG image - const string outputPath = "barcode.png"; + string outputPath = "semiTransparentBarcode.png"; - // Initialize the barcode generator with Code128 symbology and sample text - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Sample123")) + // Initialize the barcode generator for Code128 symbology with sample data + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456789")) { - // Set a semi‑transparent blue bar color (alpha 128, red 0, green 0, blue 255) - generator.Parameters.Barcode.BarColor = Color.FromArgb(128, 0, 0, 255); + // Apply a semi‑transparent red color to the bars (alpha 128 out of 255) + generator.Parameters.Barcode.BarColor = Color.FromArgb(128, 255, 0, 0); - // Save the barcode image as a PNG file at the specified path - generator.Save(outputPath); + // Set a white background to improve visibility of the semi‑transparent bars + generator.Parameters.BackColor = Color.White; + + // Save the generated barcode image as a PNG file + generator.Save(outputPath, BarCodeImageFormat.Png); } - // Output a confirmation message with the location of the saved file + // Inform the user where the barcode image has been saved Console.WriteLine($"Barcode saved to {outputPath}"); } } \ No newline at end of file diff --git a/barcode-color-customization/use-loop-to-generate-barcodes-with-alternating-background-colors-while-keeping-bar-color-constant.cs b/barcode-color-customization/use-loop-to-generate-barcodes-with-alternating-background-colors-while-keeping-bar-color-constant.cs index 111167a..ecf44a5 100644 --- a/barcode-color-customization/use-loop-to-generate-barcodes-with-alternating-background-colors-while-keeping-bar-color-constant.cs +++ b/barcode-color-customization/use-loop-to-generate-barcodes-with-alternating-background-colors-while-keeping-bar-color-constant.cs @@ -1,52 +1,56 @@ -// Title: Generate Barcodes with Alternating Background Colors -// Description: Demonstrates creating multiple Code128 barcodes where the bar color stays black while the background alternates between white and light gray. +// Title: Generate Code128 barcodes with alternating background colors +// Description: Demonstrates creating multiple Code128 barcodes where the background color alternates while the bar color stays black, saving each as a PNG file. +// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, illustrating how to use the BarcodeGenerator class together with the Parameters property to customize visual appearance such as bar and background colors. Typical use cases include batch creation of barcodes for product labeling or testing visual themes. Developers often need to programmatically vary colors, formats, or symbologies across multiple images. // Prompt: Use a loop to generate barcodes with alternating background colors while keeping bar color constant. -// Tags: barcode, code128, background color, loop, aspose.barcode, png +// Tags: code128, barcode generation, background color, bar color, png, aspose.barcode, barcodegenerator, parameters using System; +using System.IO; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; /// -/// Example program that generates a series of Code128 barcodes with alternating background colors. +/// Generates a series of Code128 barcode images with alternating background colors. /// class Program { /// - /// Entry point of the application. Generates barcodes using a loop and saves them as PNG files. + /// Entry point of the application. Creates five barcode PNG files with alternating backgrounds. /// static void Main() { - // Sample codetext values to encode in each barcode - string[] codes = { "ABC123", "DEF456", "GHI789", "JKL012", "MNO345" }; + // Ensure the output directory exists + string outputDir = "Barcodes"; + Directory.CreateDirectory(outputDir); - // Define alternating background colors: white and light gray - Color[] backgrounds = { Color.White, Color.LightGray }; + // Define the two background colors to alternate between + Color[] backgroundColors = new Color[] { Color.White, Color.LightGray }; - // Loop through each codetext, generate a barcode, and save it with the appropriate background - for (int i = 0; i < codes.Length; i++) + // Loop to generate a small set of barcodes (5 samples) + for (int i = 0; i < 5; i++) { - string code = codes[i]; + // Build the text to encode for the current barcode + string codeText = $"Sample{i + 1}"; - // Initialize the barcode generator with Code128 symbology and the current codetext - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, code)) + // Determine the file path for the generated image + string filePath = Path.Combine(outputDir, $"barcode_{i + 1}.png"); + + // Initialize the barcode generator with Code128 symbology and the sample text + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, codeText)) { - // Keep the bar (foreground) color constant (black) + // Set the bar (foreground) color to black (constant for all barcodes) generator.Parameters.Barcode.BarColor = Color.Black; - // Apply alternating background color based on the current index - generator.Parameters.BackColor = backgrounds[i % backgrounds.Length]; - - // Construct the output file name (e.g., barcode_0.png) - string fileName = $"barcode_{i}.png"; + // Alternate the background color based on the loop index + generator.Parameters.BackColor = backgroundColors[i % backgroundColors.Length]; - // Save the generated barcode image to disk - generator.Save(fileName); - - // Log the saved file and its background color to the console - Console.WriteLine($"Saved {fileName} with background {generator.Parameters.BackColor}"); + // Save the barcode image as a PNG file + generator.Save(filePath, BarCodeImageFormat.Png); } } + + // Inform the user that generation is complete + Console.WriteLine("Barcode images have been generated in the 'Barcodes' folder."); } } \ No newline at end of file diff --git a/barcode-color-customization/use-loop-to-generate-one-hundred-barcodes-each-with-unique-random-background-color.cs b/barcode-color-customization/use-loop-to-generate-one-hundred-barcodes-each-with-unique-random-background-color.cs index 9563ac1..71e6b43 100644 --- a/barcode-color-customization/use-loop-to-generate-one-hundred-barcodes-each-with-unique-random-background-color.cs +++ b/barcode-color-customization/use-loop-to-generate-one-hundred-barcodes-each-with-unique-random-background-color.cs @@ -1,7 +1,8 @@ -// Title: Generate multiple barcodes with random background colors -// Description: Demonstrates creating barcode images, each with a unique random background color, and saving them as PNG files. +// Title: Generate 100 Barcodes with Random Background Colors +// Description: This example creates 100 Code128 barcodes, each saved as a PNG with a unique random background color. +// Category-Description: Demonstrates Aspose.BarCode generation for bulk barcode creation. It uses BarcodeGenerator, EncodeTypes, and BarCodeImageFormat classes to produce images. Typical use cases include batch processing of product codes, inventory labeling, or testing visual variations. Developers often need loops, randomization, and file handling to automate large‑scale barcode output. // Prompt: Use a loop to generate one hundred barcodes each with a unique random background color. -// Tags: barcode, code128, random background, png, aspose.barcode, aspose.drawing +// Tags: barcode symbology, generation, png, random background, loop, aspose.barcode, aspose.drawing, code128 using System; using System.IO; @@ -10,55 +11,51 @@ using Aspose.Drawing; /// -/// Example program that generates barcode images with random background colors. +/// Generates a set of barcode images with random background colors using Aspose.BarCode. /// class Program { /// - /// Entry point of the application. Generates a set of barcode PNG files with random backgrounds. + /// Entry point of the application. Creates 100 Code128 barcodes, each with a distinct random background color, + /// and saves them as PNG files in the "Barcodes" directory. /// static void Main() { - // Define the output directory for generated barcode images + // Ensure the output directory exists string outputDir = "Barcodes"; + Directory.CreateDirectory(outputDir); - // Ensure the output directory exists; create it if it does not - if (!Directory.Exists(outputDir)) - { - Directory.CreateDirectory(outputDir); - } - - // Initialize a random number generator for creating random colors - Random rnd = new Random(); + // Random number generator for background colors + Random rand = new Random(); - // Generate a safe sample of 10 barcodes (large batches should be limited) - for (int i = 1; i <= 10; i++) + // Loop to generate 100 barcodes + for (int i = 0; i < 100; i++) { - // Create a unique code text for each barcode (e.g., Sample1, Sample2, ...) - string codeText = $"Sample{i}"; + // Build a unique text value for the barcode (e.g., Code001, Code002, ...) + string codeText = $"Code{i:D3}"; - // Generate a random background color using RGB components + // Generate a random RGB color for the barcode background Aspose.Drawing.Color bgColor = Aspose.Drawing.Color.FromArgb( - rnd.Next(256), // Red component (0-255) - rnd.Next(256), // Green component (0-255) - rnd.Next(256) // Blue component (0-255) + rand.Next(256), // Red component (0‑255) + rand.Next(256), // Green component (0‑255) + rand.Next(256) // Blue component (0‑255) ); - // Use a using block to ensure the BarcodeGenerator is properly disposed + // Initialize the barcode generator with Code128 symbology and the unique text using (var generator = new BarcodeGenerator(EncodeTypes.Code128, codeText)) { - // Apply the randomly generated background color to the barcode + // Apply the random background color to the barcode image generator.Parameters.BackColor = bgColor; - // Construct the full file path for the PNG output - string filePath = Path.Combine(outputDir, $"barcode_{i}.png"); + // Define the full file path for the PNG output + string filePath = Path.Combine(outputDir, $"barcode_{i:D3}.png"); - // Save the barcode image in PNG format + // Save the generated barcode as a PNG file generator.Save(filePath, BarCodeImageFormat.Png); } } - // Inform the user that barcode generation has completed - Console.WriteLine("Barcode generation completed."); + // Notify the user that generation is complete + Console.WriteLine("Generated 100 barcode images in the 'Barcodes' folder."); } } \ No newline at end of file diff --git a/barcode-color-customization/use-same-barcodegenerator-to-produce-red-bar-code39-barcode-then-change-to-blue-bar-and-save-again.cs b/barcode-color-customization/use-same-barcodegenerator-to-produce-red-bar-code39-barcode-then-change-to-blue-bar-and-save-again.cs index 0ece0f1..d77f81b 100644 --- a/barcode-color-customization/use-same-barcodegenerator-to-produce-red-bar-code39-barcode-then-change-to-blue-bar-and-save-again.cs +++ b/barcode-color-customization/use-same-barcodegenerator-to-produce-red-bar-code39-barcode-then-change-to-blue-bar-and-save-again.cs @@ -1,7 +1,8 @@ -// Title: Generate Code39 barcode with red and blue bars -// Description: Demonstrates creating a Code39 barcode, saving it with a red bar color, then changing to blue and saving again. +// Title: Generate Code39 barcode with red and blue bars using Aspose.BarCode +// Description: Demonstrates how to create a Code39 barcode, set its bar color to red, save it, then change the color to blue and save again. +// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, illustrating how to customize barcode appearance using the BarcodeGenerator class and its Parameters.Barcode properties. Typical use cases include generating barcodes with specific color schemes for branding or visual distinction. Developers often need to modify bar colors, sizes, and formats before saving to image files. // Prompt: Use the same BarcodeGenerator to produce a red‑bar Code39 barcode, then change to blue‑bar and save again. -// Tags: barcode, code39, color, generation, png, aspose.barcode, aspose.drawing +// Tags: code39, barcode generation, color customization, png, aspose.barcode, aspose.drawing using System; using Aspose.BarCode; @@ -9,27 +10,30 @@ using Aspose.Drawing; /// -/// Example program that generates a Code39 barcode in two colors. +/// Demonstrates generating a Code39 barcode with different bar colors and saving them as PNG images. /// class Program { /// - /// Entry point. Generates a Code39 barcode, saves it with red bars, then with blue bars. + /// Entry point of the example. Creates a barcode, changes its bar color, and saves the images. /// static void Main() { - // Initialize the barcode generator with Code39 symbology and sample text - using (var generator = new BarcodeGenerator(EncodeTypes.Code39, "ABC-123")) + // Initialize a BarcodeGenerator for Code39 with the sample text "123ABC" + using (var generator = new BarcodeGenerator(EncodeTypes.Code39, "123ABC")) { - // Set the bar color to red - generator.Parameters.Barcode.BarColor = Color.Red; - // Save the barcode image as PNG + // Set the bar (foreground) color to red + generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Red; + // Save the red barcode as a PNG file generator.Save("code39_red.png"); // Change the bar color to blue - generator.Parameters.Barcode.BarColor = Color.Blue; - // Save the barcode image again with the new color + generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Blue; + // Save the blue barcode as a PNG file generator.Save("code39_blue.png"); } + + // Inform the user that the barcode images have been generated + Console.WriteLine("Red and blue Code39 barcodes have been generated."); } } \ No newline at end of file diff --git a/barcode-color-customization/verify-that-generated-png-file-contains-exact-rgb-values-specified-for-each-color-property.cs b/barcode-color-customization/verify-that-generated-png-file-contains-exact-rgb-values-specified-for-each-color-property.cs index 4b4ce8d..e77a381 100644 --- a/barcode-color-customization/verify-that-generated-png-file-contains-exact-rgb-values-specified-for-each-color-property.cs +++ b/barcode-color-customization/verify-that-generated-png-file-contains-exact-rgb-values-specified-for-each-color-property.cs @@ -1,83 +1,99 @@ -// Title: Barcode generation with custom colors and pixel verification -// Description: Demonstrates creating a Code128 barcode PNG with specific bar and background colors, then verifies that the image contains only those RGB values. +// Title: Generate and verify a Code128 barcode PNG with custom colors +// Description: This example creates a Code128 barcode, sets specific foreground and background colors, saves it as a PNG, and verifies that the image contains only those exact RGB values. +// Category-Description: Demonstrates Aspose.BarCode barcode generation and image verification using Aspose.Drawing. It covers setting bar and background colors via BarcodeGenerator, saving to PNG, and pixel‑level validation. Developers working with barcode rendering, custom color schemes, or automated image testing will find this pattern useful. // Prompt: Verify that the generated PNG file contains the exact RGB values specified for each color property. -// Tags: barcode symbology, generation, png, color verification, aspose.barcode, aspose.drawing +// Tags: barcode symbology, color customization, png output, aspose.barcode, aspose.drawing, verification using System; using System.IO; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; -using Aspose.Drawing.Imaging; /// /// Example program that generates a Code128 barcode with custom colors, -/// saves it as PNG, and verifies the pixel colors match the specified values. +/// saves it as a PNG file, and validates the pixel colors. /// class Program { /// - /// Entry point. Generates the barcode, saves it, and validates pixel colors. + /// Entry point of the example. Generates the barcode, saves it, + /// and checks that only the specified foreground and background colors are present. /// static void Main() { - // Define the output file path for the generated PNG - const string outputPath = "barcode.png"; + // Define the output PNG file path. + string outputPath = "barcode.png"; - // Define the expected bar (foreground) and background colors using ARGB values - Color expectedBarColor = Color.FromArgb(255, 255, 0, 0); // Red - Color expectedBackColor = Color.FromArgb(255, 200, 200, 200); // Light gray - - // Create and configure the barcode generator - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "Test123")) + // Create and configure the barcode generator. + using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456")) { - // Apply the custom colors to the barcode parameters - generator.Parameters.Barcode.BarColor = expectedBarColor; // Set bar (foreground) color - generator.Parameters.BackColor = expectedBackColor; // Set background color - - // Save the barcode image as a PNG file + // Set the foreground (bar) color to blue (RGB 0,0,255). + generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.FromArgb(255, 0, 0, 255); + // Set the background color to yellow (RGB 255,255,0). + generator.Parameters.BackColor = Aspose.Drawing.Color.FromArgb(255, 255, 255, 0); + // Save the barcode as a PNG image. generator.Save(outputPath, BarCodeImageFormat.Png); } - // Verify that the PNG file was created successfully + // Verify that the PNG file was created. if (!File.Exists(outputPath)) { - Console.WriteLine($"Error: File '{outputPath}' was not created."); + Console.WriteLine("Error: Generated file not found."); return; } - // Load the generated image as a bitmap for pixel-level inspection - using (var image = Aspose.Drawing.Image.FromFile(outputPath) as Bitmap) + // Load the generated image using Aspose.Drawing. + using (var bitmap = Aspose.Drawing.Image.FromFile(outputPath) as Aspose.Drawing.Bitmap) { - if (image == null) + if (bitmap == null) { - Console.WriteLine("Error: Unable to load the image as a bitmap."); + Console.WriteLine("Error: Unable to load image as bitmap."); return; } - bool verificationPassed = true; + // Define the expected colors for verification. + var expectedBarColor = Aspose.Drawing.Color.FromArgb(255, 0, 0, 255); // Blue + var expectedBackColor = Aspose.Drawing.Color.FromArgb(255, 255, 255, 0); // Yellow + + bool barFound = false; + bool backFound = false; - // Iterate over each pixel to ensure it matches one of the expected colors - for (int y = 0; y < image.Height; y++) + // Scan every pixel in the bitmap. + for (int y = 0; y < bitmap.Height; y++) { - for (int x = 0; x < image.Width; x++) + for (int x = 0; x < bitmap.Width; x++) { - Color pixelColor = image.GetPixel(x, y); + var pixel = bitmap.GetPixel(x, y); + int pixelArgb = pixel.ToArgb(); - // Pixel must be either the bar color or the background color - if (!pixelColor.Equals(expectedBarColor) && !pixelColor.Equals(expectedBackColor)) + if (pixelArgb == expectedBarColor.ToArgb()) { - Console.WriteLine($"Mismatch at ({x},{y}): Expected BarColor or BackColor, found R={pixelColor.R}, G={pixelColor.G}, B={pixelColor.B}"); - verificationPassed = false; - // Continue checking remaining pixels to report all mismatches + barFound = true; + } + else if (pixelArgb == expectedBackColor.ToArgb()) + { + backFound = true; + } + else + { + // An unexpected color was found; report and abort verification. + Console.WriteLine($"Unexpected color at ({x},{y}): ARGB={pixelArgb:X8}"); + Console.WriteLine("Verification failed."); + return; } } } - // Output the verification result - Console.WriteLine(verificationPassed - ? "Verification passed: All pixels match the specified BarColor or BackColor." - : "Verification failed: Some pixels do not match the specified colors."); + // Report the verification result based on the presence of both colors. + if (barFound && backFound) + { + Console.WriteLine("Verification succeeded: image contains only the specified colors."); + } + else + { + Console.WriteLine("Verification failed: expected colors not found in the image."); + } } } } \ No newline at end of file diff --git a/barcode-color-customization/verify-that-generated-png-image-contains-specified-bar-color-using-pixel-inspection.cs b/barcode-color-customization/verify-that-generated-png-image-contains-specified-bar-color-using-pixel-inspection.cs index 9f77eb5..5b1288d 100644 --- a/barcode-color-customization/verify-that-generated-png-image-contains-specified-bar-color-using-pixel-inspection.cs +++ b/barcode-color-customization/verify-that-generated-png-image-contains-specified-bar-color-using-pixel-inspection.cs @@ -1,73 +1,64 @@ -// Title: Barcode generation with custom bar color and verification -// Description: Demonstrates creating a Code128 barcode PNG with a blue bar color and verifies the color by inspecting pixels. +// Title: Verify barcode bar color in generated PNG +// Description: Demonstrates generating a Code128 barcode with a custom bar color and programmatically confirming the color exists in the saved PNG image. +// Category-Description: This example belongs to the Aspose.BarCode image generation and recognition category, illustrating how to use BarcodeGenerator to set visual properties (e.g., BarColor) and how to employ Aspose.Drawing.Bitmap for pixel-level inspection. Developers often need to customize barcode appearance and validate output images in automated tests or CI pipelines. // Prompt: Verify that the generated PNG image contains the specified bar color using pixel inspection. -// Tags: barcode, code128, color, png, verification, aspose.barcode, aspose.drawing +// Tags: barcode, code128, barcolor, png, pixel-inspection, aspose.barcode, aspose.drawing, image-generation using System; using System.IO; using Aspose.BarCode; using Aspose.BarCode.Generation; using Aspose.Drawing; -using Aspose.Drawing.Imaging; +using Aspose.BarCode.BarCodeRecognition; /// -/// Generates a Code128 barcode with a custom bar color, saves it as a PNG, +/// Generates a Code128 barcode with a custom bar color, saves it as PNG, /// and verifies that the specified color appears in the resulting image. /// class Program { /// - /// Entry point of the example. Performs barcode generation, saving, and color verification. + /// Entry point of the example. Performs barcode creation, saving, and color verification. /// static void Main() { - // -------------------------------------------------------------------- - // Define output file path - // -------------------------------------------------------------------- + // Define the output file path for the generated barcode image. string outputPath = "barcode.png"; - // -------------------------------------------------------------------- - // Desired bar color (blue) - // -------------------------------------------------------------------- - Color barColor = Color.Blue; - - // -------------------------------------------------------------------- - // Generate barcode with the specified bar color and save as PNG - // -------------------------------------------------------------------- - using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456")) + // Create a barcode generator for the Code128 symbology. + using (var generator = new BarcodeGenerator(EncodeTypes.Code128)) { - generator.Parameters.Barcode.BarColor = barColor; - generator.Save(outputPath); + // Set the text to be encoded in the barcode. + generator.CodeText = "1234567890"; + + // Apply a custom bar color (red) to the barcode. + generator.Parameters.Barcode.BarColor = Aspose.Drawing.Color.Red; + + // Save the generated barcode as a PNG image. + generator.Save(outputPath, BarCodeImageFormat.Png); } - // -------------------------------------------------------------------- - // Verify that the generated image file exists - // -------------------------------------------------------------------- + // Ensure the image file was created before attempting verification. if (!File.Exists(outputPath)) { - Console.WriteLine("Barcode image file was not created."); + Console.WriteLine("Error: Barcode image file was not created."); return; } - // -------------------------------------------------------------------- - // Scan the image pixel by pixel to find the expected bar color - // -------------------------------------------------------------------- + // Flag indicating whether the expected color was found in the image. bool colorFound = false; - using (var image = Image.FromFile(outputPath) as Bitmap) - { - if (image == null) - { - Console.WriteLine("Failed to load the barcode image as a bitmap."); - return; - } + Aspose.Drawing.Color expectedColor = Aspose.Drawing.Color.Red; - // Iterate over each pixel until the color is found - for (int y = 0; y < image.Height && !colorFound; y++) + // Load the PNG image for pixel-level inspection. + using (var bitmap = new Bitmap(outputPath)) + { + // Iterate over each pixel until the expected color is found. + for (int y = 0; y < bitmap.Height && !colorFound; y++) { - for (int x = 0; x < image.Width && !colorFound; x++) + for (int x = 0; x < bitmap.Width && !colorFound; x++) { - // Compare the pixel's ARGB value with the expected bar color's ARGB value - if (image.GetPixel(x, y).ToArgb() == barColor.ToArgb()) + Aspose.Drawing.Color pixelColor = bitmap.GetPixel(x, y); + if (pixelColor.ToArgb() == expectedColor.ToArgb()) { colorFound = true; } @@ -75,11 +66,9 @@ static void Main() } } - // -------------------------------------------------------------------- - // Output verification result - // -------------------------------------------------------------------- + // Output the verification result to the console. Console.WriteLine(colorFound - ? "Verification succeeded: bar color is present in the image." - : "Verification failed: bar color not found in the image."); + ? "Verification succeeded: Bar color is present in the image." + : "Verification failed: Bar color not found in the image."); } } \ No newline at end of file