How to Create Barcode Images in C# with IronBarcode
IronBarcode enables .NET developers to generate barcode and QR code images in C# with a single line of code, supporting multiple formats and image types. Build inventory management systems, retail applications, or document processing solutions with a robust API that creates professional-quality barcodes for reliable scanning across all major readers.
Quickstart: Create and Save a Code128 Barcode in One LineUse IronBarcode's one-line API to create a Code128 barcode from a string and save it as a PNG image. Get started immediately - supply the data, choose the encoding and size, and write your image file.
-
1Install IronBarcode with NuGet Package Manager
-
2Copy and run this code snippet.
IronBarCode.BarcodeWriter.CreateBarcode("Sample123", BarcodeEncoding.Code128, 250, 100).SaveAsPng("Barcode.png");C# -
3Deploy to test on your live environment
Start using IronBarcode in your project today with a free trial
Minimal Workflow (5 steps)
- Download the C# IronBarcode Library
- Generate barcodes and save as image files
- Generate QR codes and save as image files
How Do I Generate Barcodes and Save as Image Files?
Besides reading barcodes, IronBarcode allows users to write barcodes with minimal coding. Call the CreateBarcode() method from the BarcodeWriter class, specifying the barcode value, type, width, and height in the method parameters. This outputs a GeneratedBarcode object, which can be saved as an image file using the SaveAs() method.
What Types of Data Can I Use for Barcode Values?
The BarcodeWriter.CreateBarcode() method accepts multiple data types for barcode values: byte[] array, MemoryStream, and string. String lengths and accepted characters vary by barcode type - see our API documentation for details. Code128 supports the full ASCII character set, while UPC-A requires exactly 12 numeric digits. For binary data, use byte[] array to encode complex data structures.
Which Barcode Encoding Types Are Supported?
IronBarcode supports numerous barcode formats - see our Supported Barcode Formats article for the complete list. Popular formats include:
- Linear/1D Barcodes:
Code128,Code39,UPC-A,UPC-E,EAN-8,EAN-13 - 2D Barcodes:
QR Code,Data Matrix,PDF417,Aztec - Specialized Formats:
MaxiCodefor shipping,Databarfor retail
Each format serves specific uses. Code128 handles general alphanumeric data well, while QR codes offer superior error correction for mobile scanning.
How Do Width and Height Settings Affect My Barcode?
Set the output barcode image dimensions in pixels. Default measurements are 250 px for both. Some barcode types (QR, PDF417) require specific dimensions for compliance. When input dimensions don't match requirements, the barcode generates at compliant dimensions with whitespace filling remaining space. Too-small dimensions throw an exception.
For optimal scanning:
- Linear barcodes: Maintain width significantly greater than height
- 2D barcodes: Use square dimensions for QR codes (e.g., 250x250)
- Print quality: Higher pixel dimensions improve print quality at larger physical sizes
What Image Formats Can I Export Barcodes To?
The GeneratedBarcode object from BarcodeWriter.CreateBarcode() saves to various image formats using specific SaveAs() methods:
SaveAsGif(): Saves as GIF image fileSaveAsJpeg(): Saves as JPEG image fileSaveAsPng(): Saves as PNG image file (recommended for web)SaveAsTiff(): Saves as TIFF image file (ideal for archival/printing)SaveAsWindowsBitmap(): Saves as BMP image fileSaveAsImage(): General method - specify format extension in file path
Additionally, export barcodes as streams for in-memory processing or create barcodes as PDF documents for document integration.
How Do I Generate a Barcode and Save It As an Image File?
Use BarcodeWriter.CreateBarcode() to create a Code128 barcode and save it as a JPEG image:
using IronBarCode;
// Create a basic barcode
BarcodeWriter.CreateBarcode("IronBarcode123", BarcodeEncoding.Code128, 200, 100).SaveAsJpeg("OneDBarcode.jpeg");
// Create a barcode with custom options
var myBarcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128, 300, 150);
// Apply styling
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.SetMargins(10);
// Save in multiple formats
myBarcode.SaveAsJpeg("product-barcode.jpg");
myBarcode.SaveAsPng("product-barcode.png");Imports IronBarCode
' Create a basic barcode
BarcodeWriter.CreateBarcode("IronBarcode123", BarcodeEncoding.Code128, 200, 100).SaveAsJpeg("OneDBarcode.jpeg")
' Create a barcode with custom options
Dim myBarcode = BarcodeWriter.CreateBarcode("PRODUCT-12345", BarcodeEncoding.Code128, 300, 150)
' Apply styling
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.SetMargins(10)
' Save in multiple formats
myBarcode.SaveAsJpeg("product-barcode.jpg")
myBarcode.SaveAsPng("product-barcode.png")
For advanced styling options, see our guide on customizing barcode styles.
How Do I Generate QR Codes and Save as Image Files?
QR codes, classified as two-dimensional barcodes, are fully supported by IronBarcode. Their versatility, cosmetic appeal, and customizable features have driven widespread adoption from restaurant menus to payment systems.
Creating QR codes requires different methods due to their complexity. Call the CreateQrCode() method from the QRCodeWriter class, which accepts 4 arguments: the barcode value, output QR code size, QRCodeWriter.QrErrorCorrectionLevel enum field, and QRVersion.
What Data Types Can I Use for QR Code Values?
QRCodeWriter.CreateQrCode() accepts numerical, alphabetical, or alphanumeric values as byte[] array, MemoryStream, or System.String types. QR codes excel at storing:
- URLs: Direct users to websites or landing pages
- Text: Store up to 4,296 alphanumeric characters
- Binary data: Encode files or complex data structures
- Structured data: vCards, WiFi credentials, or payment information
For Unicode support and international characters, see our writing Unicode barcodes tutorial.
How Do I Set the QR Code Size?
Specify QR code size directly as Int32 type in pixels (px). Default size is 500 px. Consider these factors:
- Display medium: Screen displays typically need 200-400px
- Print applications: Use 600px or higher for quality printing
- Scanning distance: Larger codes scan better from greater distances
What Is QR Error Correction Level and Why Does It Matter?
QRErrorCorrectionLevel determines fault tolerance, with four levels: Highest, High, Medium, and Low. Higher correction levels create more complex QR codes that resist reading errors even when damaged or partially obscured.
QRErrorCorrectionLevel.Highest
Generates the most complex QR code with 30% error correction. Supports logo stamping or image graphics. Ideal for customizing QR codes with logos.

