How to Create Barcode & QR Code Images in C#

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 Line

Use 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.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    IronBarCode.BarcodeWriter.CreateBarcode("Sample123", BarcodeEncoding.Code128, 250, 100).SaveAsPng("Barcode.png");
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer

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: MaxiCode for shipping, Databar for 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 file
  • SaveAsJpeg(): Saves as JPEG image file
  • SaveAsPng(): Saves as PNG image file (recommended for web)
  • SaveAsTiff(): Saves as TIFF image file (ideal for archival/printing)
  • SaveAsWindowsBitmap(): Saves as BMP image file
  • SaveAsImage(): 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");
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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
Generated one-dimensional barcode with black vertical bars showing example output from barcode creation code

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.

QR code generated with highest error correction level showing dense black and white pattern with positioning squares

QRErrorCorrectionLevel.High

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

QR code example showing high error correction level with dense data pattern and standard positioning elements

QRErrorCorrectionLevel.Medium

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

Generated QR code example showing black and white data matrix with corner position markers

QRErrorCorrectionLevel.Low

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

Sample QR code generated with low error correction level showing standard black and white module pattern

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 custom styling
var qrCode = QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.High);

// Add logo and styling
qrCode.AddLogo("logo.png");
qrCode.ChangeBarCodeColor(System.Drawing.Color.DarkBlue);

// Save in multiple formats
qrCode.SaveAsPng("styled-qr.png");
qrCode.SaveAsPdf("styled-qr.pdf");
using IronBarCode;

// Basic QR code creation
QRCodeWriter.CreateQrCode("IronBarcode1234", 250, QRCodeWriter.QrErrorCorrectionLevel.Medium, qrVersion: 0).SaveAsJpeg("QRMedium.jpeg");

// Advanced QR code with custom styling
var qrCode = QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, QRCodeWriter.QrErrorCorrectionLevel.High);

// Add logo and styling
qrCode.AddLogo("logo.png");
qrCode.ChangeBarCodeColor(System.Drawing.Color.DarkBlue);

// Save in multiple formats
qrCode.SaveAsPng("styled-qr.png");
qrCode.SaveAsPdf("styled-qr.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
QR code example showing medium error correction level with standard finder patterns and data encoding structure

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:

  1. Choose the right format: PNG for web, TIFF for archival, JPEG for general use
  2. Test scanning: Verify barcodes with multiple scanning devices
  3. Apply proper margins: Use barcode margin settings for reliable scanning
  4. 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

How do I create a barcode image in C# using a single line of code?

With IronBarcode, you can create a barcode image in one line using the BarcodeWriter.CreateBarcode() method. Simply pass your data, encoding type, width, and height, then save it as an image file. For example: IronBarCode.BarcodeWriter.CreateBarcode("Sample123", BarcodeEncoding.Code128, 250, 100).SaveAsPng("Barcode.png");

What types of data can I encode in barcodes?

IronBarcode's BarcodeWriter.CreateBarcode() method accepts multiple data types including byte arrays, MemoryStreams, and strings. String lengths and accepted characters vary by barcode type - Code128 supports the full ASCII character set while UPC-A requires exactly 12 numeric digits. For binary data, you can use byte arrays to encode complex data structures.

Which barcode formats are supported for image generation?

IronBarcode supports numerous barcode formats including linear/1D barcodes (Code128, Code39, UPC-A, UPC-E, EAN-8, EAN-13), 2D barcodes (QR Code, Data Matrix, PDF417, Aztec), and specialized formats like MaxiCode for shipping and Databar for retail. Each format serves specific uses, with Code128 handling general alphanumeric data and QR codes offering superior error correction for mobile scanning.

How do I set the dimensions of my barcode image?

You can set barcode image dimensions in pixels directly in the CreateBarcode() method parameters. IronBarcode uses default measurements of 250px for both width and height. Some barcode types like QR and PDF417 may require specific dimensions for compliance, and the library automatically adjusts when input dimensions don't match requirements.

What image file formats can I save my generated barcodes as?

IronBarcode allows you to save generated barcodes in multiple image formats using the SaveAs() method. You can save as PNG using SaveAsPng(), as well as other common formats like JPEG, BMP, and TIFF. The GeneratedBarcode object returned by CreateBarcode() provides various save methods for different file types.

Can I generate QR codes as well as traditional barcodes?

Yes, IronBarcode fully supports QR code generation alongside traditional barcodes. You can create QR codes using the same CreateBarcode() method by specifying BarcodeEncoding.QRCode as the encoding type. QR codes offer advantages like superior error correction and are ideal for mobile scanning applications.

Hairil Hasyimi Bin Omar
Software Engineer
Like all great engineers, Hairil is an avid learner. He’s refining his knowledge of C#, Python, and Java, using that knowledge to add value to team members across Iron Software. Hairil joined the Iron Software team from Universiti Teknologi MARA in Malaysia, where he graduated with a Bachelor's degree ...
Read More
Ready to Get Started?
Nuget Downloads 1,990,480 | Version: 2025.12 just released