@@ -18,43 +18,39 @@ public ArtQRCode(QRCodeData data) : base(data) { }
18
18
19
19
public Bitmap GetGraphic ( int pixelsPerModule )
20
20
{
21
- return this . GetGraphic ( pixelsPerModule , ( pixelsPerModule * 8 ) / 10 , Color . Black , Color . White ) ;
21
+ return this . GetGraphic ( pixelsPerModule , Color . Black , Color . White , Color . Transparent ) ;
22
22
}
23
23
24
24
public Bitmap GetGraphic ( Bitmap backgroundImage = null )
25
25
{
26
- return this . GetGraphic ( 10 , 7 , Color . Black , Color . White , backgroundImage : backgroundImage ) ;
26
+ return this . GetGraphic ( 10 , Color . Black , Color . White , Color . Transparent , backgroundImage : backgroundImage ) ;
27
27
}
28
28
29
- public Bitmap GetGraphic (
30
- int pixelsPerModule ,
31
- int pixelSize ,
32
- Color darkColor ,
33
- Color lightColor ,
34
- bool drawQuietZones = false ,
35
- Bitmap reticleImage = null ,
36
- Bitmap backgroundImage = null )
29
+ public Bitmap GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , Color backgroundColor , Bitmap backgroundImage = null , double pixelSizeFactor = 0.8 ,
30
+ bool drawQuietZones = true , QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle . Flat , Bitmap finderPatternImage = null )
37
31
{
38
- var numModules = this . QrCodeData . ModuleMatrix . Count - ( drawQuietZones ? 0 : 8 ) ;
32
+ if ( pixelSizeFactor > 1 )
33
+ throw new Exception ( "pixelSize must be between 0 and 1. (0-100%)" ) ;
34
+ int pixelSize = ( int ) Math . Min ( pixelsPerModule , Math . Floor ( pixelsPerModule / pixelSizeFactor ) ) ;
35
+
36
+ var numModules = QrCodeData . ModuleMatrix . Count - ( drawQuietZones ? 0 : 8 ) ;
39
37
var offset = ( drawQuietZones ? 0 : 4 ) ;
40
38
var size = numModules * pixelsPerModule ;
41
39
42
- var bitmap = Resize ( backgroundImage , size ) ?? new Bitmap ( size , size ) ;
40
+ var bitmap = new Bitmap ( size , size ) ;
43
41
44
42
using ( var graphics = Graphics . FromImage ( bitmap ) )
45
43
{
46
44
using ( var lightBrush = new SolidBrush ( lightColor ) )
47
45
{
48
46
using ( var darkBrush = new SolidBrush ( darkColor ) )
49
47
{
50
- // make background transparent if you don't have an image -- not sure this is needed
51
- if ( backgroundImage == null )
52
- {
53
- using ( var brush = new SolidBrush ( Color . Transparent ) )
54
- {
55
- graphics . FillRectangle ( brush , new Rectangle ( 0 , 0 , size , size ) ) ;
56
- }
57
- }
48
+ // make background transparent
49
+ using ( var brush = new SolidBrush ( backgroundColor ) )
50
+ graphics . FillRectangle ( brush , new Rectangle ( 0 , 0 , size , size ) ) ;
51
+ //Render background if set
52
+ if ( backgroundImage != null )
53
+ graphics . DrawImage ( Resize ( backgroundImage , size ) , 0 , 0 ) ;
58
54
59
55
var darkModulePixel = MakeDotPixel ( pixelsPerModule , pixelSize , darkBrush ) ;
60
56
var lightModulePixel = MakeDotPixel ( pixelsPerModule , pixelSize , lightBrush ) ;
@@ -69,19 +65,22 @@ public Bitmap GetGraphic(
69
65
var solidBrush = pixelIsDark ? darkBrush : lightBrush ;
70
66
var pixelImage = pixelIsDark ? darkModulePixel : lightModulePixel ;
71
67
72
- if ( ! IsPartOfReticle ( x , y , numModules , offset ) )
73
- graphics . DrawImage ( pixelImage , rectangleF ) ;
74
- else if ( reticleImage == null )
68
+ if ( ! IsPartOfFinderPattern ( x , y , numModules , offset ) )
69
+ if ( drawQuietZones && quietZoneRenderingStyle == QuietZoneStyle . Flat && IsPartOfQuietZone ( x , y , numModules ) )
70
+ graphics . FillRectangle ( solidBrush , rectangleF ) ;
71
+ else
72
+ graphics . DrawImage ( pixelImage , rectangleF ) ;
73
+ else if ( finderPatternImage == null )
75
74
graphics . FillRectangle ( solidBrush , rectangleF ) ;
76
75
}
77
76
}
78
77
79
- if ( reticleImage != null )
78
+ if ( finderPatternImage != null )
80
79
{
81
- var reticleSize = 7 * pixelsPerModule ;
82
- graphics . DrawImage ( reticleImage , new Rectangle ( 0 , 0 , reticleSize , reticleSize ) ) ;
83
- graphics . DrawImage ( reticleImage , new Rectangle ( size - reticleSize , 0 , reticleSize , reticleSize ) ) ;
84
- graphics . DrawImage ( reticleImage , new Rectangle ( 0 , size - reticleSize , reticleSize , reticleSize ) ) ;
80
+ var finderPatternSize = 7 * pixelsPerModule ;
81
+ graphics . DrawImage ( finderPatternImage , new Rectangle ( 0 , 0 , finderPatternSize , finderPatternSize ) ) ;
82
+ graphics . DrawImage ( finderPatternImage , new Rectangle ( size - finderPatternSize , 0 , finderPatternSize , finderPatternSize ) ) ;
83
+ graphics . DrawImage ( finderPatternImage , new Rectangle ( 0 , size - finderPatternSize , finderPatternSize , finderPatternSize ) ) ;
85
84
}
86
85
87
86
graphics . Save ( ) ;
@@ -99,7 +98,7 @@ public Bitmap GetGraphic(
99
98
/// <param name="brush"></param>
100
99
/// <returns></returns>
101
100
private Bitmap MakeDotPixel ( int pixelsPerModule , int pixelSize , SolidBrush brush )
102
- {
101
+ {
103
102
// draw a dot
104
103
var bitmap = new Bitmap ( pixelSize , pixelSize ) ;
105
104
using ( var graphics = Graphics . FromImage ( bitmap ) )
@@ -124,13 +123,27 @@ private Bitmap MakeDotPixel(int pixelsPerModule, int pixelSize, SolidBrush brush
124
123
return cropped ;
125
124
}
126
125
127
- private bool IsPartOfReticle ( int x , int y , int numModules , int offset )
126
+
127
+ private bool IsPartOfQuietZone ( int x , int y , int numModules )
128
+ {
129
+ return
130
+ x < 4 || //left
131
+ y < 4 || //top
132
+ x > numModules - 5 || //right
133
+ y > numModules - 5 ; //bottom
134
+ }
135
+
136
+
137
+ private bool IsPartOfFinderPattern ( int x , int y , int numModules , int offset )
128
138
{
129
139
var cornerSize = 11 - offset ;
140
+ var outerLimitLow = ( numModules - cornerSize - 1 ) ;
141
+ var outerLimitHigh = outerLimitLow + 8 ;
142
+ var invertedOffset = 4 - offset ;
130
143
return
131
- ( x < cornerSize && y < cornerSize ) ||
132
- ( x > ( numModules - cornerSize - 1 ) && y < cornerSize ) ||
133
- ( x < cornerSize && y > ( numModules - cornerSize - 1 ) ) ;
144
+ ( x >= invertedOffset && x < cornerSize && y >= invertedOffset && y < cornerSize ) || //Top-left finder pattern
145
+ ( x > outerLimitLow && x < outerLimitHigh && y >= invertedOffset && y < cornerSize ) || //Top-right finder pattern
146
+ ( x >= invertedOffset && x < cornerSize && y > outerLimitLow && y < outerLimitHigh ) ; //Bottom-left finder pattern
134
147
}
135
148
136
149
/// <summary>
@@ -167,9 +180,14 @@ private Bitmap Resize(Bitmap image, int newSize)
167
180
graphics . DrawImage ( scaledImage , new Rectangle ( offsetX , offsetY , scaledWidth , scaledHeight ) ) ;
168
181
}
169
182
}
170
-
171
183
return bm ;
172
184
}
185
+
186
+ public enum QuietZoneStyle
187
+ {
188
+ Dotted ,
189
+ Flat
190
+ }
173
191
}
174
192
}
175
193
0 commit comments