Skip to content

Commit 191cfce

Browse files
committed
Added bacjkgroundImageStyle and fixed test cases
1 parent 5d057d1 commit 191cfce

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

QRCoder/ArtQRCode.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ public Bitmap GetGraphic(Bitmap backgroundImage = null)
5353
/// <param name="pixelSizeFactor">Value between 0.0 to 1.0 that defines how big the module dots are. The bigger the value, the less round the dots will be.</param>
5454
/// <param name="drawQuietZones">If true a white border is drawn around the whole QR Code</param>
5555
/// <param name="quietZoneRenderingStyle">Style of the quiet zones</param>
56+
/// <param name="backgroundImageStyle">Style of the background image (if set). Fill=spanning complete graphic; DataAreaOnly=Don't paint background into quietzone</param>
5657
/// <param name="finderPatternImage">Optional image that should be used instead of the default finder patterns</param>
5758
/// <returns>QRCode graphic as bitmap</returns>
5859
public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Color backgroundColor, Bitmap backgroundImage = null, double pixelSizeFactor = 0.8,
59-
bool drawQuietZones = true, QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle.Flat, Bitmap finderPatternImage = null)
60+
bool drawQuietZones = true, QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle.Dotted,
61+
BackgroundImageStyle backgroundImageStyle = BackgroundImageStyle.DataAreaOnly, Bitmap finderPatternImage = null)
6062
{
6163
if (pixelSizeFactor > 1)
6264
throw new Exception("The parameter pixelSize must be between 0 and 1. (0-100%)");
@@ -79,7 +81,16 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
7981
graphics.FillRectangle(brush, new Rectangle(0, 0, size, size));
8082
//Render background if set
8183
if (backgroundImage != null)
82-
graphics.DrawImage(Resize(backgroundImage, size), 0, 0);
84+
{
85+
if (backgroundImageStyle == BackgroundImageStyle.Fill)
86+
graphics.DrawImage(Resize(backgroundImage, size), 0, 0);
87+
else if (backgroundImageStyle == BackgroundImageStyle.DataAreaOnly)
88+
{
89+
var bgOffset = 4 - offset;
90+
graphics.DrawImage(Resize(backgroundImage, size - (2 * bgOffset * pixelsPerModule)), 0 + (bgOffset * pixelsPerModule), (bgOffset * pixelsPerModule));
91+
}
92+
}
93+
8394

8495
var darkModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, darkBrush);
8596
var lightModulePixel = MakeDotPixel(pixelsPerModule, pixelSize, lightBrush);
@@ -232,6 +243,15 @@ public enum QuietZoneStyle
232243
Dotted,
233244
Flat
234245
}
246+
247+
/// <summary>
248+
/// Defines how the background image (if set) shall be rendered.
249+
/// </summary>
250+
public enum BackgroundImageStyle
251+
{
252+
Fill,
253+
DataAreaOnly
254+
}
235255
}
236256

237257
public static class ArtQRCodeHelper
@@ -253,16 +273,18 @@ public static class ArtQRCodeHelper
253273
/// <param name="pixelSizeFactor">Value between 0.0 to 1.0 that defines how big the module dots are. The bigger the value, the less round the dots will be.</param>
254274
/// <param name="drawQuietZones">If true a white border is drawn around the whole QR Code</param>
255275
/// <param name="quietZoneRenderingStyle">Style of the quiet zones</param>
276+
/// <param name="backgroundImageStyle">Style of the background image (if set). Fill=spanning complete graphic; DataAreaOnly=Don't paint background into quietzone</param>
256277
/// <param name="finderPatternImage">Optional image that should be used instead of the default finder patterns</param>
257278
/// <returns>QRCode graphic as bitmap</returns>
258279
public static Bitmap GetQRCode(string plainText, int pixelsPerModule, Color darkColor, Color lightColor, Color backgroundColor, ECCLevel eccLevel, bool forceUtf8 = false,
259280
bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, Bitmap backgroundImage = null, double pixelSizeFactor = 0.8,
260-
bool drawQuietZones = true, QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle.Flat, Bitmap finderPatternImage = null)
281+
bool drawQuietZones = true, QuietZoneStyle quietZoneRenderingStyle = QuietZoneStyle.Flat,
282+
BackgroundImageStyle backgroundImageStyle = BackgroundImageStyle.DataAreaOnly, Bitmap finderPatternImage = null)
261283
{
262284
using (var qrGenerator = new QRCodeGenerator())
263285
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
264286
using (var qrCode = new ArtQRCode(qrCodeData))
265-
return qrCode.GetGraphic(pixelsPerModule, darkColor, lightColor, backgroundColor, backgroundImage, pixelSizeFactor, drawQuietZones, quietZoneRenderingStyle, finderPatternImage);
287+
return qrCode.GetGraphic(pixelsPerModule, darkColor, lightColor, backgroundColor, backgroundImage, pixelSizeFactor, drawQuietZones, quietZoneRenderingStyle, backgroundImageStyle, finderPatternImage);
266288
}
267289
}
268290
}

QRCoderTests/ArtQRCodeRendererTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public void can_create_standard_qrcode_graphic()
2424

2525
var result = HelperFunctions.BitmapToHash(bmp);
2626
#if NET35_OR_GREATER || NET40_OR_GREATER
27-
result.ShouldBe("ea1a9008dbfe7825a6e17ed228a83a6f");
27+
result.ShouldBe("11ebdda91b9632d016798cb6de2f5339");
2828
#else
29-
result.ShouldBe("9e18d03a3f2a4ca0fb603350f9764d6d");
29+
result.ShouldBe("cb38c3156eaf13cdfba699bdafc3a84c");
3030
#endif
3131
}
3232

@@ -41,9 +41,9 @@ public void can_create_standard_qrcode_graphic_with_custom_finder()
4141

4242
var result = HelperFunctions.BitmapToHash(bmp);
4343
#if NET35_OR_GREATER || NET40_OR_GREATER
44-
result.ShouldBe("a6895846ed6b56de997bfcb2c4a72736");
44+
result.ShouldBe("c54a7389ae995abc838f0d228acc3bad");
4545
#else
46-
result.ShouldBe("8185b055473b170afa56152ec46c4be4");
46+
result.ShouldBe("1102c0c6f235eaf4c3ac639f82f17bfa");
4747
#endif
4848
}
4949

@@ -75,9 +75,9 @@ public void can_create_standard_qrcode_graphic_with_background()
7575
var result = HelperFunctions.BitmapToHash(bmp);
7676

7777
#if NET35_OR_GREATER || NET40_OR_GREATER
78-
result.ShouldBe("9f4f860d0c15ed4b1258dbbea89c4b4b");
78+
result.ShouldBe("2caa9c0ee8fcb4a93841debb58cf41bc");
7979
#else
80-
result.ShouldBe("16f3ad423cc0690cc2e259d3731400cd");
80+
result.ShouldBe("bbea08507282773175cfe7b52f0ddae4");
8181
#endif
8282
}
8383

0 commit comments

Comments
 (0)