Skip to footer content
USING IRONBARCODE

Xamarin Barcode Generator: Generate Barcodes in C# with IronBarcode

Creating barcodes in Xamarin applications doesn't have to be complex or time-consuming. With IronBarcode, developers can generate professional-quality barcodes and QR codes across Android and iOS platforms using just a few lines of C# code. This .NET barcode library eliminates the need for platform-specific implementations while providing enterprise-grade features that work consistently across all mobile devices in your Xamarin Forms or native Xamarin projects.

This article demonstrates how to implement a barcode generation system in your cross-platform mobile application using IronBarcode. You'll learn how to install the library via NuGet, generate various barcode formats including QR codes, customize appearance with colors and annotations, and export results in multiple file formats -- all while maintaining clean, maintainable C# code that works across Android and iOS.

Why Do You Need a Professional Barcode Library for Xamarin Forms?

Implementing barcode generation from scratch in Xamarin.Forms requires handling complex encoding algorithms, managing platform-specific rendering differences, and ensuring accurate output across various barcode symbologies. A professional .NET barcode library, such as IronBarcode, eliminates these challenges by providing a unified API that handles all technical complexities internally through simple C# methods.

IronBarcode supports over 30 barcode formats, including QR codes, Code 128, Code 39, EAN-13, UPC-A, Data Matrix, PDF 417, and Aztec codes. Each format is optimized for specific use cases. For example, QR codes excel at storing URLs and large text data, EAN-13 serves retail products, while Code 128 provides high-density alphanumeric encoding. The library automatically calculates checksums, applies appropriate error correction levels, and ensures compliance with GS1 standards for commercial applications.

The cross-platform nature of IronBarcode ensures that your barcode generation code remains identical regardless of whether you are targeting Android or iOS. This consistency extends beyond Xamarin applications -- the same code works in ASP.NET web applications, desktop software, and Docker containers, making it ideal for enterprises with diverse deployment requirements. Learn more about IronBarcode's cross-platform capabilities.

Xamarin vs. .NET MAUI: Understanding the Context

Xamarin has been Microsoft's primary cross-platform mobile development framework for C# developers for many years. While Microsoft has now transitioned to .NET MAUI as the modern successor, many production applications still run on Xamarin.Forms and Xamarin.Android/iOS. IronBarcode works with both frameworks, so the patterns shown in this article apply equally whether you are maintaining a Xamarin project or migrating to .NET MAUI.

If you are starting a brand new mobile project today, consider using .NET MAUI with IronBarcode. The IronBarcode .NET MAUI documentation covers the same features shown here with .NET MAUI-specific project setup steps.

Why Not Build Barcode Logic from Scratch?

Writing custom barcode encoding logic introduces several maintenance risks. You must handle format-specific checksum algorithms, bar width calculations, quiet zone requirements, and scanner compatibility across different hardware vendors. IronBarcode abstracts all of this behind a clean API. The IronBarcode API reference documents every method and parameter so your team can quickly find the right tool for each scenario.

How Do You Install IronBarcode in a Xamarin Project?

Installing IronBarcode in your Xamarin.Forms project takes just minutes through NuGet Package Manager. Open your Visual Studio solution containing your cross-platform mobile projects and follow the steps below.

Right-click on your solution in Solution Explorer and select "Manage NuGet Packages for Solution". In the Browse tab, search for "IronBarCode". Select the official IronBarcode package by Iron Software and install it across all projects in your solution, including the shared project and both platform-specific projects. You can explore the complete NuGet package details for version history and dependencies.

Alternatively, use the Package Manager Console with this command:

Install-Package BarCode
Install-Package BarCode
SHELL

For Android projects, no additional permissions are required for basic barcode creation. However, if you plan to save generated barcodes to external storage, add the WRITE_EXTERNAL_STORAGE permission to your AndroidManifest.xml. Review the Android-specific setup guide for detailed platform considerations.

For iOS projects, if you're saving QR codes or barcodes to the photo library, add the NSPhotoLibraryAddUsageDescription key to your Info.plist with an appropriate user-facing description. The iOS barcode setup documentation provides complete instructions.

Setting Up Your License Key

To use IronBarcode's full functionality without watermarks, you'll need a license key. You can obtain a free trial license that provides full access to all barcode generation features for evaluation. Set your license key once in your application startup code:

IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY-HERE";
IronBarCode.License.LicenseKey = "YOUR-LICENSE-KEY-HERE";
$vbLabelText   $csharpLabel

Without a license key, IronBarcode operates in evaluation mode with watermarks on generated barcodes. For production applications, Iron Software offers various licensing tiers, including single-project, organization-wide, and SaaS distribution licenses designed for mobile app deployment.

How Do You Generate Your First Barcode with IronBarcode?

