Sample code for 30+ languages & platforms
C# Requires Chilkat v11.7.0+

Measure a QR Code without Rendering It

See more QR Code Examples

Demonstrates the measure operation, which does everything generate does except draw the image. It reports the version chosen and the exact size the image would have, which is useful for laying out a page or a label before any image is made. Unlike generate, a code that does not fit the limits is not a failure for measure: the call succeeds, fits is false, and reasons[] says why.

Chilkat C# Downloads

C#
bool success = false;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

Chilkat.JsonObject json = new Chilkat.JsonObject();
json.UpdateString("payload.text","https://www.chilkatsoft.com/barcode_overview.asp");
json.UpdateInt("render.moduleSize",8);

Chilkat.Formats fmt = new Chilkat.Formats();
Chilkat.JsonObject jsonOut = new Chilkat.JsonObject();
success = fmt.QrCode("measure",json,jsonOut);
if (success == false) {
    Debug.WriteLine(fmt.LastErrorText);
    return;
}

Debug.WriteLine("fits: " + Convert.ToString(jsonOut.BoolOf("fits")));
Debug.WriteLine("QR version: " + Convert.ToString(jsonOut.IntOf("symbols[0].selectedEncoding.version")));
Debug.WriteLine("image would be: " + Convert.ToString(jsonOut.IntOf("measurements[0].canvasWidth")) + " x " + Convert.ToString(jsonOut.IntOf("measurements[0].canvasHeight"))
     + " pixels");
Debug.WriteLine("symbol: " + Convert.ToString(jsonOut.IntOf("measurements[0].symbolWidth")) + " pixels wide at x = " + Convert.ToString(jsonOut.IntOf("measurements[0].symbolX")));

//  Would it fit in a space 200 pixels wide?
json.UpdateInt("render.maxWidth",200);
success = fmt.QrCode("measure",json,jsonOut);
if (success == false) {
    Debug.WriteLine(fmt.LastErrorText);
    return;
}

Debug.WriteLine("within 200 pixels, fits: " + Convert.ToString(jsonOut.BoolOf("fits")));
if (jsonOut.BoolOf("fits") == false) {
    Debug.WriteLine("reason: " + jsonOut.StringOf("reasons[0].code") + " (" + jsonOut.StringOf("reasons[0].message") + ")");
}

//  What measure reported, drawn as a diagram.  The measure operation itself makes no image:

//  image

//  Sample output:

//  fits: True
//  QR version: 4
//  image would be: 328 x 328 pixels
//  symbol: 264 pixels wide at x = 32
//  within 200 pixels, fits: False
//  reason: noFit (exceedsMaxWidth)