Class BarcodeGenerator

Class BarcodeGenerator

Namespace: Aspose.BarCode.Generation
Assembly: Aspose.BarCode.dll (25.7.0)

BarcodeGenerator for backend barcode images generation.

supported symbologies: 1D: Codabar, Code11, Code128, Code39, Code39FullASCII Code93, EAN13, EAN8, Interleaved2of5, MSI, Standard2of5, UPCA, UPCE, ISBN, GS1Code128, Postnet, Planet EAN14, SCC14, SSCC18, ITF14, SingaporePost ... 2D: Aztec, DataMatrix, PDf417, QR code ...

[XmlSerialization(Name = "Aspose.BarCode.Generator")]
public sealed class BarcodeGenerator : IDisposable

Inheritance

object BarcodeGenerator

Implements

IDisposable

Inherited Members

object.GetType() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()

Examples

This sample shows how to create and save a barcode image.

using(var generator = new BarcodeGenerator(EncodeTypes.Code128))
  {
      generator.CodeText = "123ABC";
      generator.Save("code128.png");
  }

Constructors

BarcodeGenerator(BaseEncodeType)

Creates an instance of BarcodeGenerator.

public BarcodeGenerator(BaseEncodeType type)

Parameters

type BaseEncodeType

Barcode symbology type. Use Aspose.BarCode.Generation.EncodeTypes class to setup a symbology.

BarcodeGenerator(BaseEncodeType, string)

Creates an instance of BarcodeGenerator.

public BarcodeGenerator(BaseEncodeType type, string codeText)

Parameters

type BaseEncodeType

Barcode symbology type. Use Aspose.BarCode.Generation.EncodeTypes class to setup a symbology.

codeText string

Text to be encoded.

Properties

BarcodeType

Barcode symbology type.

[XmlSerialization(Type = XmlSerializationType.Element)]
public BaseEncodeType BarcodeType { get; set; }

Property Value

BaseEncodeType

CodeText

Text to be encoded.

[XmlSerialization(Type = XmlSerializationType.Element)]
public string CodeText { get; set; }

Property Value

string

Parameters

Generation parameters.

[XmlSerialization(Type = XmlSerializationType.Element)]
public BaseGenerationParameters Parameters { get; }

Property Value

BaseGenerationParameters

Methods

Dispose()

public void Dispose()

ExportToXml(string)

Exports BarCode properties to the xml-file specified

public bool ExportToXml(string xmlFile)

Parameters

xmlFile string

The name for the file

Returns

bool

Whether or not export completed successfully.

Returns True in case of success; False Otherwise

ExportToXml(Stream)

Exports BarCode properties to the xml-stream specified

public bool ExportToXml(Stream xml)

Parameters

xml Stream

The xml-stream

Returns

bool

Whether or not export completed successfully.

Returns True in case of success; False Otherwise

GenerateBarCodeImage()

Generate the barcode image under current settings.

public Bitmap GenerateBarCodeImage()

Returns

Bitmap

Barcode image. See System.Drawing.Bitmap.

Examples

This sample shows how to create and save a barcode image.

using(var generator = new BarcodeGenerator(EncodeTypes.Code128))
  {
      Bitmap barcode = generator.GenerateBarCodeImage();
      barcode.Save("test.png");
  }

ImportFromXml(string)

Imports BarCode properties from the xml-file specified and creates BarcodeGenerator instance.

public static BarcodeGenerator ImportFromXml(string xmlFile)

Parameters

xmlFile string

The name for the file

Returns

BarcodeGenerator

BarcodeGenerator instance

ImportFromXml(Stream)

Imports BarCode properties from the xml-stream specified and creates BarcodeGenerator instance.

public static BarcodeGenerator ImportFromXml(Stream xml)

Parameters

xml Stream

The xml-stream

Returns

BarcodeGenerator

BarcodeGenerator instance

Save(Stream, BarCodeImageFormat)

Save barcode image to stream in specific format.

public void Save(Stream stream, BarCodeImageFormat format)

Parameters

stream Stream

Output System.IO.Stream.

format BarCodeImageFormat

Specifies the file format of the output image.

Save(string, BarCodeImageFormat)

Save barcode image to specific file in specific format.

public void Save(string filename, BarCodeImageFormat format)

