Export Barcodes to Images and PDF
- 3 minutes to read
Use the BarcodeGenerator class to export barcodes to in-memory images and PDF documents.
Export API
| Method | Usage |
|---|---|
| BarcodeGenerator.Export | Exports a barcode to a stream. |
| BarcodeGenerator.ExportAsync | Exports a barcode to a stream asynchronously. |
| ExportToImage | Exports a barcode to a DXImage object. |
| BarcodeGenerator.ExportToImageAsync | Exports a barcode to a DXImage object asynchronously. |
| ExportToPdf | Exports a barcode to a PDF document. |
Export Barcodes to an Image
Use the BarcodeGenerator.ExportToImage method to obtain a DXImage object (for example, for image previews, post-processing, and custom save logic).
The following code snippet creates a barcode image in memory and saves it to a file:
using DevExpress.Docs.Barcode;
using DevExpress.Drawing;
using System.Drawing;
using var fileStream = new FileStream("logo.png", FileMode.Open);
var qrOptions = new QRCodeOptions();
qrOptions.Logo = DXImage.FromStream(fileStream);
qrOptions.Version = QRCodeVersion.Version10;
qrOptions.ModuleSize = 40;
qrOptions.BackColor = Color.LightGray;
qrOptions.ShowText = false;
using var qrOptionsStream = new FileStream(Path.Combine(outDir, "barcode_symbology_qr_code.png"), FileMode.Create, FileAccess.Write);
using var qrOptionsGenerator = new BarcodeGenerator(qrOptions);
var image = qrOptionsGenerator.ExportToImage("https://www.devexpress.com", DXImageFormat.Png);
image.Save(qrOptionsStream, System.Drawing.Imaging.ImageFormat.Png);
Use BarcodeGenerator.ExportToImage(Byte[], DXImageFormat) when the symbology requires binary input.
Export Barcodes to PDF
Use the BarcodeGenerator.ExportToPdf method to generate PDF output. This method writes PDF content to a stream.
using DevExpress.Docs.Barcode;
using DevExpress.Drawing;
using System.Drawing;
using var fileStream = new FileStream("logo.png", FileMode.Open);
var qrOptions = new QRCodeOptions();
qrOptions.Logo = DXImage.FromStream(fileStream);
qrOptions.Version = QRCodeVersion.Version10;
qrOptions.ModuleSize = 40;
qrOptions.BackColor = Color.LightGray;
qrOptions.ShowText = false;
using var qrOptionsStream = new FileStream(Path.Combine(outDir, "barcode_symbology_qr_code.pdf"), FileMode.Create, FileAccess.Write);
using var qrOptionsGenerator = new BarcodeGenerator(qrOptions);
qrOptionsGenerator.ExportToPdf("https://www.devexpress.com", qrOptionsStream);
Use the BarcodeGenerator.ExportToPdf(Byte[], Stream) method if you work with raw binary data.
Specify Character Encoding
Use an Encoding object when the text requires a specific encoding. These overloads that use the Encoding object are available for the ExportToImage and ExportToPdf methods.
The following code snippet exports text with UTF-8 encoding:
using DevExpress.Docs.Barcode;
using DevExpress.Drawing;
using System.Drawing;
using System.Text;
using var fileStream = new FileStream("logo.png", FileMode.Open);
var qrOptions = new QRCodeOptions();
qrOptions.Logo = DXImage.FromStream(fileStream);
qrOptions.Version = QRCodeVersion.Version10;
qrOptions.ModuleSize = 40;
qrOptions.BackColor = Color.LightGray;
qrOptions.ShowText = false;
using var qrOptionsStream = new FileStream(Path.Combine(outDir, "barcode_symbology_qr_code.pdf"), FileMode.Create, FileAccess.Write);
using var qrOptionsGenerator = new BarcodeGenerator(qrOptions);
qrOptionsGenerator.Export("https://www.devexpress.com", Encoding.UTF8, qrOptionsStream);
Note
Ensure that the selected barcode symbology supports characters in the encoded text.