How to Add Margins to QR Codes
Scaling a QR code correctly is a vital step in balancing visual impact with technical readability in brand advertising. To achieve this, using margins (often called "quiet zones") improves QR code readability and ensures that scanning devices recognize the code correctly within unique layouts. Specific adjustments to width, height, and overall scale directly influence the final output ratio and visual presentation.
This guide demonstrates how to use margins to effectively resize and frame your QR code with IronQR, ensuring a perfect fit for any professional layout.
How to Add Margins to QR Codes
- Download the IronQR C# library to add margins to QR codes
- Generate a QR code with a string value using
QrWriter - Apply a uniform quiet zone to all four sides using
Marginsproperty - Apply a margin to each side of the QR code with
MarginTop,MarginBottom,MarginLeft,MarginRight - Save the QR code as an image for external use
Adding Margins to QR Code
Establishing a proper quiet zone can be achieved by configuring the Margins property within the QrStyleOptions class. This property accepts a single integer that defines a uniform blank border around all four sides of the QR code.
In this example, we define a style configuration with a margin of 40 pixels, apply these settings to the QR code during the rendering process, and then save the resulting image as a PNG file.
:path=/static-assets/qr/content-code-examples/how-to/add-margins-qr-code.csusing IronQr;
using IronSoftware.Drawing;
QrStyleOptions styleOptions = new QrStyleOptions
{
// Set all margins to 40px
Margins = 40,
};
string url = "https://ironsoftware.com/csharp/qr/";
// Create QR code
QrCode qr = QrWriter.Write(url);
// Save QR code as a bitmap using the style options
AnyBitmap qrImage = qr.Save(styleOptions);
// Save QR code bitmap as file
qrImage.SaveAs("qr-uniform-margins.png");Output

Adding Margins to Specific Sides
For asymmetrical layouts, use the MarginTop, MarginBottom, MarginLeft, and MarginRight properties within QrStyleOptions. This provides precise control over each side of the quiet zone independently.
In this example, custom values are defined for each margin alongside specific dimensions and a background color.
:path=/static-assets/qr/content-code-examples/how-to/add-specific-margins-qr-code.csusing IronQr;
using IronSoftware.Drawing;
// Configure QR code styling with individual margins and background color
QrStyleOptions combinedOptions = new QrStyleOptions
{
Dimensions = 400,
MarginTop = 50,
MarginBottom = 50,
MarginLeft = 20,
MarginRight = 20,
BackgroundColor = IronSoftware.Drawing.Color.LightYellow,
};
string url = "https://ironsoftware.com/";
// Generate QR code from URL
QrCode qrCombined = QrWriter.Write(url);
// Apply styling options and export as bitmap
AnyBitmap qrCombinedImage = qrCombined.Save(combinedOptions);
// Save QR code to file
qrCombinedImage.SaveAs("qr-custom-margins.png");Output
As you can see, the vertical margins are visibly larger than horizontal sides.

Generated QR Codes with Added Margins

Uniform Margins

Individual Margins






