How to Create Barcode Images
How to write barcode images in C#
- Download the C# IronBarcode Library
- Write OneDimensional Barcodes and Save as Image Files
- Write QR codes and Save as Image Files
Get started with IronBarcode
Start using IronBarcode in your project today with a free trial.
Write One-Dimensional Barcodes and Save as Image Files
Besides reading barcodes, IronBarcode is also a powerful tool that allow users to write and produce barcode images in a very short line of code. To achieve this, simply call CreateBarcode()
method from BarcodeWriter
class, whereby the barcode value, types, the width, and the height can be specified as the methods arguments. Simply attach a SaveAs()
method to the result to import the image file in local disk. Let's first discuss them and later see how to implement this in a code snippet.
Barcode Value
BarcodeWriter.CreateBarcode()
method accepts multiple types of barcode values. This includes value of type byte [] array
, MemoryStream
, and System.String
. This is very convenient for the users that wish to integrate IronBarcode into their application, as IronBarcode will be able to accept the value feed as is without having to cast or change the value type.
Barcode Encoding Types
IronBarcode supports wide range of barcode types for users to choose in producing their own unique barcodes. These barcode types have their own unique properties, specialties and uses. Kindly note however, not all barcode encoding types accepts the same type of values, as some of them accept numerical values only, alphabets only, or alphanumerical values. Therefore it is important to choose carefully which barcode type suits your use cases. Users can access the barcode types available from BarcodeEncoding
class. Refer to this API Reference for more information on supported barcode types in IronBarcode.
Width and Height
BarcodeWriter.CreateBarcode()
method also allow users to define the size of the barcode produced by allowing users to input desired width and height of the barcode into the method as arguments. Kindly note however, the measurement unit for the width and height of the barcode is in pixels(px). The default measurement for Width and Height is 250 px.
Import Barcodes as Image
The barcode produced from calling the BarcodeWriter.CreateBarcode()
method will be of GeneratedBarcode
class object. There are many things that we can do with this object, however, in this context, we will save the object as an image file into our local disk. The following are methods that the we can use to save the GeneratedBarcode
object according to desired image format:
SaveAsGif()
: This method saves theGeneratedBarcode
as a GIF image file and accepts image file path as string argument.SaveAsImage()
: This method saves theGeneratedBarcode
as an image file and accepts the image file path as string argument. Users must specify the desired image file format extension when specifying the image file path in the methodSaveAsJpeg()
: 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.
Creating a OneDimensional Barcode
Now we will use BarcodeWriter.CreateBarcode()
to demonstrate a one dimensional barcode creation, and save it to disk as an 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")
Write QR Codes and Save as Image File
One of the most popular barcodes nowadays, QR code, which is also one of the barcodes classified as a 2-dimensional barcode is fully supported by IronBarcode. Due to its versatility, cosmetic appeal, and highly customizable, 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.