Skip to content

Commit c2e640c

Browse files
committed
Use ToRectangle()
1 parent 974245b commit c2e640c

14 files changed

Lines changed: 38 additions & 98 deletions

File tree

Demo-ASP.NET-Core/QrCodeBitmapExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,9 @@ public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border, SKCol
5252
// draw modules
5353
using (SKPaint paint = new SKPaint { Color = foreground })
5454
{
55-
for (int y = 0; y < size; y++)
55+
foreach (var rect in qrCode.ToRectangles())
5656
{
57-
for (int x = 0; x < size; x++)
58-
{
59-
if (qrCode.GetModule(x, y))
60-
{
61-
canvas.DrawRect((x + border) * scale, (y + border) * scale, scale, scale, paint);
62-
}
63-
}
57+
canvas.DrawRect((rect.X + border) * scale, (rect.Y + border) * scale, rect.Width * scale, rect.Height * scale, paint);
6458
}
6559
}
6660
}

Demo-ImageMagick/Demo-ImageMagick.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="14.13.0" />
12+
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="14.13.1" />
1313
<PackageReference Include="Net.Codecrete.QrCodeGenerator" Version="3.0.0" />
1414
</ItemGroup>
1515

Demo-ImageMagick/QrCodeImageExtensions.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,13 @@ public static MagickImage ToImage(this QrCode qrCode, int scale, int border, Mag
6060
var drawables = new Drawables();
6161
drawables.FillColor(foreground);
6262

63-
for (var x = 0; x < size; x++)
63+
foreach (var rect in qrCode.ToRectangles())
6464
{
65-
var pointerX = (x + border) * scale;
66-
67-
for (var y = 0; y < size; y++)
68-
{
69-
if (qrCode.GetModule(x, y))
70-
{
71-
var pointerY = (y + border) * scale;
72-
drawables.Rectangle(pointerX, pointerY, pointerX + scale - 1, pointerY + scale - 1);
73-
}
74-
}
65+
var pointerX = (rect.X + border) * scale;
66+
var pointerY = (rect.Y + border) * scale;
67+
drawables.Rectangle(pointerX, pointerY, pointerX + rect.Width * scale - 1, pointerY + rect.Height * scale - 1);
7568
}
69+
7670
drawables.Draw(image);
7771
return image;
7872
}

Demo-ImageSharp/QrCodeBitmapExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,9 @@ public static Image ToBitmap(this QrCode qrCode, int scale, int border, Color fo
5050
img.Fill(background);
5151

5252
// draw modules
53-
for (int y = 0; y < size; y++)
53+
foreach (var rect in qrCode.ToRectangles())
5454
{
55-
for (int x = 0; x < size; x++)
56-
{
57-
if (qrCode.GetModule(x, y))
58-
{
59-
img.Fill(foreground, new Rectangle((x + border) * scale, (y + border) * scale, scale, scale));
60-
}
61-
}
55+
img.Fill(foreground, new Rectangle((rect.X + border) * scale, (rect.Y + border) * scale, rect.Width * scale, rect.Height * scale));
6256
}
6357
});
6458

Demo-QRCode-Variety/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private static void EmojisWithoutEci()
108108
// Some readers always ignore the ECI designator.
109109
var qrCode = QrCode.EncodeTextAdvanced("🎲 😇 🤒 🏌 ⏭ 🚍", QrCode.Ecc.Quartile,
110110
encoding: Encoding.UTF8, eci: ECI.None);
111-
SaveAsSvg(qrCode, "emojis-qr.svg");
111+
SaveAsSvg(qrCode, "emojis-no-eci-qr.svg");
112112
}
113113

114114
private static void BinaryData()

Demo-SkiaSharp/QrCodeBitmapExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,9 @@ public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border, SKCol
5252
// draw modules
5353
using (SKPaint paint = new SKPaint { Color = foreground })
5454
{
55-
for (int y = 0; y < size; y++)
55+
foreach (var rect in qrCode.ToRectangles())
5656
{
57-
for (int x = 0; x < size; x++)
58-
{
59-
if (qrCode.GetModule(x, y))
60-
{
61-
canvas.DrawRect((x + border) * scale, (y + border) * scale, scale, scale, paint);
62-
}
63-
}
57+
canvas.DrawRect((rect.X + border) * scale, (rect.Y + border) * scale, rect.Width * scale, rect.Height * scale, paint);
6458
}
6559
}
6660
}

