@@ -20,13 +20,13 @@ public PngByteQRCode(QRCodeData data) : base(data)
20
20
/// <summary>
21
21
/// Creates a black & white PNG of the QR code, using 1-bit grayscale.
22
22
/// </summary>
23
- public byte [ ] GetGraphic ( int pixelsPerModule )
23
+ public byte [ ] GetGraphic ( int pixelsPerModule , bool drawQuietZones = true )
24
24
{
25
25
using ( var png = new PngBuilder ( ) )
26
26
{
27
- var size = this . QrCodeData . ModuleMatrix . Count * pixelsPerModule ;
27
+ var size = ( this . QrCodeData . ModuleMatrix . Count - ( drawQuietZones ? 0 : 8 ) ) * pixelsPerModule ;
28
28
png . WriteHeader ( size , size , 1 , PngBuilder . ColorType . Greyscale ) ;
29
- png . WriteScanlines ( this . DrawScanlines ( pixelsPerModule ) ) ;
29
+ png . WriteScanlines ( this . DrawScanlines ( pixelsPerModule , drawQuietZones ) ) ;
30
30
png . WriteEnd ( ) ;
31
31
return png . GetBytes ( ) ;
32
32
}
@@ -35,14 +35,14 @@ public byte[] GetGraphic(int pixelsPerModule)
35
35
/// <summary>
36
36
/// Creates 2-color PNG of the QR code, using 1-bit indexed color. Accepts 3-byte RGB colors for normal images and 4-byte RGBA-colors for transparent images.
37
37
/// </summary>
38
- public byte [ ] GetGraphic ( int pixelsPerModule , byte [ ] darkColorRgba , byte [ ] lightColorRgba )
38
+ public byte [ ] GetGraphic ( int pixelsPerModule , byte [ ] darkColorRgba , byte [ ] lightColorRgba , bool drawQuietZones = true )
39
39
{
40
40
using ( var png = new PngBuilder ( ) )
41
41
{
42
- var size = this . QrCodeData . ModuleMatrix . Count * pixelsPerModule ;
42
+ var size = ( this . QrCodeData . ModuleMatrix . Count - ( drawQuietZones ? 0 : 8 ) ) * pixelsPerModule ;
43
43
png . WriteHeader ( size , size , 1 , PngBuilder . ColorType . Indexed ) ;
44
44
png . WritePalette ( darkColorRgba , lightColorRgba ) ;
45
- png . WriteScanlines ( this . DrawScanlines ( pixelsPerModule ) ) ;
45
+ png . WriteScanlines ( this . DrawScanlines ( pixelsPerModule , drawQuietZones ) ) ;
46
46
png . WriteEnd ( ) ;
47
47
return png . GetBytes ( ) ;
48
48
}
@@ -51,22 +51,23 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgba, byte[] light
51
51
/// <summary>
52
52
/// Creates a bitmap where each pixel is represented by a single bit, dark = 0 and light = 1.
53
53
/// </summary>
54
- private byte [ ] DrawScanlines ( int pixelsPerModule )
54
+ private byte [ ] DrawScanlines ( int pixelsPerModule , bool drawQuietZones )
55
55
{
56
56
var moduleMatrix = this . QrCodeData . ModuleMatrix ;
57
- var matrixSize = moduleMatrix . Count ;
57
+ var matrixSize = moduleMatrix . Count - ( drawQuietZones ? 0 : 8 ) ;
58
+ var quietZoneOffset = ( drawQuietZones ? 0 : 4 ) ;
58
59
var bytesPerScanline = ( matrixSize * pixelsPerModule + 7 ) / 8 + 1 ; // A monochrome scanline is one byte for filter type then one bit per pixel.
59
60
var scanlines = new byte [ bytesPerScanline * matrixSize * pixelsPerModule ] ;
60
61
61
62
for ( var y = 0 ; y < matrixSize ; y ++ )
62
63
{
63
- var modules = moduleMatrix [ y ] ;
64
+ var modules = moduleMatrix [ y + quietZoneOffset ] ;
64
65
var scanlineOffset = y * pixelsPerModule * bytesPerScanline ;
65
66
66
67
// Draw a scanline with the modules from the QR code.
67
68
for ( var x = 0 ; x < matrixSize ; x ++ )
68
69
{
69
- if ( modules [ x ] )
70
+ if ( modules [ x + quietZoneOffset ] )
70
71
{
71
72
continue ;
72
73
}
@@ -319,22 +320,22 @@ private static uint Crc32(byte[] data, int index, int length)
319
320
320
321
public static class PngByteQRCodeHelper
321
322
{
322
- public static byte [ ] GetQRCode ( string plainText , int pixelsPerModule , byte [ ] darkColorRgba , byte [ ] lightColorRgba , ECCLevel eccLevel , bool forceUtf8 = false , bool utf8BOM = false , EciMode eciMode = EciMode . Default , int requestedVersion = - 1 )
323
+ public static byte [ ] GetQRCode ( string plainText , int pixelsPerModule , byte [ ] darkColorRgba , byte [ ] lightColorRgba , ECCLevel eccLevel , bool forceUtf8 = false , bool utf8BOM = false , EciMode eciMode = EciMode . Default , int requestedVersion = - 1 , bool drawQuietZones = true )
323
324
{
324
325
using ( var qrGenerator = new QRCodeGenerator ( ) )
325
326
using ( var qrCodeData = qrGenerator . CreateQrCode ( plainText , eccLevel , forceUtf8 , utf8BOM , eciMode , requestedVersion ) )
326
327
using ( var qrCode = new PngByteQRCode ( qrCodeData ) )
327
- return qrCode . GetGraphic ( pixelsPerModule , darkColorRgba , lightColorRgba ) ;
328
+ return qrCode . GetGraphic ( pixelsPerModule , darkColorRgba , lightColorRgba , drawQuietZones ) ;
328
329
}
329
330
330
331
331
332
332
- public static byte [ ] GetQRCode ( string txt , QRCodeGenerator . ECCLevel eccLevel , int size )
333
+ public static byte [ ] GetQRCode ( string txt , QRCodeGenerator . ECCLevel eccLevel , int size , bool drawQuietZones = true )
333
334
{
334
335
using ( var qrGen = new QRCodeGenerator ( ) )
335
336
using ( var qrCode = qrGen . CreateQrCode ( txt , eccLevel ) )
336
337
using ( var qrPng = new PngByteQRCode ( qrCode ) )
337
- return qrPng . GetGraphic ( size ) ;
338
+ return qrPng . GetGraphic ( size , drawQuietZones ) ;
338
339
}
339
340
}
340
341
}
0 commit comments