Creating your first barcode with IronBarcode in a Xamarin.Forms application requires minimal C# code. The example below generates a Code 128 barcode, saves it as a PNG, and adds a human-readable text label:

using IronBarCode;
using System.IO;

string filePath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.Personal),
    "barcode.png");

var myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com", BarcodeEncoding.Code128);
myBarcode.ResizeTo(500, 200);
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SaveAsPng(filePath);
using IronBarCode;
using System.IO;

string filePath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.Personal),
    "barcode.png");

var myBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com", BarcodeEncoding.Code128);
myBarcode.ResizeTo(500, 200);
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SaveAsPng(filePath);
$vbLabelText   $csharpLabel

The BarcodeWriter.CreateBarcode() method is the primary entry point for barcode generation. It accepts two arguments: the data to encode and the barcode format from the BarcodeEncoding enum. The method returns a GeneratedBarcode object that provides extensive customization through a fluent API.

The ResizeTo() method adjusts barcode dimensions while maintaining proper aspect ratios and bar width requirements for the selected symbology. AddBarcodeValueTextBelowBarcode() adds human-readable text below the bars, which is required for many retail and inventory management scenarios. Explore the full BarcodeWriter API reference for advanced features.

Output

How to Generate Barcodes in Xamarin Using IronBarcode: Figure 10 - PDF417 Output

Generating QR Codes for Mobile Apps

For QR code generation in your Xamarin barcode scanner app, use the specialized QRCodeWriter class. QR codes are ideal for mobile applications because device cameras can scan them quickly without specialized hardware:

using IronBarCode;

var qrCode = QRCodeWriter.CreateQrCode("SKU-12345|Batch-789|Exp-2025", 500);
qrCode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkBlue);

byte[] barcodeBytes = qrCode.ToPngBinaryData();
using IronBarCode;

var qrCode = QRCodeWriter.CreateQrCode("SKU-12345|Batch-789|Exp-2025", 500);
qrCode.ChangeBarCodeColor(IronSoftware.Drawing.Color.DarkBlue);

byte[] barcodeBytes = qrCode.ToPngBinaryData();
$vbLabelText   $csharpLabel

The QRCodeWriter.CreateQrCode() method specializes in QR code creation with support for logo embedding and color customization. The second parameter specifies the QR code size in pixels. The ToPngBinaryData() method converts the generated QR code to a byte array, useful for storing in a database or transmitting over a network. Review additional QR code generation examples for advanced configurations including embedded logos.

How Do You Work with Different Barcode Formats?

IronBarcode's broad format support allows you to generate exactly the barcode type your Xamarin.Forms application requires. The example below demonstrates how to create EAN-13, UPC-A, DataMatrix, and PDF417 barcodes from a single product code string:

using IronBarCode;

string productCode = "012345678905";

// EAN-13 for European retail
var ean13 = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.EAN13);
ean13.SaveAsPng("product_ean13.png");

// UPC-A for North American retail
var upcA = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.UPCA);
upcA.SaveAsJpeg("product_upca.jpg");

// DataMatrix for small items with high data density
var dataMatrix = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.DataMatrix);
dataMatrix.SaveAsPdf("product_datamatrix.pdf");

// PDF417 for driver licenses and ID cards
var pdf417 = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.PDF417);
pdf417.SaveAsTiff("product_pdf417.tiff");
using IronBarCode;

string productCode = "012345678905";

// EAN-13 for European retail
var ean13 = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.EAN13);
ean13.SaveAsPng("product_ean13.png");

// UPC-A for North American retail
var upcA = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.UPCA);
upcA.SaveAsJpeg("product_upca.jpg");

// DataMatrix for small items with high data density
var dataMatrix = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.DataMatrix);
dataMatrix.SaveAsPdf("product_datamatrix.pdf");

// PDF417 for driver licenses and ID cards
var pdf417 = BarcodeWriter.CreateBarcode(productCode, BarcodeEncoding.PDF417);
pdf417.SaveAsTiff("product_pdf417.tiff");
$vbLabelText   $csharpLabel

Each barcode symbology has specific characteristics and use cases. EAN-13 and UPC-A require numeric-only input and automatically calculate check digits for retail scanning accuracy. DataMatrix excels at encoding large amounts of data in minimal space, making it ideal for tracking electronic components and pharmaceuticals. PDF417 supports extensive data storage with built-in error correction, commonly used in transportation and identification documents.

The library automatically validates input data against format requirements and throws descriptive exceptions for invalid inputs. This validation ensures generated barcodes comply with international ISO standards and scan reliably with commercial barcode readers used in retail and logistics. Learn more about choosing the right barcode format for your specific use case.

Output