Parameters

filename string

Path to save to.

format BarCodeImageFormat

Specifies the file format of the output image.

Save(string)

Save barcode image to specific file.

public void Save(string filename)

Parameters

filename string

Path to save to.

SetCodeText(byte[])

Set codetext as sequence of bytes.

public void SetCodeText(byte[] codeBytes)

Parameters

codeBytes byte []

Bytes of codetext

SetCodeText(string, Encoding)

Encodes the Unicode codeText into a byte sequence using the specified encoding. UTF-8 is the most commonly used encoding. If the encoding supports it, the function automatically inserts a byte order mark (BOM).

This function is intended for use with 2D barcodes only (e.g., Aztec, QR, DataMatrix, PDF417, MaxiCode, DotCode, HanXin, RectMicroQR, etc.). It enables manual encoding of Unicode text using national or special encodings; however, this method is considered obsolete in modern applications. For modern use cases, ECI encoding is recommended for Unicode data.

Using this function with 1D barcodes, GS1-compliant barcodes (including 2D), or HIBC barcodes (including 2D) is not supported by the corresponding barcode standards and may lead to unpredictable results.

public void SetCodeText(string codeText, Encoding encoding)

Parameters

codeText string

CodeText string

encoding Encoding

Applied encoding

Examples

This example shows how to use SetCodeText setting Unicode-encoded text for 2D barcodes using different encodings:

//Encode QR Code text using UTF-8 with BOM
using (BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR))
{
    gen.SetCodeText("車種名", Encoding.UTF8);
    gen.Save("barcode.png", BarCodeImageFormat.Png);
}
using (BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.QR))
    foreach (BarCodeResult result in reader.ReadBarCodes())
        Console.WriteLine("BarCode CodeText: " + result.CodeText);

//Encode DataMatrix text using Shift-JIS (Japanese encoding)
using (BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.DataMatrix))
{
    gen.SetCodeText("車種名", Encoding.GetEncoding(932));
    gen.Save("barcode.png", BarCodeImageFormat.Png);
}
using (BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.DataMatrix))
    foreach (BarCodeResult result in reader.ReadBarCodes())
        Console.WriteLine("BarCode CodeText: " + result.GetCodeText(Encoding.GetEncoding(932)));

SetCodeText(string, Encoding, bool)

Encodes the Unicode codeText into a byte sequence using the specified encoding. UTF-8 is the most commonly used encoding. If the encoding supports it and insertBOM is set to true, the function includes a byte order mark (BOM).

This function is intended for use with 2D barcodes only (e.g., Aztec, QR, DataMatrix, PDF417, MaxiCode, DotCode, HanXin, RectMicroQR, etc.). It enables manual encoding of Unicode text using national or special encodings; however, this method is considered obsolete in modern applications. For modern use cases, ECI encoding is recommended for Unicode data.

Using this function with 1D barcodes, GS1-compliant barcodes (including 2D), or HIBC barcodes (including 2D) is not supported by the corresponding barcode standards and may lead to unpredictable results.

public void SetCodeText(string codeText, Encoding encoding, bool insertBOM)

Parameters

codeText string

CodeText string

encoding Encoding

Applied encoding

insertBOM bool

Indicates whether to insert a byte order mark (BOM) when the specified encoding supports it (e.g., UTF-8, UTF-16, UTF-32). If set to true, the BOM is added; if false, the BOM is omitted even if the encoding normally uses one.

Examples

This example shows how to use SetCodeText with or without a BOM for 2D barcodes.

//Encode codetext using UTF-8 with BOM
using (BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR))
{
    gen.SetCodeText("車種名", Encoding.UTF8, true);
    gen.Save("barcode.png", BarCodeImageFormat.Png);
}
using (BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.QR))
    foreach (BarCodeResult result in reader.ReadBarCodes())
        Console.WriteLine("BarCode CodeText: " + result.CodeText);

//Encode codetext using UTF-8 without BOM
using (BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR))
{
    gen.SetCodeText("車種名", Encoding.UTF8, false);
    gen.Save("barcode.png", BarCodeImageFormat.Png);
}
using (BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.QR))
    foreach (BarCodeResult result in reader.ReadBarCodes())
        Console.WriteLine("BarCode CodeText: " + result.CodeText);
 English