C# Generate Barcodes with UTF-8 Encoding

In the previous post, you have seen how to generate and read different types of barcodes using C#. However, in certain cases, you have to deal with non-English characters. For example, when you work with Arabic, Latin, Greek, or similar languages. In such cases, you may need to encode the characters into Unicode standards i.e. UTF-8. In accordance with that, this article covers how to generate and read barcodes using UTF-8 encoding in C#.

C# API to Generate Barcodes using UTF-8 Encoding

Aspose.BarCode for .NET is a powerful C# API for barcode generation and recognition. Using the API, you can work with a wide range of barcode symbologies. In addition, the API supports generating barcodes using UTF-8 encoding. You can either download the API or install it using NuGet.

PM> Install-Package Aspose.BarCode

Generate Barcode using UTF-8 Encoding in C#

The following are the steps to generate barcode using UTF-8 encoding.

The following code sample shows how to generate a barcode using UTF-8 encoding in C#.

// Create a barcode generator
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.Pdf417);
// Set barcode text
generator.CodeText = "منحة";
// Set resolution
generator.Parameters.Resolution = 400;
// Set encoding type
generator.Parameters.Barcode.Pdf417.CodeTextEncoding = Encoding.UTF8;
// Generate barcode
Bitmap imgBarcode = generator.GenerateBarCodeImage();
// Save barcode image
imgBarcode.Save("generate-barcode.png");

Read UTF-8 Encoded Barcode using C#

The following are the steps to recognize UTF-8 encoded barcode using C#.

The following code sample shows how to recognize barcodes using UTF-8 encoding in C#.

// Recognize the above barcode
using (BarCodeReader reader = new BarCodeReader("generate-barcode.png"))
{
// Read barcodes
foreach (BarCodeResult result in reader.ReadBarCodes())
{
// Set encoding
Encoding unicode = Encoding.UTF8;
// Get the characters array from the bytes
char[] unicodeChars = new char[unicode.GetCharCount(result.CodeBytes, 0, result.CodeBytes.Length)];
unicode.GetChars(result.CodeBytes, 0, result.CodeBytes.Length, unicodeChars, 0);
// Build unicode string
string strCodeText = new string(unicodeChars);
Console.WriteLine(strCodeText);
}
}

Get a Free API License

In case you are interested in trying and using the API, you can get a free temporary license.

Conclusion

In this article, you have learned how to generate barcodes using UTF-8 encoding in C#. Furthermore, you have seen how to recognize and read a UTF-8 encoded barcode. You can explore more about the C# barcode API using the documentation.