How to Generate Barcodes in Xamarin Using IronBarcode: Figure 9 - PDF417 Output

Barcode Format Comparison

The following table summarizes the most commonly used barcode formats and their typical applications in mobile projects:

Barcode Format Comparison for Mobile Applications
Format Data Type Typical Use Case BarcodeEncoding Value
QR Code Alphanumeric, binary, URLs Product links, contact cards, payment flows QRCode
Code 128 Alphanumeric Shipping labels, inventory Code128
EAN-13 Numeric (13 digits) European retail products EAN13
UPC-A Numeric (12 digits) North American retail products UPCA
DataMatrix Alphanumeric, binary Electronics, pharmaceuticals, small labels DataMatrix
PDF417 Alphanumeric, binary ID documents, boarding passes, transport PDF417

How Do You Customize and Export Barcodes for Mobile UIs?

IronBarcode provides extensive customization options to match your Xamarin application's visual requirements. The example below shows how to apply colors, add text annotations, and save to platform-specific file locations:

using IronBarCode;
using System.IO;
using Xamarin.Forms;

var barcode = BarcodeWriter.CreateBarcode("CUSTOM-2024", BarcodeEncoding.Code128);

// Apply styling to match your mobile UI
barcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Navy);
barcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.LightGray);
barcode.SetMargins(10);

// Add human-readable annotations
barcode.AddAnnotationTextAboveBarcode("Product ID");
barcode.AddBarcodeValueTextBelowBarcode(new IronSoftware.Drawing.Font("Arial", 12));

// Determine platform-specific save path
string documentsPath;
if (Device.RuntimePlatform == Device.iOS)
{
    documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
else // Android
{
    documentsPath = Android.OS.Environment
        .GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)
        .AbsolutePath;
}

// Export in multiple formats
barcode.SaveAsPng(Path.Combine(documentsPath, "barcode.png"));
barcode.SaveAsPdf(Path.Combine(documentsPath, "barcode.pdf"));
using IronBarCode;
using System.IO;
using Xamarin.Forms;

var barcode = BarcodeWriter.CreateBarcode("CUSTOM-2024", BarcodeEncoding.Code128);

// Apply styling to match your mobile UI
barcode.ChangeBarCodeColor(IronSoftware.Drawing.Color.Navy);
barcode.ChangeBackgroundColor(IronSoftware.Drawing.Color.LightGray);
barcode.SetMargins(10);

// Add human-readable annotations
barcode.AddAnnotationTextAboveBarcode("Product ID");
barcode.AddBarcodeValueTextBelowBarcode(new IronSoftware.Drawing.Font("Arial", 12));

// Determine platform-specific save path
string documentsPath;
if (Device.RuntimePlatform == Device.iOS)
{
    documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
else // Android
{
    documentsPath = Android.OS.Environment
        .GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)
        .AbsolutePath;
}

// Export in multiple formats
barcode.SaveAsPng(Path.Combine(documentsPath, "barcode.png"));
barcode.SaveAsPdf(Path.Combine(documentsPath, "barcode.pdf"));
$vbLabelText   $csharpLabel

The styling methods give you full control over barcode appearance in your mobile interface. Colors can be specified using RGB values or predefined color constants to match your app's theme. The SetMargins() method adds padding around the barcode, preventing scanning issues that arise when barcodes are placed near image edges.

File handling in cross-platform Xamarin development requires platform awareness. iOS apps should save to the documents directory or photo library. Android apps typically use external storage directories or the application's internal data directory. The library supports all major image formats -- PNG, JPEG, GIF, TIFF, BMP -- plus PDF for document integration. For more complex scenarios, review the barcode export documentation for additional output options.

Displaying Barcodes Directly in Xamarin Views

Beyond saving to files, you can display barcodes directly in a Xamarin page by converting the generated barcode to a StreamImageSource. This avoids unnecessary disk reads and writes for display-only scenarios:

using IronBarCode;
using System.IO;
using Xamarin.Forms;

var barcode = BarcodeWriter.CreateBarcode("ORDER-99812", BarcodeEncoding.QRCode, 300);
byte[] pngBytes = barcode.ToPngBinaryData();

barcodeImage.Source = ImageSource.FromStream(() => new MemoryStream(pngBytes));
using IronBarCode;
using System.IO;
using Xamarin.Forms;

var barcode = BarcodeWriter.CreateBarcode("ORDER-99812", BarcodeEncoding.QRCode, 300);
byte[] pngBytes = barcode.ToPngBinaryData();

barcodeImage.Source = ImageSource.FromStream(() => new MemoryStream(pngBytes));
$vbLabelText   $csharpLabel

Assign the result to any Xamarin Image control by setting its Source property. This pattern works without file system permissions and is the recommended approach for transient display use cases such as order confirmation screens or ticketing flows. See the IronBarcode image rendering guide for further details.

