QRCode encoder margin

Mar 28, 2014 at 6:17 AM
Hi,

I notice that when encoding a QRCode, the generated barcode has quite a thick white margin.
When I tried generating a 60x60 pixels barcode, the actual matrix is only 30x30 pixels while the white margin already taken up 15 pixels on each side.

I want to generate a barcode bitmap that's as close to the edge as possible.
Is it possible to change this in the code and where?

Thanks.
Coordinator
Mar 28, 2014 at 8:51 AM
Edited Mar 28, 2014 at 8:51 AM
You can use the property QrCodeEncodingOptions.Margin.
var writer = new BarcodeWriter
{
   Format = BarcodeFormat.QR_CODE,
   Options = new QrCodeEncodingOptions
   {
      Margin = 2,
      Width = 60,
      Height = 60
   }
};
var bitmap = writer.Write("your content here");
Mar 31, 2014 at 3:07 AM
I did but still, the margin is quite large.
I tried with a 60x60 pixels barcode with margin of 1 but the output image still has 12 pixels of white bar on all 4 sides.
Is this intensional?
What's the unit used for the margin parameter? Pixels?
Thanks.
Coordinator
Apr 1, 2014 at 7:47 PM
You are right, depending on the content and the requested output size and margin it adds some more white pixels.
The renderer of the bit matrix doesn't support anti aliasing or something similar.
That means if there are for example 35x35 pixel needed for the content and you want an output of 60x60 it will add a bigger quiet zone.
On the other hand if you want an output of 70x70 pixels the renderer will stretch the matrix by 2.
The margin units are pixel (the width and height, too).

If you want more control over the stretching process you can only use the following workaround:
var writer = new BarcodeWriter
{
   Format = BarcodeFormat.QR_CODE,
   Options = new QrCodeEncodingOptions
   {
      Margin = 0,
      Width = 1,
      Height = 1
   }
};
var bitmap = writer.Write("your content here");

// and now do some manual stretching ...
Apr 4, 2014 at 7:39 AM
I tried your suggestion but I cannot control the size of the resultant image.

I have no choice but to clone the barcode image after barcode generation, taking only the square containing the matrix.

Thanks.