Write a QR code to an image
QR code generation enables businesses to create seamless connections between physical and digital experiences. This sample demonstrates how to generate QR codes programmatically, supporting applications like marketing campaigns, contactless payments, event management, and customer engagement initiatives.
From retail businesses creating contactless menu access to marketing teams building interactive campaign materials, QR codes bridge the gap between physical touchpoints and digital engagement. Modern applications range from simple URL sharing to complex authentication workflows, inventory tracking systems, and mobile payment processing. The ability to generate high-quality QR codes programmatically enables businesses to integrate these capabilities directly into their existing workflows, from automated marketing materials to on-demand labeling systems.
Preparing the project
The first step involves initializing the SDK by registering the license. This needs to be done only once during the application’s lifetime and must occur before executing any further logic (see Getting Started with .NET SDK(opens in a new tab) for more details.)
using GdPicture14;
LicenseManager licence = new LicenseManager();licence.RegisterKEY(""); // Set your license keyCreating the image canvas
The QR code generation process begins by creating a new image canvas that will contain the generated QR code with specified dimensions and background properties.
using GdPictureImaging imaging = new GdPictureImaging();
// Create a new image canvas with specified dimensions and color depthint imageId = imaging.CreateNewGdPictureImage(600, 300, 32, imaging.ARGBI(255, 255, 255, 255));The CreateNewGdPictureImage method creates a blank image canvas with the specified parameters: width (600 pixels), height (300 pixels), color depth (32-bit for full color support), and background color (white with full opacity). This provides adequate space for the QR code plus any required quiet zones around the code for optimal scanning performance.
The ARGBI method creates a color value using Alpha, Red, Green, Blue, and Intensity components, enabling precise control over the canvas background color and ensuring optimal contrast for QR code scanning applications.
Generating the QR code
The core generation process renders the QR code onto the image canvas with comprehensive customization options for error correction, positioning, size, and visual appearance.
// Generate QR code with specified data and formatting parametersimaging.BarcodeQRWrite(imageId, "https://www.nutrient.io", BarcodeQREncodingMode.BarcodeQREncodingModeUndefined, BarcodeQRErrorCorrectionLevel.BarcodeQRErrorCorrectionLevelH, 0, 5, 5, 50, 50, 0, GdPicture14.Imaging.GdPictureColor.Black, GdPicture14.Imaging.GdPictureColor.White);The BarcodeQRWrite method generates the QR code with multiple configuration parameters that control every aspect of the output. The encoding mode is set to automatic detection, allowing the SDK to choose the optimal data encoding method based on the input content. The high error correction level (H) provides 30% recovery capability, ensuring the QR code remains scannable even if damaged or partially obscured.
The positioning parameters (X: 5, Y: 5) and dimensions (width: 50, height: 50) control the QR code placement and size within the image canvas. The color specification uses black modules against a white background for maximum contrast and scanning reliability across different devices and lighting conditions.
Saving the generated QR code
The final step exports the image containing the QR code to a file format suitable for printing, distribution, or integration with digital platforms and business applications.
// Save the generated QR code image as PNG formatimaging.SaveAsPNG(imageId, @"output_qr_code.png");The SaveAsPNG method exports the image in PNG format, which provides lossless compression ideal for maintaining the sharp edges and high contrast necessary for reliable QR code scanning. PNG format ensures that the precise QR code module definitions are preserved without compression artifacts that could affect scanning accuracy.
The verification step confirms successful file creation, providing quality assurance for automated generation workflows and ensuring the output is ready for use in marketing materials, labeling systems, or digital distribution channels.
QR Code Configuration Options
The SDK provides extensive customization capabilities:
- Error Correction Levels: Choose from L (7%), M (15%), Q (25%), or H (30%) recovery
- Encoding Modes: Automatic detection or specific modes for optimal data storage
- Size Control: Define dimensions and positioning within the image
- Color Customization: Set foreground and background colors for branding
- Data Capacity: Support for up to 4,296 alphanumeric characters
- Format Output: Generate PNG, JPEG, TIFF, or other image formats
Key Features
- High Reliability: Error correction ensures readability even when damaged
- Flexible Sizing: Scalable from small labels to large signage
- Brand Integration: Customizable colors and positioning for brand consistency
- Batch Generation: Create multiple QR codes efficiently for large campaigns
- Quality Control: High-resolution output suitable for print and digital use
Technical Benefits
- Memory-efficient image creation and processing
- Vector-based generation ensures crisp output at any size
- No external dependencies for QR code creation
- Consistent formatting across different output requirements
- Integration-ready for web services and automated workflows
This implementation provides the foundation for any application requiring QR code generation, from simple URL sharing to complex marketing automation systems.