How to Create Barcode Images in C#
- Download the C# IronBarcode Library
- Generate barcodes and save as image files
- Generate QR codes and save as image files
Generate Barcodes and Save as Image Files
Besides reading barcodes, IronBarcode is a powerful tool that also allows users to write barcodes with very minimal coding. To achieve this, simply call the CreateBarcode()
method from the BarcodeWriter
class, where the barcode value, type, width, and height can be specified in the method parameters. This outputs a GeneratedBarcode
object, which can then be saved as an image file using the SaveAs()
method. Let's discuss each parameter in detail and later see how to implement barcode writing with a code snippet.
Barcode Value
The BarcodeWriter.CreateBarcode()
method accepts multiple data types for barcode values. These include byte[] array
, MemoryStream
, and string
. The lengths of the string and what characters are accepted vary depending on the barcode type, but this is all detailed within our documentation.
Barcode Encoding Types
IronBarcode supports a wide variety of barcode formats for writing—the complete list can be found in our Supported Barcode Formats article. These barcode types all have their own unique properties, specialties, and uses—you can research which one is the best fit for your use case with our documentation.
Width and Height
Set the width and height of the output barcode image in pixels. By default, both measurements are set at 250 px. Several barcode types, such as QR and PDF417, require having certain dimensions to be compliant, so for input widths and heights that don't match the required dimensions of the barcode, the barcode will be generated at the compliant dimensions and whitespace will fill the remaining space. If the dimensions are too small for the barcode, an exception will be thrown.
Import Barcodes as Image
When creating a barcode with the BarcodeWriter.CreateBarcode()
method, a GeneratedBarcode
object will be generated. With this object, we can save the barcode to a variety of image types with several SaveAs()
methods specific to each image format. These methods include:
SaveAsGif()
: This method saves theGeneratedBarcode
as a GIF image file and accepts the image file path as a string argument.SaveAsJpeg()
: This method saves theGeneratedBarcode
as a JPEG image file and accepts the image file path as a string argument.SaveAsPng()
: This method saves theGeneratedBarcode
as a PNG image file and accepts the image file path as a string argument.SaveAsTiff()
: This method saves theGeneratedBarcode
as a TIFF image file and accepts the image file path as a string argument.SaveAsWindowsBitmap()
: This method saves theGeneratedBarcode
as a BMP image file and accepts the image file path as a string argument.SaveAsImage()
: This is a general method for saving theGeneratedBarcode
as an image file—users must specify the desired file format extension when inputting the file path.
Generate a Barcode and Save As Image File
Now we will use BarcodeWriter.CreateBarcode()
to demonstrate the creation of a Code128 barcode, and save it to disk as a JPEG image file.
:path=/static-assets/barcode/content-code-examples/how-to/create-barcode-images-one-dimensional.cs
// Import the IronBarCode library to the project to handle barcode generation and manipulation
using IronBarCode;
// This code snippet demonstrates how to generate a barcode using the IronBarCode library.
// The barcode is created using the Code128 encoding standard and saved as a JPEG image file.
// Create a barcode with specific text ("IronBarcode123"), using Code128 encoding.
// The barcode image is initialized with dimensions: a width of 200 pixels and a height of 100 pixels.
var barcode = BarcodeWriter.CreateBarcode(
"IronBarcode123", // Text to encode within the barcode
BarcodeEncoding.Code128, // Encoding method to use (Code128 standard in this case)
width: 200, // Width of the barcode image in pixels
height: 100 // Height of the barcode image in pixels
);
// Save the generated barcode as a JPEG image file named "OneDBarcode.jpeg".
// This method serializes the barcode image into a JPEG formatted file located in the default directory.
barcode.SaveAsJpeg("OneDBarcode.jpeg");
' Import the IronBarCode library to the project to handle barcode generation and manipulation
Imports IronBarCode
' This code snippet demonstrates how to generate a barcode using the IronBarCode library.
' The barcode is created using the Code128 encoding standard and saved as a JPEG image file.
' Create a barcode with specific text ("IronBarcode123"), using Code128 encoding.
' The barcode image is initialized with dimensions: a width of 200 pixels and a height of 100 pixels.
Private barcode = BarcodeWriter.CreateBarcode("IronBarcode123", BarcodeEncoding.Code128, width:= 200, height:= 100)
' Save the generated barcode as a JPEG image file named "OneDBarcode.jpeg".
' This method serializes the barcode image into a JPEG formatted file located in the default directory.
barcode.SaveAsJpeg("OneDBarcode.jpeg")

Generate QR Codes and Save as Image Files
One of the most popular barcodes nowadays, QR code, which is also one of the barcodes classified as a two-dimensional barcode, is fully supported by IronBarcode. Due to its versatility, cosmetic appeal, and highly customizable features, QR codes have gained high popularity among users.
Different from the creation of 1-dimensional and other barcodes, creating QR codes will be using a different method from a different class in IronBarcode because of the intricacy of QR codes which require different properties and arguments to produce high-quality QR codes as required by customers. To create QR codes in IronBarcode, users will need to call the CreateQrCode()
method from the QRCodeWriter
class. QRCodeWriter.CreateQrCode()
accepts 4 arguments: the barcode value as the first argument, size of the output QR code as the second, QRCodeWriter.QrErrorCorrectionLevel
enum field as the third argument, and lastly the QRVersion
. Let us discuss the arguments for this method in detail.
QR Code Values
Similar to the BarcodeWriter.CreateBarcode()
method, QRCodeWriter.CreateQrCode()
first accepts a value for the QR Code which can be numerical, alphabetical, or alphanumerical. These values can be input inside the method as byte[] array
, MemoryStream
, and System.String
type.
QR Code Size
Users can also specify the size of the QR code directly into the method as Int32
type. The measurement unit for QR code size used in this method is in pixels (px). The default QR code size is 500 px.
QR Error Correction Level
QRErrorCorrectionLevel
is a member property of the QRCodeWriter
class where it has 4 fields: Highest, High, Medium, and Low. Basically, this property is the fault tolerance level of a QR code, where a higher correction level creates more complex QR codes that are less prone to reading errors, even if it is damaged, or partially obscured. Now, let us discuss in detail each of the fields in this property, as well as look at the difference in the appearance of the QR code produced.
QRErrorCorrectionLevel.Highest
QR codes generated with the Highest correction level will have the most complex QR code image, with 30% of it being error correction. The QR code produced can also be stamped with a logo or image graphics on the QR code.