Demo-System-Drawing/QrCodeBitmapExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,9 @@ public static void Draw(this QrCode qrCode, Graphics graphics, float scale, floa
130130
// draw modules
131131
using (SolidBrush brush = new SolidBrush(foreground))
132132
{
133-
for (int y = 0; y < size; y++)
133+
foreach (var rect in qrCode.ToRectangles())
134134
{
135-
for (int x = 0; x < size; x++)
136-
{
137-
if (qrCode.GetModule(x, y))
138-
{
139-
graphics.FillRectangle(brush, (x + border) * scale, (y + border) * scale, scale, scale);
140-
}
141-
}
135+
graphics.FillRectangle(brush, (rect.X + border) * scale, (rect.Y + border) * scale, rect.Width * scale, rect.Height * scale);
142136
}
143137
}
144138
}

Demo-WinForms/QrCodeBitmapExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,9 @@ public static void Draw(this QrCode qrCode, Graphics graphics, float scale, floa
130130
// draw modules
131131
using (SolidBrush brush = new SolidBrush(foreground))
132132
{
133-
for (int y = 0; y < size; y++)
133+
foreach (var rect in qrCode.ToRectangles())
134134
{
135-
for (int x = 0; x < size; x++)
136-
{
137-
if (qrCode.GetModule(x, y))
138-
{
139-
graphics.FillRectangle(brush, (x + border) * scale, (y + border) * scale, scale, scale);
140-
}
141-
}
135+
graphics.FillRectangle(brush, (rect.X + border) * scale, (rect.Y + border) * scale, rect.Width * scale, rect.Height * scale);
142136
}
143137
}
144138
}

Demo-WinUI/Demo-WinUI/Views/QrCodeDrawing.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public static class QrCodeDrawing
3434
/// <param name="background"></param>
3535
public static void Draw(QrCode qrCode, CanvasDrawingSession drawingSession, int borderWidth, Color foreground, Color? background)
3636
{
37+
// turn off anti-aliasing to get crisp edges and avoid gray lines between modules
38+
drawingSession.Antialiasing = CanvasAntialiasing.Aliased;
39+
3740
int size = qrCode.Size;
3841

3942
// draw the background
@@ -43,16 +46,10 @@ public static void Draw(QrCode qrCode, CanvasDrawingSession drawingSession, int
4346
}
4447

4548
// draw the modules
46-
for (int y = 0; y < size; y++)
49+
foreach (var rect in qrCode.ToRectangles())
4750
{
48-
for (int x = 0; x < size; x++)
49-
{
50-
if (qrCode.GetModule(x, y))
51-
{
52-
var rect = new Rect(x + borderWidth, y + borderWidth, 1, 1);
53-
drawingSession.FillRectangle(rect, foreground);
54-
}
55-
}
51+
var r = new Rect(rect.X + borderWidth, rect.Y + borderWidth, rect.Width, rect.Height);
52+
drawingSession.FillRectangle(r, foreground);
5653
}
5754
}
5855

Demo-WindowsPresentationFoundation/QrCodeDrawing.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,10 @@ public static void Draw(QrCode qrCode, DrawingContext drawingContext, int border
7777
}
7878

7979
// draw the modules
80-
for (int y = 0; y < size; y++)
80+
foreach (var r in qrCode.ToRectangles())
8181
{
82-
for (int x = 0; x < size; x++)
83-
{
84-
if (qrCode.GetModule(x, y))
85-
{
86-
var rect = new Rect(x + borderWidth, y + borderWidth, 1, 1);
87-
drawingContext.DrawRectangle(foreground, null, rect);
88-
}
89-
}
82+
var rect = new Rect(r.X + borderWidth, r.Y + borderWidth, r.Width, r.Height);
83+
drawingContext.DrawRectangle(foreground, null, rect);
9084
}
9185
}
9286

0 commit comments

Comments
 (0)