QRErrorCorrectionLevel.High
Applies 25% error correction. Less complex than Highest level. Balances durability with data capacity.

QRErrorCorrectionLevel.Medium
Applies 15% error correction. Produces QR codes faster but with more error susceptibility. Suitable for most business applications with clear printing.

QRErrorCorrectionLevel.Low
Applies 7% error correction, creating the least complex QR code. Use when maximum data capacity is required with optimal scanning conditions.

When Should I Use Different QR Versions?
QR Version (1-40) determines symbol complexity and data capacity. Higher versions produce more complex codes storing more data. Setting version too low causes issues encoding larger data amounts. Version 0 automatically assigns the appropriate version based on encoded value. See QR Version for more information.
How Do I Create a QR Code Image with Specific Settings?
Use QRCodeWriter.CreateQrCode() to write a QR code and save it as an image:
using IronBarCode;
// Basic QR code creation
QRCodeWriter.CreateQrCode("IronBarcode1234", 250, QRCodeWriter.QrErrorCorrectionLevel.Medium, qrVersion: 0).SaveAsJpeg("QRMedium.jpeg");
// Advanced QR code with logo and custom styling
var qrCode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com", new QRCodeLogo("logo.png"), 500);
// Apply styling
qrCode.ChangeBarCodeColor(System.Drawing.Color.DarkBlue);
// Save in multiple formats
qrCode.SaveAsPng("styled-qr.png");
qrCode.SaveAsPdf("styled-qr.pdf");

The code encodes an alphanumeric value at 250 pixels with medium error correction. Version 0 lets the program select the appropriate QR code version. SaveAsJpeg() accepts the filename with JPEG extension.
Best Practices for Barcode Image Generation
For production applications:
- Choose the right format: PNG for web, TIFF for archival, JPEG for general use
- Test scanning: Verify barcodes with multiple scanning devices
- Apply proper margins: Use barcode margin settings for reliable scanning
- Consider performance: Explore async and multithreading options for high-volume generation
For additional examples and techniques, visit our C# Barcode Image Generator tutorial.
Frequently Asked Questions
What is the simplest way to create a barcode image in C# using IronBarcode?
IronBarcode provides a one-line API to create barcode images. You can use `BarcodeWriter.CreateBarcode()` method to generate a `Code128` barcode from a string with specified dimensions and save it as a PNG image.
What are some popular barcode formats supported by IronBarcode?
IronBarcode supports various barcode formats such as `Code128`, `Code39`, `UPC-A`, `QR Code`, `Data Matrix`, and many more. Each format is suitable for specific use cases like retail, inventory management, or document processing.
How can I save a generated barcode image to different file formats using IronBarcode?
The `GeneratedBarcode` object in IronBarcode can be saved in several formats, including PNG, JPEG, GIF, BMP, and TIFF. Use methods like `SaveAsPng()`, `SaveAsJpeg()`, or `SaveAsTiff()` to save your barcode image in the desired format.
Can IronBarcode generate QR codes with specific error correction levels?
Yes, IronBarcode allows setting QR error correction levels ranging from Low to Highest. This feature ensures your QR codes remain scannable even if damaged or partially obscured.
What types of data can be encoded as barcode values in IronBarcode?
IronBarcode can encode data types including `byte[] array`, `MemoryStream`, and `string`. Different barcode formats have specific requirements for data types and character sets.
How does IronBarcode ensure compliance with barcode dimension requirements?
IronBarcode allows you to specify width and height in pixels for your barcode images. If specified dimensions do not comply with format requirements, IronBarcode automatically adjusts them to include necessary whitespace.
What are the capabilities of QR codes generated using IronBarcode?
The QR codes generated by IronBarcode can store a variety of data types such as URLs, text, binary data, and structured data like vCards or WiFi credentials. They offer flexibility and customization for diverse applications.
How can I customize the appearance of barcode images with IronBarcode?
IronBarcode provides customization options like changing colors, adjusting margins, and applying text below the barcode. Additionally, you can embed logos within QR codes for branding purposes.
Why is it important to choose the correct image format for barcode generation?
Choosing the right image format ensures optimal quality for specific uses. For instance, PNG is best for web display, TIFF for archival purposes, and JPEG for general use. Proper format selection promotes reliable scanning and data integrity.
What best practices should I follow when generating barcode images with IronBarcode?
Best practices include choosing the right image format, testing barcodes with multiple scanners, setting proper margins, and considering performance needs by leveraging async and multithreading techniques for high-volume generation.
