Skip to content

Commit 623352e

Browse files
committed
clean up reticle rendering
1 parent 182293d commit 623352e

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

QRCoder/LogoQRCode.cs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Drawing;
55
using System.Drawing.Drawing2D;
66

7+
// pull request raised to extend library used.
78
namespace QRCoder
89
{
910
public class LogoQRCode : AbstractQRCode, IDisposable
@@ -15,17 +16,25 @@ public LogoQRCode() { }
1516

1617
public LogoQRCode(QRCodeData data) : base(data) { }
1718

18-
public Bitmap GetGraphic(QRCodeData qrCodeData,
19-
int pixelsPerModule,
19+
public Bitmap GetGraphic(Bitmap backgroundImage = null)
20+
{
21+
return this.GetGraphic(10, 7, Color.Black, Color.White, backgroundImage: backgroundImage);
22+
}
23+
24+
public Bitmap GetGraphic(int pixelsPerModule, Bitmap backgroundImage = null)
25+
{
26+
return this.GetGraphic(pixelsPerModule, (pixelsPerModule * 8) / 10, Color.Black, Color.White, backgroundImage: backgroundImage);
27+
}
28+
29+
public Bitmap GetGraphic(int pixelsPerModule,
2030
int pixelSize,
2131
Color darkColor,
2232
Color lightColor,
33+
Bitmap backgroundImage = null,
2334
bool drawQuietZones = false,
24-
Bitmap reticalImage = null,
25-
Base64QRCode.ImageType imgType = Base64QRCode.ImageType.Png,
26-
Bitmap backgroundImage = null)
35+
Bitmap reticleImage = null)
2736
{
28-
int numModules = qrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
37+
int numModules = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
2938
var offset = (drawQuietZones ? 0 : 4);
3039
int size = numModules * pixelsPerModule;
3140
var moduleMargin = pixelsPerModule - pixelSize;
@@ -52,22 +61,26 @@ public Bitmap GetGraphic(QRCodeData qrCodeData,
5261
{
5362
for (int y = 0; y < numModules; y += 1)
5463
{
55-
var solidBrush = (System.Drawing.Brush)(qrCodeData.ModuleMatrix[offset + y][offset + x] ? darkBrush : lightBrush);
64+
var solidBrush = (Brush)(this.QrCodeData.ModuleMatrix[offset + y][offset + x] ? darkBrush : lightBrush);
5665

57-
if (IsPartOfRetical(x, y, numModules, offset))
58-
if (reticalImage == null)
66+
if (IsPartOfReticle(x, y, numModules, offset))
67+
{
68+
if (reticleImage == null)
69+
{
5970
graphics.FillRectangle(solidBrush, new Rectangle(x * pixelsPerModule, y * pixelsPerModule, pixelsPerModule, pixelsPerModule));
60-
else
61-
graphics.FillEllipse(solidBrush, new Rectangle(x * pixelsPerModule + moduleMargin, y * pixelsPerModule + moduleMargin, pixelSize, pixelSize));
71+
}
72+
}
73+
else
74+
graphics.FillEllipse(solidBrush, new Rectangle(x * pixelsPerModule + moduleMargin, y * pixelsPerModule + moduleMargin, pixelSize, pixelSize));
6275
}
6376
}
6477

65-
if (reticalImage != null)
78+
if (reticleImage != null)
6679
{
6780
var reticleSize = 7 * pixelsPerModule;
68-
graphics.DrawImage(reticalImage, new Rectangle(0, 0, reticleSize, reticleSize));
69-
graphics.DrawImage(reticalImage, new Rectangle(size - reticleSize, 0, reticleSize, reticleSize));
70-
graphics.DrawImage(reticalImage, new Rectangle(0, size - reticleSize, reticleSize, reticleSize));
81+
graphics.DrawImage(reticleImage, new Rectangle(0, 0, reticleSize, reticleSize));
82+
graphics.DrawImage(reticleImage, new Rectangle(size - reticleSize, 0, reticleSize, reticleSize));
83+
graphics.DrawImage(reticleImage, new Rectangle(0, size - reticleSize, reticleSize, reticleSize));
7184
}
7285

7386
graphics.Save();
@@ -77,13 +90,13 @@ public Bitmap GetGraphic(QRCodeData qrCodeData,
7790
return bitmap;
7891
}
7992

80-
private bool IsPartOfRetical(int x, int y, int numModules, int offset)
93+
private bool IsPartOfReticle(int x, int y, int numModules, int offset)
8194
{
82-
var cornerSize = 7 + offset;
95+
var cornerSize = 11 - offset;
8396
return
8497
(x < cornerSize && y < cornerSize) ||
85-
(x > (numModules - cornerSize) && y < cornerSize) ||
86-
(x < cornerSize && y < (numModules - cornerSize));
98+
(x > (numModules - cornerSize - 1) && y < cornerSize) ||
99+
(x < cornerSize && y > (numModules - cornerSize - 1));
87100
}
88101

89102
private Bitmap Resize(Bitmap image, int size)

0 commit comments

Comments
 (0)