How to generate, customize QR Code size in C#?

Developer Guide for QR Code 2D barcode image dimension size (width & height) in C#

On this page, we will provide a detailed introduction on how to use the QR Code library to generate the desired dimension size. At the same time, we will also explain how the library determines the various size‑related values of the QR Code based on your settings.

Here we will learn:
  • How to quickly generate a QR Code image with a specific size.
  • How to display, print a QR Code image in the browser that matches our expected size.
  • How to deeply customize the QR Code size in auto‑resize mode.
  • How to deeply customize the QR Code size in non‑auto‑resize mode.
  • Why a padding area appears around the QR Code in certain situations.

How to print QR Code with specified dimension size in C# ASP.NET application?

  1. Download C# QR Code Barcode Generator SDK
  2. Install C# library to print QR Code images in .NET projects
  3. Step by Step Tutorial










Create, Print QR Code with Specified Dimension Size in C#

You can easily generate QR Code with target image width and height using C# in ASP.NET web and Windows applications.
  1. Create a new QRCode object with text message https://www.onbarcode.com encoded
  2. Set the QR Code image width in property BarcodeWidth to 200 pixels. QR Code image height will be automatically the same as image width.
  3. Call method drawBarcode() to generate and print QR Code image file with specified width and height.
QRCode barcode = new QRCode();
barcode.Data = "https://www.onbarcode.com";
barcode.BarcodeWidth = 200;
barcode.drawBarcode("C://Output//qrcode-dimension-size.png");



Print QR Code with target unit of measure

Using BarcodeLib QR Code Generator library, you can choose your preferred unit of measure.
  • Set property UOM with your preferred unit of measure.
    Three options in enum UnitOfMeasure: PIXEL (default), CM, and INCH.
  • In property BarcodeWidth, set the QR Code side length using the selected unit of measurement.
barcode.UOM = UnitOfMeasure.INCH;
barcode.BarcodeWidth = 2f;




Print Specified QR Code dimension size in web browser

In this section, we will explain how to generate a QR Code with a side length of 2 inches in an ASP.NET project, display it in the browser, and then use the browser's print function to print it out, ensuring that the printed QR Code still has a side length of 2 inches.


Create QR Code with specified size in C# class

In ASP.NET Core web application, add the following C# code to cshtml.cs file.

  1. Define a model object BarcodeImage, which will store the created QR Code image data.

    public String BarcodeImage { get; set; } = "";
    

    • Create a QRCode object in C# class, and apply the unit of measure to INCH, and QR Code size length to 2-inch.
    • Set QR Code encoding text and other properties.
    • Call method drawBarcodeAsBytes() to print and output QR Code to a byte array object.
    • Store the QR Code byte array object to previously defined model object BarcodeImage
    QRCode barcode = new QRCode();
    barcode.UOM = UnitOfMeasure.INCH;
    barcode.BarcodeWidth = 2f;
    
    // Other QR Code settings, such as encoding text
    
    byte[] dataBytes = barcode.drawBarcodeAsBytes();
    BarcodeImage = System.Convert.ToBase64String(dataBytes);
    


Print QR Code in the web browser

In ASP.NET web app .cshtml file, add, insert the following img to the web page where you need print QR Code in browser.

<img src="data:image/png;base64,@Model.BarcodeImage"
     style="width: 2in; height: 2in;" />


Now run the ASP.NET Core web app, you can view and print QR Code image with specified dimension size in web browser.




QR Code Library Auto-Resize Mode

When the QR Code library's default auto resize is set to true, the SDK needs to calculate and derive appropriate Module Width/Height (X) based on the given QR Code Width (BarcodeWidth) and Height BarcodeHeight.

barcode.AutoResize = true;


How does the QR Code library calculate the bar module width (X)?

