Skip to content

Commit 05a1e53

Browse files
committed
Fixed icon-border/-background rendering
1 parent c9314f9 commit 05a1e53

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

QRCoder/QRCode.cs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
5858
return bmp;
5959
}
6060

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)
6262
{
6363
var size = (this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8)) * pixelsPerModule;
6464
var offset = drawQuietZones ? 0 : 4 * pixelsPerModule;
@@ -72,36 +72,34 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
7272
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
7373
gfx.CompositingQuality = CompositingQuality.HighQuality;
7474
gfx.Clear(lightColor);
75-
7675
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+
9277
for (var x = 0; x < size + offset; x = x + pixelsPerModule)
9378
{
9479
for (var y = 0; y < size + offset; y = y + pixelsPerModule)
9580
{
9681
var moduleBrush = this.QrCodeData.ModuleMatrix[(y + pixelsPerModule) / pixelsPerModule - 1][(x + pixelsPerModule) / pixelsPerModule - 1] ? darkBrush : lightBrush;
97-
9882
gfx.FillRectangle(moduleBrush , new Rectangle(x - offset, y - offset, pixelsPerModule, pixelsPerModule));
9983
}
10084
}
10185

10286
if (drawIconFlag)
10387
{
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);
10493
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+
}
105103
gfx.DrawImage(icon, iconDestRect, new RectangleF(0, 0, icon.Width, icon.Height), GraphicsUnit.Pixel);
106104
}
107105

@@ -129,7 +127,7 @@ internal GraphicsPath CreateRoundedRectanglePath(RectangleF rect, int cornerRadi
129127

130128
public static class QRCodeHelper
131129
{
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)
133131
{
134132
using (var qrGenerator = new QRCodeGenerator())
135133
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))

0 commit comments

Comments
 (0)