### Windows Forms Integration Example Source: https://context7.com/barnhill/barcodelib/llms.txt A comprehensive example demonstrating how to integrate barcode generation into a Windows Forms application, including UI elements and customization options. ```APIDOC ## Complete Windows Forms Integration Example Full example showing barcode generation in a Windows Forms application with all customization options. ### Method Implicit (Code Execution within a Windows Forms Application) ### Endpoint N/A ### Parameters N/A ### Request Example ```csharp using BarcodeStandard; using SkiaSharp; using System; using System.Drawing; using System.IO; using System.Windows.Forms; public class BarcodeGeneratorForm : Form { private readonly Barcode _barcode = new Barcode(); private PictureBox _pictureBox; private TextBox _dataTextBox; private ComboBox _typeComboBox; public BarcodeGeneratorForm() { InitializeComponents(); } private void GenerateBarcode() { try { // Configure barcode settings _barcode.IncludeLabel = true; _barcode.Alignment = AlignmentPositions.Center; _barcode.ForeColor = SKColors.Black; _barcode.BackColor = SKColors.White; _barcode.LabelFont = new SKFont { Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold), Size = 28 }; // Parse selected type var selectedType = (Type)Enum.Parse( typeof(Type), _typeComboBox.SelectedItem.ToString() ); // Encode the barcode var skImage = _barcode.Encode( selectedType, _dataTextBox.Text, _barcode.ForeColor, _barcode.BackColor, 400, 150 ); // Convert SkiaSharp image to Windows Forms Image using var encoded = skImage.Encode(); using var stream = encoded.AsStream(); _pictureBox.Image = Image.FromStream(stream); // Display encoding time MessageBox.Show($"Encoded in {_barcode.EncodingTime:F2}ms"); } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } } private void SaveBarcode(string filename, SaveTypes format) { _barcode.SaveImage(filename, format); } private void ExportToJson(string filename) { string json = _barcode.ToJson(includeImage: true); File.WriteAllText(filename, json); } protected override void Dispose(bool disposing) { if (disposing) { _barcode?.Dispose(); } base.Dispose(disposing); } } ``` ### Response N/A (UI Interaction and Image Display) #### Success Response - **Barcode Image**: Displayed in a `PictureBox` control. - **Encoding Time**: Shown in a `MessageBox`. - **Saved Barcode**: Saved to a file in the specified format. - **JSON Export**: Saved to a JSON file. ``` -------------------------------- ### Export and Import Barcode Configurations via JSON Source: https://context7.com/barnhill/barcodelib/llms.txt This example demonstrates how to serialize barcode configurations and images to JSON format, both with and without the embedded image data. It also shows how to load barcode data from a JSON file and reconstruct the barcode object or image from the loaded data. ```csharp using BarcodeStandard; using System.IO; using var barcode = new Barcode(); barcode.IncludeLabel = true; barcode.Encode(Type.Ean13, "5901234123457", 300, 150); // Export to JSON with embedded image string jsonWithImage = barcode.ToJson(includeImage: true); // Export to JSON without image (smaller file) string jsonNoImage = barcode.ToJson(includeImage: false); // Save JSON to file File.WriteAllText("barcode.json", jsonWithImage); // Load barcode configuration from JSON using var fileStream = File.OpenRead("barcode.json"); SaveData loadedData = Barcode.FromJson(fileStream); // Access loaded properties Console.WriteLine($"Type: {loadedData.Type}"); Console.WriteLine($"Data: {loadedData.RawData}"); Console.WriteLine($"Encoded: {loadedData.EncodedValue}"); Console.WriteLine($"Dimensions: {loadedData.ImageWidth}x{loadedData.ImageHeight}"); // Reconstruct image from SaveData if (loadedData.Image != null) { var reconstructedImage = Barcode.GetImageFromSaveData(loadedData); } ``` -------------------------------- ### Windows Forms Barcode Integration Example (C#) Source: https://context7.com/barnhill/barcodelib/llms.txt Provides a comprehensive example of integrating barcode generation into a Windows Forms application. This snippet demonstrates setting up a form with UI elements for barcode data input and type selection, configuring various barcode properties like color and font, encoding the barcode, displaying it in a PictureBox, and handling saving and exporting functionalities. ```csharp using BarcodeStandard; using SkiaSharp; using System; using System.Drawing; using System.IO; using System.Windows.Forms; public class BarcodeGeneratorForm : Form { private readonly Barcode _barcode = new(); private PictureBox _pictureBox; private TextBox _dataTextBox; private ComboBox _typeComboBox; public BarcodeGeneratorForm() { InitializeComponents(); } private void GenerateBarcode() { try { // Configure barcode settings _barcode.IncludeLabel = true; _barcode.Alignment = AlignmentPositions.Center; _barcode.ForeColor = SKColors.Black; _barcode.BackColor = SKColors.White; _barcode.LabelFont = new SKFont { Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold), Size = 28 }; // Parse selected type var selectedType = (Type)Enum.Parse( typeof(Type), _typeComboBox.SelectedItem.ToString() ); // Encode the barcode var skImage = _barcode.Encode( selectedType, _dataTextBox.Text, _barcode.ForeColor, _barcode.BackColor, 400, 150 ); // Convert SkiaSharp image to Windows Forms Image using var encoded = skImage.Encode(); using var stream = encoded.AsStream(); _pictureBox.Image = Image.FromStream(stream); // Display encoding time MessageBox.Show($"Encoded in {_barcode.EncodingTime:F2}ms"); } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}"); } } private void SaveBarcode(string filename, SaveTypes format) { _barcode.SaveImage(filename, format); } private void ExportToJson(string filename) { string json = _barcode.ToJson(includeImage: true); File.WriteAllText(filename, json); } protected override void Dispose(bool disposing) { if (disposing) { _barcode?.Dispose(); } base.Dispose(disposing); } } ``` -------------------------------- ### Generate Barcode Image Source: https://github.com/barnhill/barcodelib/blob/master/README.md Shows how to configure a Barcode instance and generate an image. This example sets the label visibility and uses the Encode method to produce a UPC-A barcode with specific colors and dimensions. ```csharp var b = new Barcode(); b.IncludeLabel = true; var img = b.Encode(Type.UpcA, "038000356216", SKColors.Black, SKColors.White, 290, 120); ``` -------------------------------- ### Barcode Encoding Time and Diagnostics (C#) Source: https://context7.com/barnhill/barcodelib/llms.txt Demonstrates how to access encoding performance metrics, library version, and encoded data for debugging. It shows how to encode a barcode, retrieve the encoding time in milliseconds, get the library's version, and access the binary representation of the encoded value. It also includes an example of retrieving the country code for EAN/UPC barcodes. ```csharp using BarcodeStandard; using System; using var barcode = new Barcode(); barcode.IncludeLabel = true; // Perform encoding barcode.Encode(Type.Code128, "TIMING-TEST-12345", 400, 150); // Access encoding time in milliseconds double encodingTime = barcode.EncodingTime; Console.WriteLine($"Encoding took: {encodingTime:F2} ms"); // Access library version (static property) Version version = Barcode.Version; Console.WriteLine($"BarcodeLib Version: {version}"); // Access encoded binary value for debugging string binaryEncoding = barcode.EncodedValue; Console.WriteLine($"Binary: {binaryEncoding}"); // Access country code for EAN/UPC (when applicable) barcode.Encode(Type.Ean13, "5901234123457", 300, 100); string countryCode = barcode.CountryAssigningManufacturerCode; Console.WriteLine($"Country: {countryCode}"); ``` -------------------------------- ### Barcode Symbology Types and Encoding Example (C#) Source: https://context7.com/barnhill/barcodelib/llms.txt Demonstrates the various barcode symbology types supported by the library, including UPC, EAN, Code 128, Code 39, and others. It also provides an example of how to encode multiple barcode types and save them as images. ```csharp using BarcodeStandard; // UPC Family Type.UpcA // 12-digit Universal Product Code Type.UpcE // 8-digit compressed UPC Type.UpcSupplemental2Digit // 2-digit add-on Type.UpcSupplemental5Digit // 5-digit add-on // EAN Family Type.Ean13 // 13-digit European Article Number Type.Ean8 // 8-digit EAN Type.Jan13 // Japanese Article Number (same as EAN-13) Type.Isbn // International Standard Book Number Type.Bookland // Bookland EAN (alias for ISBN) // Code 128 Family Type.Code128 // Auto-selects optimal Code 128 subset Type.Code128A // Code 128 Subset A (control chars + uppercase) Type.Code128B // Code 128 Subset B (all printable ASCII) Type.Code128C // Code 128 Subset C (numeric pairs only) // Code 39 Family Type.Code39 // Standard Code 39 Type.Code39Extended // Full ASCII Code 39 Type.Code39Mod43 // Code 39 with Mod 43 check digit Type.Logmars // LOGMARS (same encoding as Code 39) // Industrial Codes Type.Interleaved2Of5 // Interleaved 2 of 5 Type.Interleaved2Of5Mod10 // With Mod 10 check digit Type.Standard2Of5 // Standard 2 of 5 Type.Standard2Of5Mod10 // With Mod 10 check digit Type.Industrial2Of5 // Industrial 2 of 5 Type.IATA2of5 // IATA 2 of 5 Type.Itf14 // ITF-14 shipping container code // Other Symbologies Type.Code11 // Code 11 (USD-8) Type.Code93 // Code 93 Type.Codabar // Codabar (NW-7) Type.PostNet // USPS PostNet Type.Telepen // Telepen Type.Fim // Facing Identification Mark Type.Pharmacode // Pharmaceutical binary code Type.MsiMod10 // MSI with Mod 10 check Type.MsiMod11 // MSI with Mod 11 check Type.Msi2Mod10 // MSI with double Mod 10 Type.MsiMod11Mod10 // MSI with Mod 11 and Mod 10 // Example: Generate multiple barcode types var symbologies = new[] { Type.Code128, Type.UpcA, Type.Ean13, Type.Code39 }; foreach (var symbology in symbologies) { using var b = new Barcode(); b.IncludeLabel = true; var img = b.Encode(symbology, "123456789012", 300, 100); b.SaveImage($"barcode_{symbology}.png", SaveTypes.Png); } ``` -------------------------------- ### Static DoEncode Methods - C# Source: https://context7.com/barnhill/barcodelib/llms.txt Provides examples of using the static DoEncode methods for quick, one-off barcode generation. It covers simple encoding, encoding with XML output, including labels, specifying dimensions, and using custom colors. ```csharp using BarcodeStandard; using SkiaSharp; using System.Drawing; // Simple static encode SKImage img1 = Barcode.DoEncode(Type.Code128, "ProductCode123"); // With XML output string xml; SKImage img2 = Barcode.DoEncode(Type.Ean13, "5901234123457", out xml); // With label SKImage img3 = Barcode.DoEncode(Type.UpcA, "038000356216", includeLabel: true); // With label and dimensions SKImage img4 = Barcode.DoEncode( Type.Code39, "ABC-123", includeLabel: true, width: 400, height: 150 ); // Full parameters with colors SKImage img5 = Barcode.DoEncode( Type.Itf14, "00012345678905", includeLabel: true, drawColor: Color.Black, backColor: Color.White, width: 500, height: 200 ); // Full parameters with XML output string xmlData; SKImage img6 = Barcode.DoEncode( Type.Ean8, "12345670", includeLabel: true, drawColor: Color.DarkGreen, backColor: Color.LightYellow, width: 200, height: 100, xml: out xmlData ); ``` -------------------------------- ### Initialize Barcode Object Source: https://github.com/barnhill/barcodelib/blob/master/README.md Demonstrates the available constructors for the Barcode class. Users can instantiate the class with no arguments, with data, or with both data and a specific symbology type. ```csharp Barcode(); Barcode(string); Barcode(string, Type); ``` -------------------------------- ### Barcode Class Constructor Source: https://context7.com/barnhill/barcodelib/llms.txt Demonstrates how to instantiate the Barcode class using different constructors, with or without initial data and symbology. ```APIDOC ## Barcode Class Constructor The main entry point for barcode generation. Create instances with or without initial data and symbology type specification. ### Method N/A (Constructor) ### Endpoint N/A (Class Constructor) ### Parameters N/A (Constructor Overloads) ### Request Example ```csharp using BarcodeStandard; using SkiaSharp; // Default constructor - set properties later var barcode1 = new Barcode(); barcode1.RawData = "123456789012"; barcode1.EncodedType = Type.UpcA; // Constructor with data only var barcode2 = new Barcode("123456789012"); // Constructor with data and type - encodes immediately var barcode3 = new Barcode("123456789012", Type.UpcA); // Don't forget to dispose when done barcode1.Dispose(); barcode2.Dispose(); barcode3.Dispose(); ``` ### Response N/A (Constructor) ``` -------------------------------- ### Barcode Class Constructor - C# Source: https://context7.com/barnhill/barcodelib/llms.txt Demonstrates the different ways to instantiate the Barcode class, including default construction, with data only, or with both data and symbology type. It also shows how to set properties and dispose of the instance. ```csharp using BarcodeStandard; using SkiaSharp; // Default constructor - set properties later var barcode1 = new Barcode(); barcode1.RawData = "123456789012"; barcode1.EncodedType = Type.UpcA; // Constructor with data only var barcode2 = new Barcode("123456789012"); // Constructor with data and type - encodes immediately var barcode3 = new Barcode("123456789012", Type.UpcA); // Don't forget to dispose when done barcode1.Dispose(); barcode2.Dispose(); barcode3.Dispose(); ``` -------------------------------- ### Barcode Image Format Configuration and Access (C#) Source: https://context7.com/barnhill/barcodelib/llms.txt Explains how to configure the output image format (PNG, JPEG, WebP) for barcode generation. It also shows how to access the encoded image data directly as a SKImage, a byte array, or in a specific format using GetImageData. ```csharp using BarcodeStandard; using SkiaSharp; using System.IO; using var barcode = new Barcode(); // Set default image format for encoding barcode.ImageFormat = SKEncodedImageFormat.Png; // Default is Jpeg barcode.ImageFormat = SKEncodedImageFormat.Jpeg; barcode.ImageFormat = SKEncodedImageFormat.Webp; // Generate barcode barcode.Encode(Type.Code128, "FORMAT-TEST", 300, 100); // Access encoded image directly SKImage image = barcode.EncodedImage; // Get image as byte array (uses configured ImageFormat) byte[] imageBytes = barcode.EncodedImageBytes; // Get image data in specific format byte[] jpgBytes = barcode.GetImageData(SaveTypes.Jpg); byte[] pngBytes = barcode.GetImageData(SaveTypes.Png); byte[] webpBytes = barcode.GetImageData(SaveTypes.Webp); // Convert to SkiaSharp bitmap for further manipulation using var bitmap = SKBitmap.FromImage(barcode.EncodedImage); // Encode image to stream using var ms = new MemoryStream(); image.Encode(SKEncodedImageFormat.Png, 100).SaveTo(ms); ``` -------------------------------- ### Export and Import Barcode Configurations via XML Source: https://context7.com/barnhill/barcodelib/llms.txt This snippet illustrates how to serialize barcode configurations and images to XML format, including options to embed Base64 encoded image data or exclude it. It also covers loading barcode data from an XML file and using it to reconstruct a barcode object with its original settings. ```csharp using BarcodeStandard; using System.IO; using var barcode = new Barcode(); barcode.IncludeLabel = true; barcode.ForeColor = SkiaSharp.SKColors.Navy; barcode.Encode(Type.Code39, "XML-TEST", 350, 120); // Export to XML with embedded Base64 image string xmlWithImage = barcode.ToXml(includeImage: true); // Export to XML without image string xmlNoImage = barcode.ToXml(includeImage: false); // Save XML to file File.WriteAllText("barcode.xml", xmlWithImage); // Load barcode configuration from XML using var xmlStream = File.OpenRead("barcode.xml"); SaveData loadedData = Barcode.FromXml(xmlStream); // Recreate barcode from loaded data using var newBarcode = new Barcode(); newBarcode.RawData = loadedData.RawData; newBarcode.IncludeLabel = loadedData.IncludeLabel; newBarcode.Width = loadedData.ImageWidth; newBarcode.Height = loadedData.ImageHeight; // Re-encode with original settings ``` -------------------------------- ### Static DoEncode Methods Source: https://context7.com/barnhill/barcodelib/llms.txt Provides static convenience methods for quick one-off barcode generation without managing instance lifecycle. ```APIDOC ## Static DoEncode Methods Convenience static methods for quick one-off barcode generation without managing instance lifecycle. ### Method Static Method of Barcode class ### Endpoint N/A (Static Method) ### Parameters - **type** (Type) - Required - The barcode symbology type. - **rawData** (string) - Required - The data to encode. - **includeLabel** (bool) - Optional - Whether to include a human-readable label. - **width** (int) - Optional - The desired width of the barcode image. - **height** (int) - Optional - The desired height of the barcode image. - **drawColor** (Color) - Optional - The foreground color of the barcode. - **backColor** (Color) - Optional - The background color of the barcode. - **xml** (out string) - Optional - An output parameter to receive XML data representing the barcode. ### Request Example ```csharp using BarcodeStandard; using SkiaSharp; using System.Drawing; // Simple static encode SKImage img1 = Barcode.DoEncode(Type.Code128, "ProductCode123"); // With XML output string xml; SKImage img2 = Barcode.DoEncode(Type.Ean13, "5901234123457", out xml); // With label SKImage img3 = Barcode.DoEncode(Type.UpcA, "038000356216", includeLabel: true); // With label and dimensions SKImage img4 = Barcode.DoEncode( Type.Code39, "ABC-123", includeLabel: true, width: 400, height: 150 ); // Full parameters with colors SKImage img5 = Barcode.DoEncode( Type.Itf14, "00012345678905", includeLabel: true, drawColor: Color.Black, backColor: Color.White, width: 500, height: 200 ); // Full parameters with XML output string xmlData; SKImage img6 = Barcode.DoEncode( Type.Ean8, "12345670", includeLabel: true, drawColor: Color.DarkGreen, backColor: Color.LightYellow, width: 200, height: 100, xml: out xmlData ); ``` ### Response #### Success Response (SKImage) - **SKImage** - An SkiaSharp SKImage object representing the generated barcode. ``` -------------------------------- ### SaveImage Method - C# Source: https://context7.com/barnhill/barcodelib/llms.txt Demonstrates how to save generated barcode images to files in JPG, PNG, or WebP formats, or to a memory stream. It also shows how to retrieve the image data directly as a byte array. ```csharp using BarcodeStandard; using SkiaSharp; using System.IO; using var barcode = new Barcode(); barcode.IncludeLabel = true; barcode.Encode(Type.UpcA, "038000356216", 300, 120); // Save to file with specific format barcode.SaveImage("barcode.jpg", SaveTypes.Jpg); barcode.SaveImage("barcode.png", SaveTypes.Png); barcode.SaveImage("barcode.webp", SaveTypes.Webp); // Save to memory stream using var memoryStream = new MemoryStream(); barcode.SaveImage(memoryStream, SaveTypes.Png); byte[] imageBytes = memoryStream.ToArray(); // Get image data as byte array directly byte[] jpgData = barcode.GetImageData(SaveTypes.Jpg); byte[] pngData = barcode.GetImageData(SaveTypes.Png); ``` -------------------------------- ### Encoding Time and Diagnostics Source: https://context7.com/barnhill/barcodelib/llms.txt This section demonstrates how to access encoding performance metrics, library version, and encoded binary values for debugging purposes. ```APIDOC ## Encoding Time and Diagnostics Access encoding performance metrics and version information. ### Method Implicit (Code Execution) ### Endpoint N/A ### Parameters N/A ### Request Example ```csharp using BarcodeStandard; using System; using var barcode = new Barcode(); barcode.IncludeLabel = true; // Perform encoding barcode.Encode(Type.Code128, "TIMING-TEST-12345", 400, 150); // Access encoding time in milliseconds double encodingTime = barcode.EncodingTime; Console.WriteLine($"Encoding took: {encodingTime:F2} ms"); // Access library version (static property) Version version = Barcode.Version; Console.WriteLine($"BarcodeLib Version: {version}"); // Access encoded binary value for debugging string binaryEncoding = barcode.EncodedValue; Console.WriteLine($"Binary: {binaryEncoding}"); // Access country code for EAN/UPC (when applicable) barcode.Encode(Type.Ean13, "5901234123457", 300, 100); string countryCode = barcode.CountryAssigningManufacturerCode; Console.WriteLine($"Country: {countryCode}"); ``` ### Response N/A (Console Output) #### Success Response (Console Output) - **Encoding Time**: Time taken for encoding in milliseconds. - **BarcodeLib Version**: The current version of the BarcodeLib library. - **Binary**: The raw binary representation of the encoded barcode. - **Country**: The country code assigned to the manufacturer for EAN/UPC barcodes. ``` -------------------------------- ### XML Serialization Source: https://context7.com/barnhill/barcodelib/llms.txt Export and import barcode configurations and images using XML format. ```APIDOC ## XML Serialization Export and import barcode configurations and images using XML format. ### Method Not applicable (code example demonstrates library usage, not an API endpoint). ### Parameters #### Request Body Not applicable. ### Request Example ```csharp using BarcodeStandard; using System.IO; using var barcode = new Barcode(); barcode.IncludeLabel = true; barcode.ForeColor = SkiaSharp.SKColors.Navy; barcode.Encode(Type.Code39, "XML-TEST", 350, 120); // Export to XML with embedded Base64 image string xmlWithImage = barcode.ToXml(includeImage: true); // Export to XML without image string xmlNoImage = barcode.ToXml(includeImage: false); // Save XML to file File.WriteAllText("barcode.xml", xmlWithImage); // Load barcode configuration from XML using var xmlStream = File.OpenRead("barcode.xml"); SaveData loadedData = Barcode.FromXml(xmlStream); // Recreate barcode from loaded data using var newBarcode = new Barcode(); newBarcode.RawData = loadedData.RawData; newBarcode.IncludeLabel = loadedData.IncludeLabel; newBarcode.Width = loadedData.ImageWidth; newBarcode.Height = loadedData.ImageHeight; // Re-encode with original settings ``` ### Response Not applicable (this is a code example for library configuration). ``` -------------------------------- ### Color and Alignment Configuration Source: https://context7.com/barnhill/barcodelib/llms.txt Customize barcode appearance with foreground/background colors and alignment options. ```APIDOC ## Color and Alignment Configuration Customize barcode appearance with foreground/background colors and alignment options. ### Method Not applicable (code example demonstrates library usage, not an API endpoint). ### Parameters #### Request Body Not applicable. ### Request Example ```csharp using BarcodeStandard; using SkiaSharp; using var barcode = new Barcode(); // Set foreground (bars) color barcode.ForeColor = SKColors.DarkBlue; // Set background color barcode.BackColor = SKColors.White; // Alternative: use custom RGB colors barcode.ForeColor = new SKColor(0, 51, 102); // Dark blue barcode.BackColor = new SKColor(255, 255, 240); // Ivory // Set alignment within the image barcode.Alignment = AlignmentPositions.Center; // Default barcode.Alignment = AlignmentPositions.Left; barcode.Alignment = AlignmentPositions.Right; // Set dimensions barcode.Width = 400; barcode.Height = 150; // Generate the barcode var img = barcode.Encode(Type.Code128, "ALIGNED-LEFT"); ``` ### Response Not applicable (this is a code example for library configuration). ``` -------------------------------- ### JSON Serialization Source: https://context7.com/barnhill/barcodelib/llms.txt Export and import barcode configurations and images using JSON format. ```APIDOC ## JSON Serialization Export and import barcode configurations and images using JSON format. ### Method Not applicable (code example demonstrates library usage, not an API endpoint). ### Parameters #### Request Body Not applicable. ### Request Example ```csharp using BarcodeStandard; using System.IO; using var barcode = new Barcode(); barcode.IncludeLabel = true; barcode.Encode(Type.Ean13, "5901234123457", 300, 150); // Export to JSON with embedded image string jsonWithImage = barcode.ToJson(includeImage: true); // Export to JSON without image (smaller file) string jsonNoImage = barcode.ToJson(includeImage: false); // Save JSON to file File.WriteAllText("barcode.json", jsonWithImage); // Load barcode configuration from JSON using var fileStream = File.OpenRead("barcode.json"); SaveData loadedData = Barcode.FromJson(fileStream); // Access loaded properties Console.WriteLine($"Type: {loadedData.Type}"); Console.WriteLine($"Data: {loadedData.RawData}"); Console.WriteLine($"Encoded: {loadedData.EncodedValue}"); Console.WriteLine($"Dimensions: {loadedData.ImageWidth}x{loadedData.ImageHeight}"); // Reconstruct image from SaveData if (loadedData.Image != null) { var reconstructedImage = Barcode.GetImageFromSaveData(loadedData); } ``` ### Response Not applicable (this is a code example for library configuration). ``` -------------------------------- ### GenerateBarcode Method - C# Source: https://context7.com/barnhill/barcodelib/llms.txt Shows how to use the GenerateBarcode method to obtain the binary encoding string of a barcode without generating an image. It also demonstrates accessing the EncodedValue property after an encode operation. ```csharp using BarcodeStandard; using var barcode = new Barcode(); barcode.EncodedType = Type.Code128; // Generate binary encoding from data string binaryValue = barcode.GenerateBarcode("Hello123"); // Returns: "11010010000110100111001011110..." // Access the encoded value after encoding barcode.Encode(Type.UpcA, "038000356216"); Console.WriteLine(barcode.EncodedValue); // Output: binary string of 1s and 0s representing bars/spaces ``` -------------------------------- ### Encode Method - C# Source: https://context7.com/barnhill/barcodelib/llms.txt Illustrates the use of the Encode method to generate barcode images. It covers basic encoding with type and data, encoding with specified dimensions, colors, and a full encode with all parameters. It also shows encoding using pre-set properties. ```csharp using BarcodeStandard; using SkiaSharp; using var barcode = new Barcode(); // Basic encode with type and data SKImage img1 = barcode.Encode(Type.Code128, "Hello World"); // Encode with dimensions SKImage img2 = barcode.Encode(Type.UpcA, "038000356216", 290, 120); // Encode with colors SKImage img3 = barcode.Encode(Type.Ean13, "5901234123457", SKColors.Black, SKColors.White); // Full encode with all parameters SKImage img4 = barcode.Encode( Type.Code39, "ABC123", SKColors.DarkBlue, SKColors.White, 400, 150 ); // Encode using pre-set properties barcode.RawData = "12345678"; barcode.EncodedType = Type.Code128; barcode.Width = 300; barcode.Height = 100; SKImage img5 = barcode.Encode(); ``` -------------------------------- ### Label Configuration Source: https://context7.com/barnhill/barcodelib/llms.txt Configure human-readable labels below barcodes with custom fonts, alternate text, and positioning. ```APIDOC ## Label Configuration Configure human-readable labels below barcodes with custom fonts, alternate text, and positioning. ### Method Not applicable (code example demonstrates library usage, not an API endpoint). ### Parameters #### Request Body Not applicable. ### Request Example ```csharp using BarcodeStandard; using SkiaSharp; using var barcode = new Barcode(); // Enable label display barcode.IncludeLabel = true; // Set custom label font barcode.LabelFont = new SKFont { Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold), Size = 24 }; // Use alternate label text instead of raw data barcode.AlternateLabel = "Product: ABC-123"; // Encode and generate barcode.Encode(Type.Code128, "ABC123", 350, 150); // For UPC-A and EAN-13, labels are automatically formatted // with proper spacing around guard bars using var upcBarcode = new Barcode(); upcBarcode.IncludeLabel = true; upcBarcode.Encode(Type.UpcA, "038000356216", 290, 120); ``` ### Response Not applicable (this is a code example for library configuration). ``` -------------------------------- ### Bar Width and Aspect Ratio Source: https://context7.com/barnhill/barcodelib/llms.txt Control precise bar widths and maintain aspect ratios for scanner compatibility. ```APIDOC ## Bar Width and Aspect Ratio Control precise bar widths and maintain aspect ratios for scanner compatibility. ### Method Not applicable (code example demonstrates library usage, not an API endpoint). ### Parameters #### Request Body Not applicable. ### Request Example ```csharp using BarcodeStandard; using SkiaSharp; using var barcode = new Barcode(); // Set specific bar width in pixels // Width will be calculated automatically based on encoded data length barcode.BarWidth = 2; // Set aspect ratio - Height = Width / AspectRatio // Useful for maintaining scanner-friendly proportions barcode.AspectRatio = 2.0; // Height will be half the width // Encode - dimensions calculated automatically barcode.Encode(Type.Code128, "AutoWidth123"); // Access calculated dimensions Console.WriteLine($"Width: {barcode.Width}, Height: {barcode.Height}"); // For ITF-14, bar width affects bearer bar calculations using var itfBarcode = new Barcode(); itfBarcode.BarWidth = 3; itfBarcode.IncludeLabel = true; itfBarcode.Encode(Type.Itf14, "00012345678905"); ``` ### Response Not applicable (this is a code example for library configuration). ``` -------------------------------- ### Configure Barcode Labels with Custom Fonts and Text Source: https://context7.com/barnhill/barcodelib/llms.txt This snippet demonstrates how to enable and customize human-readable labels below barcodes. It covers setting custom fonts, using alternate text instead of raw data, and notes automatic formatting for UPC-A and EAN-13 types. ```csharp using BarcodeStandard; using SkiaSharp; using var barcode = new Barcode(); // Enable label display barcode.IncludeLabel = true; // Set custom label font barcode.LabelFont = new SKFont { Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold), Size = 24 }; // Use alternate label text instead of raw data barcode.AlternateLabel = "Product: ABC-123"; // Encode and generate barcode.Encode(Type.Code128, "ABC123", 350, 150); // For UPC-A and EAN-13, labels are automatically formatted // with proper spacing around guard bars using var upcBarcode = new Barcode(); upcBarcode.IncludeLabel = true; upcBarcode.Encode(Type.UpcA, "038000356216", 290, 120); ``` -------------------------------- ### Customize Barcode Colors and Alignment Source: https://context7.com/barnhill/barcodelib/llms.txt This code configures the visual appearance of barcodes by setting foreground and background colors, and controlling alignment within the generated image. It shows how to use predefined SKColors and custom RGB values, and demonstrates setting alignment to Center, Left, and Right. ```csharp using BarcodeStandard; using SkiaSharp; using var barcode = new Barcode(); // Set foreground (bars) color barcode.ForeColor = SKColors.DarkBlue; // Set background color barcode.BackColor = SKColors.White; // Alternative: use custom RGB colors barcode.ForeColor = new SKColor(0, 51, 102); // Dark blue barcode.BackColor = new SKColor(255, 255, 240); // Ivory // Set alignment within the image barcode.Alignment = AlignmentPositions.Center; // Default barcode.Alignment = AlignmentPositions.Left; barcode.Alignment = AlignmentPositions.Right; // Set dimensions barcode.Width = 400; barcode.Height = 150; // Generate the barcode var img = barcode.Encode(Type.Code128, "ALIGNED-LEFT"); ``` -------------------------------- ### Encode Method Source: https://context7.com/barnhill/barcodelib/llms.txt Encodes raw data into a barcode image with specified symbology, dimensions, and colors, returning an SKImage object. ```APIDOC ## Encode Method Encodes the raw data into a barcode image with specified symbology, dimensions, and colors. Returns an SKImage representing the barcode. ### Method Instance Method of Barcode class ### Endpoint N/A (Instance Method) ### Parameters - **type** (Type) - Required - The barcode symbology type. - **rawData** (string) - Optional - The data to encode. - **width** (int) - Optional - The desired width of the barcode image. - **height** (int) - Optional - The desired height of the barcode image. - **drawColor** (SKColor) - Optional - The foreground color of the barcode. - **backColor** (SKColor) - Optional - The background color of the barcode. ### Request Example ```csharp using BarcodeStandard; using SkiaSharp; using var barcode = new Barcode(); // Basic encode with type and data SKImage img1 = barcode.Encode(Type.Code128, "Hello World"); // Encode with dimensions SKImage img2 = barcode.Encode(Type.UpcA, "038000356216", 290, 120); // Encode with colors SKImage img3 = barcode.Encode(Type.Ean13, "5901234123457", SKColors.Black, SKColors.White); // Full encode with all parameters SKImage img4 = barcode.Encode( Type.Code39, "ABC123", SKColors.DarkBlue, SKColors.White, 400, 150 ); // Encode using pre-set properties barcode.RawData = "12345678"; barcode.EncodedType = Type.Code128; barcode.Width = 300; barcode.Height = 100; SKImage img5 = barcode.Encode(); ``` ### Response #### Success Response (SKImage) - **SKImage** - An SkiaSharp SKImage object representing the generated barcode. ``` -------------------------------- ### Barcode Generation API Source: https://github.com/barnhill/barcodelib/blob/master/README.md This section details how to use the Barcode class to generate barcode images. It covers constructors, properties, and encoding methods. ```APIDOC ## Barcode Generation ### Description This API allows developers to generate barcode images from a given string of data using various supported symbologies. ### Constructors - `Barcode()`: Initializes a new instance of the Barcode class with default settings. - `Barcode(string data)`: Initializes a new instance with the specified data to be encoded. - `Barcode(string data, Type symbology)`: Initializes a new instance with the specified data and barcode symbology. ### Properties - `IncludeLabel` (bool): Determines whether a label should be included with the barcode image. Defaults to `false`. ### Methods - `Encode(Type symbology, string data, SKColor foreground, SKColor background, int width, int height)`: Encodes the specified data into a barcode image of the given symbology, colors, and dimensions. ### Supported Symbologies - Code 128 - Code 93 - Code 39 (Extended / Full ASCII) - Code11 - EAN-8 - FIM (Facing Identification Mark) - UPC-A - UPC-E - Pharmacode - MSI - PostNet - Standard 2 of 5 - ISBN - Codabar - Interleaved 2 of 5 - ITF-14 - Telepen - UPC Supplemental 2 - JAN-13 - EAN-13 - UPC Supplemental 5 - IATA2of5 ### Request Example (C#) ```csharp var b = new Barcode(); b.IncludeLabel = true; var img = b.Encode(Type.UpcA, "038000356216", SKColors.Black, SKColors.White, 290, 120); ``` ### Response Example (Image Data) This method returns an image object (e.g., `SKBitmap` or similar) representing the generated barcode. The exact type depends on the underlying graphics library used (e.g., SkiaSharp). ### Error Handling - Ensure the data provided is valid for the selected symbology. - Invalid symbology types or data formats may result in encoding errors. ``` -------------------------------- ### Barcode Error Handling and Validation (C#) Source: https://context7.com/barnhill/barcodelib/llms.txt Details how to handle exceptions during barcode encoding, such as providing empty data or invalid data formats. It also demonstrates how to access the list of accumulated errors and how to disable specific validation rules like the EAN-13 country code check. ```csharp using BarcodeStandard; using System; using var barcode = new Barcode(); try { // Attempting to encode empty data throws exception barcode.Encode(Type.UpcA, ""); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); // Output: EENCODE-1: Input data not allowed to be blank. } try { // Invalid data length for UPC-A (requires 11-12 digits) barcode.Encode(Type.UpcA, "123"); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } try { // Non-numeric data for numeric-only symbology barcode.Encode(Type.Code128C, "ABC123"); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); // EC128-6: Only numeric values can be encoded with C128-C } // Access accumulated errors list var errors = barcode.Errors; foreach (var error in errors) { Console.WriteLine($"Barcode error: {error}"); } // Disable EAN-13 country code validation barcode.DisableEan13CountryException = true; barcode.Encode(Type.Ean13, "0000000000000", 300, 100); ``` -------------------------------- ### GenerateBarcode Method Source: https://context7.com/barnhill/barcodelib/llms.txt Generates the binary encoding string of a barcode without creating an image, useful for debugging or custom rendering. ```APIDOC ## GenerateBarcode Method Generates only the binary encoding string without creating an image. Useful for debugging or custom rendering. ### Method Instance Method of Barcode class ### Endpoint N/A (Instance Method) ### Parameters - **rawData** (string) - The data to encode. ### Request Example ```csharp using BarcodeStandard; using var barcode = new Barcode(); barcode.EncodedType = Type.Code128; // Generate binary encoding from data string binaryValue = barcode.GenerateBarcode("Hello123"); // Returns: "11010010000110100111001011110..." // Access the encoded value after encoding barcode.Encode(Type.UpcA, "038000356216"); Console.WriteLine(barcode.EncodedValue); // Output: binary string of 1s and 0s representing bars/spaces ``` ### Response #### Success Response (string) - **binaryValue** (string) - The binary string representation of the barcode. ``` -------------------------------- ### Control Barcode Bar Width and Aspect Ratio Source: https://context7.com/barnhill/barcodelib/llms.txt This snippet explains how to precisely control the width of individual bars and maintain the aspect ratio of the barcode for scanner compatibility. It shows setting a specific bar width in pixels and defining the aspect ratio to influence the barcode's height. It also notes the impact of bar width on bearer bar calculations for ITF-14 barcodes. ```csharp using BarcodeStandard; using SkiaSharp; using var barcode = new Barcode(); // Set specific bar width in pixels // Width will be calculated automatically based on encoded data length barcode.BarWidth = 2; // Set aspect ratio - Height = Width / AspectRatio // Useful for maintaining scanner-friendly proportions barcode.AspectRatio = 2.0; // Height will be half the width // Encode - dimensions calculated automatically barcode.Encode(Type.Code128, "AutoWidth123"); // Access calculated dimensions Console.WriteLine($"Width: {barcode.Width}, Height: {barcode.Height}"); // For ITF-14, bar width affects bearer bar calculations using var itfBarcode = new Barcode(); itfBarcode.BarWidth = 3; itfBarcode.IncludeLabel = true; itfBarcode.Encode(Type.Itf14, "00012345678905"); ``` -------------------------------- ### Code 128 with FNC1 Characters (C#) Source: https://context7.com/barnhill/barcodelib/llms.txt Illustrates the usage of special function characters (FNC1, FNC2, FNC3, FNC4) within the Code 128 symbology, particularly for GS1-128 barcodes. It shows how to access these characters and construct barcode data strings that include Application Identifiers and FNC1 codes for structured data encoding. ```csharp using BarcodeStandard; using BarcodeStandard.Symbologies; using System; using var barcode = new Barcode(); // FNC1 is commonly used for GS1-128 (formerly UCC/EAN-128) // Access FNC characters from Code128 class char fnc1 = Code128.FNC1; // Convert.ToChar(200) char fnc2 = Code128.FNC2; // Convert.ToChar(201) char fnc3 = Code128.FNC3; // Convert.ToChar(202) char fnc4 = Code128.FNC4; // Convert.ToChar(203) // Create GS1-128 barcode with Application Identifiers // AI (01) = GTIN, AI (10) = Batch Number string gs1Data = $"{fnc1}0109501234567890{fnc1}10BATCH123"; barcode.Encode(Type.Code128, gs1Data, 500, 100); // For Code 128-C with FNC1 (numeric pairs with function codes) string numericWithFnc = $"{fnc1}01234567890123"; barcode.Encode(Type.Code128C, numericWithFnc, 400, 100); ``` -------------------------------- ### SaveImage Method Source: https://context7.com/barnhill/barcodelib/llms.txt Saves the generated barcode image to a file or stream in JPG, PNG, or WebP format, or retrieves image data as a byte array. ```APIDOC ## SaveImage Method Saves the generated barcode image to a file or stream in JPG, PNG, or WebP format. ### Method Instance Method of Barcode class ### Endpoint N/A (Instance Method) ### Parameters - **filePathOrStream** (string or Stream) - The path to the file or a Stream object to save the image to. - **saveTypes** (SaveTypes) - The desired image format (Jpg, Png, Webp). ### Request Example ```csharp using BarcodeStandard; using SkiaSharp; using System.IO; using var barcode = new Barcode(); barcode.IncludeLabel = true; barcode.Encode(Type.UpcA, "038000356216", 300, 120); // Save to file with specific format barcode.SaveImage("barcode.jpg", SaveTypes.Jpg); barcode.SaveImage("barcode.png", SaveTypes.Png); barcode.SaveImage("barcode.webp", SaveTypes.Webp); // Save to memory stream using var memoryStream = new MemoryStream(); barcode.SaveImage(memoryStream, SaveTypes.Png); byte[] imageBytes = memoryStream.ToArray(); // Get image data as byte array directly byte[] jpgData = barcode.GetImageData(SaveTypes.Jpg); byte[] pngData = barcode.GetImageData(SaveTypes.Png); ``` ### Response N/A (Saves image to specified location or returns byte array. ``` -------------------------------- ### Code 128 with FNC1 Characters Source: https://context7.com/barnhill/barcodelib/llms.txt This section explains how to use special function characters (FNC1, FNC2, FNC3, FNC4) within Code 128 barcodes, particularly for GS1-128 compliance. ```APIDOC ## Code 128 with FNC1 Characters Use special function characters for GS1-128 and other applications requiring FNC codes. ### Method Implicit (Code Execution) ### Endpoint N/A ### Parameters N/A ### Request Example ```csharp using BarcodeStandard; using BarcodeStandard.Symbologies; using System; using var barcode = new Barcode(); // FNC1 is commonly used for GS1-128 (formerly UCC/EAN-128) // Access FNC characters from Code128 class char fnc1 = Code128.FNC1; // Convert.ToChar(200) char fnc2 = Code128.FNC2; // Convert.ToChar(201) char fnc3 = Code128.FNC3; // Convert.ToChar(202) char fnc4 = Code128.FNC4; // Convert.ToChar(203) // Create GS1-128 barcode with Application Identifiers // AI (01) = GTIN, AI (10) = Batch Number string gs1Data = $"{fnc1}0109501234567890{fnc1}10BATCH123"; barcode.Encode(Type.Code128, gs1Data, 500, 100); // For Code 128-C with FNC1 (numeric pairs with function codes) string numericWithFnc = $"{fnc1}01234567890123"; barcode.Encode(Type.Code128C, numericWithFnc, 400, 100); ``` ### Response N/A (Barcode Generation) #### Success Response - **Encoded Barcode**: The generated barcode image based on the provided data and FNC characters. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.