The following is the algorithm used by the QR Code Generator library to calculate the Module Width and Module Height.
  1. Calculate the number of modules in the horizontal direction of the QR Code symbol, denoted as N. (The number of modules in the horizontal and vertical directions is the same for a QR Code.)
  2. Based on the given QR Code Barcode Width (BarcodeWidth) and the Left/Right Margins (LeftMargin, RightMargin), calculate the QR Code Symbol Width:
    QR Code Symbol Width = Barcode Width - Left Margin - Right Margin
  3. Calculate the QR Code Module Width (X):
    X = Symbol Width / N
  4. Based on the given QR Code Barcode Height (BarcodeHeight) and the Top/Bottom Margins (TopMargin, BottomMargin), calculate the QR Code Symbol Height:
    QR Code Symbol Height = Barcode Height - Top Margin - Bottom Margin
  5. Calculate the QR Code Module Height (Y):
    Y = Symbol Height / N
Here is an example

QRCode qrcode = new QRCode();
qrcode.Data = "QRCode";

qrcode.BarcodeWidth = 410;
qrcode.BarcodeHeight = 310;

qrcode.LeftMargin = 100;
qrcode.RightMargin = 100;
qrcode.TopMargin = 50;
qrcode.BottomMargin = 50;
  • Number of Modules per Row (N): 21
  • Target Symbol Width = 410px - 100px - 100px = 210px
  • Module Width (X) = 210px / 21 = 10px


  • Number of Modules per Column (N): 21
  • Target Symbol Height = 310px - 50px - 50px = 210px
  • Module Height (Y) = 210px / 21 = 10px




Note: According to the QR Code standard, the QR Code Module Width must equal the Module Height. Therefore, when setting the properties, you must configure the Barcode Size and Margins appropriately to ensure that the Symbol Width and Symbol Height are equal. Otherwise, the generated QR Code will have different values for Module Width and Module Height.


Add padding area around QR Code symbole raster images

In raster images, the Module Width must be an integer number of pixels. When the calculated value of X is not an integer, the largest integer less than the calculated value should be used.

To ensure that the final generated image matches the set BarcodeWidth and BarcodeHeight, additional padding areas are added on both sides of the image.

The padding area defaults to a transparent color to distinguish it from the Left/Right Margins.

Here we will generate a QR Code image with the following size settings.
  • Barcode Width: 420px; Left Margin: 100px; Right Margin: 100px
  • Barcode Height: 320px; Left Margin: 50px; Right Margin: 50px

The C# QR Code library will calculate and get the following informatoin:
  • Number of Modules per Row (N): 21
  • Target Symbol Width = 420px - 100px - 100px = 220px
  • Module Width (X) = 220px / 21 = 10.4762px
  • Actual Module Width in Raster Image = 10px
  • Total Padding Width in horizontal = 420px - 10px * 21 - 100px - 100px = 10px
  • Left/Right Padding Width = 10px / 2 = 5px
QRCode qrcode = new QRCode();
qrcode.Data = "QRCode";

qrcode.BarcodeWidth = 420;
qrcode.BarcodeHeight = 320;

qrcode.LeftMargin = 100;
qrcode.RightMargin = 100;
qrcode.TopMargin = 50;
qrcode.BottomMargin = 50;



  • Number of Modules per Column (N): 21
  • Target Symbol Height = 320px - 50px - 50px = 220px
  • Module Height (Y) = 220px / 21 = 10.4762px
  • Actual Module Height in Raster Image = 10px
  • Total Padding Width in vertical = 320px - 10px * 21 - 50px - 50px = 10px
  • Top/Bottom Padding Width = 10px / 2 = 5px



Vector Images (SVG & EPS)

When outputting to a vector image, since the QR Code Module Size supports non‑integer values, no padding is added on either side of the image. The comparison of the effects between the two image types is shown below.

QRCode qrcode = new QRCode();
qrcode.Data = "QRCode";

qrcode.BarcodeWidth = 420;
qrcode.BarcodeHeight = 320;

qrcode.LeftMargin = 100;
qrcode.RightMargin = 100;
qrcode.TopMargin = 50;
qrcode.BottomMargin = 50;

qrcode.BackColor = Color.LightGray;
qrcode.OutputFileFormat = FileFormat.SVG;