How Do You Handle Barcode Scanning in Xamarin?

While this article focuses on barcode generation, many Xamarin applications also need to read barcodes from device cameras. IronBarcode includes a barcode reading API that mirrors the generation API in simplicity. The IronBarcode barcode reader documentation covers the full reading workflow, including multi-barcode detection and format filtering.

For a Xamarin application that both generates and scans barcodes -- for example, a warehouse receiving application -- you can combine the BarcodeWriter and BarcodeReader classes in the same project. Both APIs work from the same NuGet package with no additional dependencies. The complete IronBarcode examples gallery includes a scan-and-generate workflow that demonstrates this combined approach.

Error Handling and Validation

Production barcode generation should always include error handling, particularly when encoding data from user input or external systems. IronBarcode throws typed exceptions for format violations -- for example, passing a non-numeric string to an EAN-13 encoder. Wrapping generation calls in try-catch blocks and presenting clear error messages to users improves app stability:

using IronBarCode;

try
{
    var barcode = BarcodeWriter.CreateBarcode(userInputValue, BarcodeEncoding.EAN13);
    barcode.SaveAsPng("output.png");
}
catch (BarcodeException ex)
{
    Console.WriteLine($"Barcode generation failed: {ex.Message}");
}
using IronBarCode;

try
{
    var barcode = BarcodeWriter.CreateBarcode(userInputValue, BarcodeEncoding.EAN13);
    barcode.SaveAsPng("output.png");
}
catch (BarcodeException ex)
{
    Console.WriteLine($"Barcode generation failed: {ex.Message}");
}
$vbLabelText   $csharpLabel

Always validate user-supplied values before passing them to the barcode encoder. For numeric formats like EAN-13, strip whitespace and non-digit characters before encoding. Review the input validation guidance in the IronBarcode troubleshooting section for common data quality issues.

What Are Your Next Steps?

IronBarcode turns barcode generation in Xamarin applications from a complex, platform-specific challenge into a few lines of C# code. Whether you need QR codes for mobile order flows, Code 128 for warehouse labels, or EAN-13 for retail products, the library provides a single API that works identically on Android and iOS -- and equally well in ASP.NET, desktop, and server environments.

To continue building on what you've learned here, explore these resources:

Start with the NuGet package installation and run the first example above to confirm your environment is set up correctly. From there, the format comparison table and customization examples in this article give you everything you need to tailor the barcode output to your application's exact requirements.

Frequently Asked Questions

What is a Xamarin Barcode Generator?

A Xamarin Barcode Generator is a tool or library that enables C# developers to create barcodes and QR codes within Xamarin Android and iOS applications. IronBarcode provides this capability through a unified .NET API.

How does IronBarcode simplify barcode generation in Xamarin?

IronBarcode provides a single .NET barcode API that works identically on Android and iOS, eliminating the need for platform-specific implementations and manually handling encoding algorithms or format validation.

Can I generate barcodes for both Android and iOS using IronBarcode?

Yes. IronBarcode supports barcode generation for both Android and iOS platforms in Xamarin applications, producing consistent results across devices with no platform-specific code changes.

What barcode formats does IronBarcode support in Xamarin?

IronBarcode supports over 30 formats including QR Code, Code 128, Code 39, EAN-13, UPC-A, DataMatrix, PDF417, and Aztec codes. All formats are available through the same BarcodeEncoding enum.

Is IronBarcode suitable for production mobile applications?

Yes. IronBarcode includes input validation, ISO-compliant encoding, GS1 standard support, and license-key activation for watermark-free output in production builds.

Does IronBarcode work with .NET MAUI as well as Xamarin?

Yes. IronBarcode works with both Xamarin and .NET MAUI. The same NuGet package and API are used in both frameworks, making migration straightforward.

How do I display a generated barcode in a Xamarin view?

Call ToPngBinaryData() on the generated barcode to get a byte array, then assign ImageSource.FromStream(() => new MemoryStream(bytes)) to your Xamarin Image control's Source property.

Can I use IronBarcode in native Xamarin projects as well as Xamarin.Forms?

Yes. IronBarcode works in both Xamarin.Forms shared projects and platform-specific native Xamarin.Android and Xamarin.iOS projects.

Does IronBarcode support custom barcode styling?

Yes. You can change bar and background colors, set margins, add annotation text above or below the barcode, and adjust font properties using the fluent GeneratedBarcode API.

How do I get started with a free trial of IronBarcode?

Visit https://ironsoftware.com/csharp/barcode/ to download a free 30-day trial license key. Install the NuGet package, set the license key in your startup code, and begin generating barcodes immediately.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...
Read More