How to Create Barcode Images
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 image file path as string argument.SaveAsJpeg()
: This method saves theGeneratedBarcode
as a JPEG image file and accepts image file path as string argument.SaveAsPng()
: This method saves theGeneratedBarcode
as a PNG image file and accepts image file path as string argument.SaveAsTiff()
: This method saves theGeneratedBarcode
as a TIFF image file and accepts image file path as string argument.SaveAsWindowsBitmap()
: This method saves theGeneratedBarcode
as a BMP image file and accepts image file path as 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
using IronBarCode;
BarcodeWriter.CreateBarcode("IronBarcode123", BarcodeEncoding.Code128, 200, 100).SaveAsJpeg("OneDBarcode.jpeg");
Imports IronBarCode
BarcodeWriter.CreateBarcode("IronBarcode123", BarcodeEncoding.Code128, 200, 100).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 creation of 1-dimensional and other barcodes, creating QR codes will be using different method from different class in IronBarcode because of the intricacy of QR code that would need different properties and arguments in order to produce a high quality QR codes as required by customers. To create QR codes in IronBarcode, users will need to call CreateQrCode()
method from QRCodeWriter
class. QRCodeWriter.CreateQrCode()
method accepts 4 arguments, whereby 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 is the QRVersion
. Let us discuss in detail on the arguments for this method.
QR Code Values
Same as BarcodeWriter.CreateBarcode()
method, QRCodeWriter.CreateQrCode()
first accept value for the QR Code which can be of 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) . Default QR code size is 500 px.
QR Error Correction Level
QRErrorCorrectionLevel
is a member property of QRCodeWriter
class whereby it has 4 fields, which are Highest, High, Medium, and Low. Basically, this property is the fault tolerance level of a QR code, in which higher correction level creates a 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 on the look of the QR code produced.
QRErrorCorrectionLevel.Highest
QR codes generated with Highest correction level will have the most complex QR code image, whereby 30% of it is error correction. QR code produced can also be stamped with logo or image graphics on the QR code.
QRErrorCorrectionLevel.High
Setting the property field to be High will result in application of 25% error correction in the QR code image. It will be less complex than 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 code faster, however, more prone to error.
QRErrorCorrectionLevel.Low
This is the lowest setting for error correction level, which only applies 7% error correction in the QR code image and produce the least complex QR Code.
QrVersion
QR Version is the symbole version of QR code that runs from 1 - 40, whereby higher QR version will produce a more complex QR code, that enable 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 encode. Please refer to this site for more information on QR version : QR Version
Create a QR Code image
Code snippet below demonstrate on how to use QRCodeWriter.CreateQrCode()
method in IronBarcode to write a QR code and save it as image file to disk by using SaveAsJpeg()
method.
:path=/static-assets/barcode/content-code-examples/how-to/create-barcode-images-qr.cs
using IronBarCode;
QRCodeWriter.CreateQrCode("IronBarcode1234", 250, QRCodeWriter.QrErrorCorrectionLevel.Medium, qrVersion: 0).SaveAsJpeg("QRMedium.jpeg");
Imports IronBarCode
QRCodeWriter.CreateQrCode("IronBarcode1234", 250, QRCodeWriter.QrErrorCorrectionLevel.Medium, qrVersion:= 0).SaveAsJpeg("QRMedium.jpeg")
From the code snippet above, alphanumerical value was used as value to be encoded in QR code, and we used 250 pixel 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 attached 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.