@@ -58,7 +58,7 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
58
58
return bmp ;
59
59
}
60
60
61
- public Bitmap GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , Bitmap icon = null , int iconSizePercent = 15 , int iconBorderWidth = 6 , bool drawQuietZones = true )
61
+ public Bitmap GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , Bitmap icon = null , int iconSizePercent = 15 , int iconBorderWidth = 0 , bool drawQuietZones = true , Color ? iconBackgroundColor = null )
62
62
{
63
63
var size = ( this . QrCodeData . ModuleMatrix . Count - ( drawQuietZones ? 0 : 8 ) ) * pixelsPerModule ;
64
64
var offset = drawQuietZones ? 0 : 4 * pixelsPerModule ;
@@ -72,36 +72,34 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
72
72
gfx . InterpolationMode = InterpolationMode . HighQualityBicubic ;
73
73
gfx . CompositingQuality = CompositingQuality . HighQuality ;
74
74
gfx . Clear ( lightColor ) ;
75
-
76
75
var drawIconFlag = icon != null && iconSizePercent > 0 && iconSizePercent <= 100 ;
77
-
78
- GraphicsPath iconPath = null ;
79
- float iconDestWidth = 0 , iconDestHeight = 0 , iconX = 0 , iconY = 0 ;
80
-
81
- if ( drawIconFlag )
82
- {
83
- iconDestWidth = iconSizePercent * bmp . Width / 100f ;
84
- iconDestHeight = drawIconFlag ? iconDestWidth * icon . Height / icon . Width : 0 ;
85
- iconX = ( bmp . Width - iconDestWidth ) / 2 ;
86
- iconY = ( bmp . Height - iconDestHeight ) / 2 ;
87
-
88
- var centerDest = new RectangleF ( iconX - iconBorderWidth , iconY - iconBorderWidth , iconDestWidth + iconBorderWidth * 2 , iconDestHeight + iconBorderWidth * 2 ) ;
89
- iconPath = this . CreateRoundedRectanglePath ( centerDest , iconBorderWidth * 2 ) ;
90
- }
91
-
76
+
92
77
for ( var x = 0 ; x < size + offset ; x = x + pixelsPerModule )
93
78
{
94
79
for ( var y = 0 ; y < size + offset ; y = y + pixelsPerModule )
95
80
{
96
81
var moduleBrush = this . QrCodeData . ModuleMatrix [ ( y + pixelsPerModule ) / pixelsPerModule - 1 ] [ ( x + pixelsPerModule ) / pixelsPerModule - 1 ] ? darkBrush : lightBrush ;
97
-
98
82
gfx . FillRectangle ( moduleBrush , new Rectangle ( x - offset , y - offset , pixelsPerModule , pixelsPerModule ) ) ;
99
83
}
100
84
}
101
85
102
86
if ( drawIconFlag )
103
87
{
88
+ float iconDestWidth = iconSizePercent * bmp . Width / 100f ;
89
+ float iconDestHeight = drawIconFlag ? iconDestWidth * icon . Height / icon . Width : 0 ;
90
+ float iconX = ( bmp . Width - iconDestWidth ) / 2 ;
91
+ float iconY = ( bmp . Height - iconDestHeight ) / 2 ;
92
+ var centerDest = new RectangleF ( iconX - iconBorderWidth , iconY - iconBorderWidth , iconDestWidth + iconBorderWidth * 2 , iconDestHeight + iconBorderWidth * 2 ) ;
104
93
var iconDestRect = new RectangleF ( iconX , iconY , iconDestWidth , iconDestHeight ) ;
94
+ var iconBgBrush = iconBackgroundColor != null ? new SolidBrush ( ( Color ) iconBackgroundColor ) : lightBrush ;
95
+ //Only render icon/logo background, if iconBorderWith is set > 0
96
+ if ( iconBorderWidth > 0 )
97
+ {
98
+ using ( GraphicsPath iconPath = CreateRoundedRectanglePath ( centerDest , iconBorderWidth * 2 ) )
99
+ {
100
+ gfx . FillPath ( iconBgBrush , iconPath ) ;
101
+ }
102
+ }
105
103
gfx . DrawImage ( icon , iconDestRect , new RectangleF ( 0 , 0 , icon . Width , icon . Height ) , GraphicsUnit . Pixel ) ;
106
104
}
107
105
@@ -129,7 +127,7 @@ internal GraphicsPath CreateRoundedRectanglePath(RectangleF rect, int cornerRadi
129
127
130
128
public static class QRCodeHelper
131
129
{
132
- public static Bitmap GetQRCode ( string plainText , int pixelsPerModule , Color darkColor , Color lightColor , ECCLevel eccLevel , bool forceUtf8 = false , bool utf8BOM = false , EciMode eciMode = EciMode . Default , int requestedVersion = - 1 , Bitmap icon = null , int iconSizePercent = 15 , int iconBorderWidth = 6 , bool drawQuietZones = true )
130
+ public static Bitmap GetQRCode ( string plainText , int pixelsPerModule , Color darkColor , Color lightColor , ECCLevel eccLevel , bool forceUtf8 = false , bool utf8BOM = false , EciMode eciMode = EciMode . Default , int requestedVersion = - 1 , Bitmap icon = null , int iconSizePercent = 15 , int iconBorderWidth = 0 , bool drawQuietZones = true )
133
131
{
134
132
using ( var qrGenerator = new QRCodeGenerator ( ) )
135
133
using ( var qrCodeData = qrGenerator . CreateQrCode ( plainText , eccLevel , forceUtf8 , utf8BOM , eciMode , requestedVersion ) )
0 commit comments