QRErrorCorrectionLevel.High
Setting the property field to be High will result in the application of 25% error correction in the QR code image. It will be less complex than the QR code image produced from QRErrorCorrectionLevel.Highest
.

QRErrorCorrectionLevel.Medium
This field applies only 15% error correction in the QR code image. By using this setting, users will get to produce QR codes faster; however, they are more prone to errors.

QRErrorCorrectionLevel.Low
This is the lowest setting for error correction level, which only applies 7% error correction in the QR code image, resulting in the least complex QR Code.

QR Version
QR Version is the symbol version of a QR code that ranges from 1 to 40. A higher QR version will produce a more complex QR code, enabling users to store more data, and vice versa for the lower version of QR code. Please note, however, if the QR version is set too low, issues might occur when users are trying to encode more data than what is allowed by the version. Setting the QR version as 0 will automatically assign the appropriate QR version based on the value to be encoded. Please refer to this site for more information on QR version: QR Version
Create a QR Code Image
The code snippet below demonstrates how to use the QRCodeWriter.CreateQrCode()
method in IronBarcode to write a QR code and save it as an image file to disk by using the SaveAsJpeg()
method.
:path=/static-assets/barcode/content-code-examples/how-to/create-barcode-images-qr.cs
using IronBarCode;
// This script generates a QR code using the IronBarCode library.
// Create a QR code with the specified text, size, and error correction level
var qrCode = QRCodeWriter.CreateQrCode(
content: "IronBarcode1234", // The content to be encoded in the QR code
size: 250, // Dimensions of the QR code in pixels
errorCorrection: QRCodeWriter.ErrorCorrection.Medium, // Error correction level
qrVersion: QRCodeWriter.QrVersion.Auto // QR version, use 'Auto' for automatic version decision
);
// Save the generated QR code as a JPEG image file
qrCode.SaveAsJpeg("QRMedium.jpeg");
Imports IronBarCode
' This script generates a QR code using the IronBarCode library.
' Create a QR code with the specified text, size, and error correction level
Private qrCode = QRCodeWriter.CreateQrCode(content:= "IronBarcode1234", size:= 250, errorCorrection:= QRCodeWriter.ErrorCorrection.Medium, qrVersion:= QRCodeWriter.QrVersion.Auto)
' Save the generated QR code as a JPEG image file
qrCode.SaveAsJpeg("QRMedium.jpeg")

From the code snippet above, an alphanumerical value was used as the value to be encoded in the QR code, and we used 250 pixels as the measurement of the produced QR code. We also specified the error correction of the produced QR code to be medium and let the program decide which QR code version is suitable for our QR code value. Apart from that, we also used SaveAsJpeg()
that accepts the QR code image file name with the image format extension, which is a JPEG in this case, to be saved as the argument.
Frequently Asked Questions
How do I create barcode images using IronBarcode in C#?
To create a barcode image in C#, download the IronBarcode library, use the BarcodeWriter.CreateBarcode() method, specify the barcode value, type, width, and height, and save the output as an image file using the SaveAs() method.
What types of barcodes can I generate with IronBarcode?
IronBarcode supports a wide variety of barcode formats, including Code128, QR codes, and others. The complete list is available in the IronBarcode documentation.
How do I save a generated barcode as a specific image format?
You can save a generated barcode as a specific image format using methods like SaveAsGif(), SaveAsJpeg(), SaveAsPng(), SaveAsTiff(), and SaveAsWindowsBitmap(). Specify the image file path as a string argument.
What is the default size for barcodes in IronBarcode?
The default size for barcodes in IronBarcode is 250x250 pixels.
How do I create a QR code with IronBarcode?
To create a QR code, use the QRCodeWriter.CreateQrCode() method, providing the barcode value, size, error correction level, and QR version. Save the QR code using a method like SaveAsJpeg().
What is the QR error correction level in IronBarcode?
The QR error correction level in IronBarcode determines the fault tolerance of a QR code, with options like Highest, High, Medium, and Low, affecting the complexity and error correction.
Can IronBarcode handle different data types for barcode values?
Yes, IronBarcode's BarcodeWriter.CreateBarcode() and QRCodeWriter.CreateQrCode() methods accept values as byte[] arrays, MemoryStream, and strings.
What happens if the specified dimensions for a barcode are too small?
If the specified dimensions are too small, an exception will be thrown, or the barcode will be generated at compliant dimensions with whitespace filling the space.
How does QR version affect the QR code in IronBarcode?
The QR version in IronBarcode determines the complexity and data capacity of a QR code. Higher versions store more data, while lower versions may not support larger data inputs.
What programming namespace is needed to use IronBarcode?
You need to import the IronBarCode namespace to use IronBarcode in your C# projects.