QR Code Library Non Auto-Resize Mode (AutoResize = false)

Once the Module Width (X) is determined, the QR Code Size generated by the SDK based on these settings is fixed.

If, in this mode, the BarcodeWidth and BarcodeHeight properties are also set, there may be a discrepancy between these values and the Barcode Size computed by the SDK. The SDK will then determine the actual output Barcode Size depending on the situation.


No Barcode Width or Barcode Height is set.

The SDK directly creates the QR Code based on settings such as Module Width.

In the following C# sample codes, we will create a QR Code
  • Version 1 QR Code
  • Module Width (X): 5px
QRCode qrcode = new QRCode();
qrcode.Data = "QRCode";

qrcode.AutoResize = false;
qrcode.X = 5;


Here the QR Code library will print QR Code with the following properties
  • Number of Modules per Row (N): 21
  • Number of Modules per Column (N): 21
  • Output QR Code Image Size: Width = 105px; Height = 105px



Set specific Barcode Width and Barcode Height values.

When the given Barcode Width and Height exceed the barcode size generated by the SDK based on the Module Size and other property settings, the SDK will evenly add Padding Areas on all four sides (left, right, top, and bottom) of the image to ensure that the final image size equals the specified values.

In the following C# sample codes, we will create a QR Code
  • Version 1 QR Code
  • Module Width (X): 5px
QRCode qrcode = new QRCode();
qrcode.Data = "QRCode";

qrcode.AutoResize = false;
qrcode.X = 5;

qrcode.BarcodeWidth = 200;
qrcode.BarcodeHeight = 150;


Here the QR Code library will print QR Code with the following properties
  • QR Code Size calculated by SDK: Barcode Width = 105px; Barcode Height = 105px
  • Expected Barcode Size by BarcodeWidth & BarcodeHeight : Barcode Width = 200px; Barcode Height = 150px
  • Padding:
    • Left = (200px - 105px) / 2 = 48px
    • Right = 200px - 105px – 48px = 47px
    • Top = (150px - 105px) / 2 = 23px
    • Bottom = 150px - 105px - 23px = 22px
  • Output QR Code Image Size: Width = 200px; Height = 150px



Note: The color of the Padding Area is the same as the Barcode Background, and is defined by the BackColor property.


Special Case: The Barcode Size Setting Is Too Small

The C# QR Code Generator library will prioritize ensuring the accuracy of the Module Size (X) and other settings (e.g., Margins). If the given Barcode Width or Height is smaller than the barcode size generated by the library based on other properties, that setting will be ignored.

QRCode qrcode = new QRCode();
qrcode.Data = "QRCode";

qrcode.AutoResize = false;
qrcode.X = 5;

qrcode.BarcodeWidth = 90;       //  less than 105px
qrcode.BarcodeHeight = 150;


  • QR Code Barcode Size calculated by SDK: Barcode Width = 105px; Barcode Height = 105px
  • Expected Barcode Width (BarcodeWidth) = 90px (less than the value calculated by SDK)
  • Output Image Size: Width = 105px (Not 90px); Height = 150px




QR Code Image Padding Area Layout Settings

Supports setting the layout of the Padding Area via the BarAlignment property. (Only the horizontal direction is supported.)
  • AlignmentHori.Center (Default). The QR Code symbol is center‑aligned, and the Padding Area is evenly distributed on both the left and right sides.
  • AlignmentHori.Left. The QR Code symbol is aligned to the left, with all Padding Area located on the right.
  • AlignmentHori.Right. The QR Code symbol is right‑aligned, and the entire Padding Area is placed on the left side.

Here we will show how to print a QR Code with left bar alignment applied in C# program.

QRCode qrcode = new QRCode();
qrcode.Data = "QRCode";

qrcode.AutoResize = false;
qrcode.BarAlignment = AlignmentHori.Left;
qrcode.X = 5;

qrcode.BarcodeWidth = 200;
qrcode.BarcodeHeight = 150;





































Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.