Set Barcodes Margins
using IronBarCode;

// Specify Code 128 Format
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128);

// Add barcode value text below the barcode
barcode.AddBarcodeValueTextBelowBarcode();

// Save it as JPG
barcode.SaveAsJpeg("code128-sample.jpg");
NuGet Download
PM > Install-Package BarCode
Set Barcodes Margins

Set Barcodes Margins

The margins of the barcode are the blank space that surrounds the code. This area is critical for scanner reliability. It enables the scanner to identify where the barcodebeginsandends, thereby separating it from nearby text or graphics. Without proper margins, a scan can fail or misread data. In this code example, we will demonstrate how to set a consistent margin on a barcode with IronBarcode to ensure readability.

3-Step Guide to Setting Barcode Margins

  • using IronBarCode;
  • GeneratedBarcode qrcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode", BarcodeWriterEncoding.QRCode);
  • qrcode.SetMargins(100);

Code Explanation

We first import the IronBarcode library, then we generate a barcode using the BarcodeWriter.CreateBarcode method. We pass two arguments to the method: the string value and the type of the barcode. In the example above, these are "https://ironsoftware.com/csharp/barcode" and BarcodeWriterEncoding.QRCode, respectively.

After creating the GeneratedBarcode object, we call the SetMargins method. We pass it a single integer value, which represents the margin in pixels. In this case, 100 applies a 100-pixel quiet zone to all four sides of the generated QR code.

Discover how to enhance your barcode designs with in-depth Guide!
View on GitHub

Ready to Get Started?

Nuget Downloads 2,540,041Version:2026.11just released