Skip to content

Commit 9fe1f2b

Browse files
committed
Added opt. parameter to render logos on SvgQRCodes
1 parent 613ef03 commit 9fe1f2b

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

QRCoder/SvgQRCode.cs

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,33 @@ public string GetGraphic(int pixelsPerModule)
2121
var viewBox = new Size(pixelsPerModule*this.QrCodeData.ModuleMatrix.Count, pixelsPerModule * this.QrCodeData.ModuleMatrix.Count);
2222
return this.GetGraphic(viewBox, Color.Black, Color.White);
2323
}
24-
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute)
24+
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
2525
{
2626
var offset = drawQuietZones ? 0 : 4;
2727
var edgeSize = this.QrCodeData.ModuleMatrix.Count * pixelsPerModule - (offset * 2 * pixelsPerModule);
2828
var viewBox = new Size(edgeSize, edgeSize);
29-
return this.GetGraphic(viewBox, darkColor, lightColor, drawQuietZones, sizingMode);
29+
return this.GetGraphic(viewBox, darkColor, lightColor, drawQuietZones, sizingMode, logo);
3030
}
3131

32-
public string GetGraphic(int pixelsPerModule, string darkColorHex, string lightColorHex, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute)
32+
public string GetGraphic(int pixelsPerModule, string darkColorHex, string lightColorHex, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
3333
{
3434
var offset = drawQuietZones ? 0 : 4;
3535
var edgeSize = this.QrCodeData.ModuleMatrix.Count * pixelsPerModule - (offset * 2 * pixelsPerModule);
3636
var viewBox = new Size(edgeSize, edgeSize);
37-
return this.GetGraphic(viewBox, darkColorHex, lightColorHex, drawQuietZones, sizingMode);
37+
return this.GetGraphic(viewBox, darkColorHex, lightColorHex, drawQuietZones, sizingMode, logo);
3838
}
3939

40-
public string GetGraphic(Size viewBox, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute)
40+
public string GetGraphic(Size viewBox, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
4141
{
42-
return this.GetGraphic(viewBox, Color.Black, Color.White, drawQuietZones, sizingMode);
42+
return this.GetGraphic(viewBox, Color.Black, Color.White, drawQuietZones, sizingMode, logo);
4343
}
4444

45-
public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute)
45+
public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
4646
{
47-
return this.GetGraphic(viewBox, ColorTranslator.ToHtml(Color.FromArgb(darkColor.ToArgb())), ColorTranslator.ToHtml(Color.FromArgb(lightColor.ToArgb())), drawQuietZones, sizingMode);
47+
return this.GetGraphic(viewBox, ColorTranslator.ToHtml(Color.FromArgb(darkColor.ToArgb())), ColorTranslator.ToHtml(Color.FromArgb(lightColor.ToArgb())), drawQuietZones, sizingMode, logo);
4848
}
4949

50-
public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute)
50+
public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
5151
{
5252
int offset = drawQuietZones ? 0 : 4;
5353
int drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : offset * 2);
@@ -122,6 +122,15 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
122122
}
123123
}
124124
}
125+
126+
//Render logo, if set
127+
if (logo != null)
128+
{
129+
svgFile.AppendLine($@"<svg width=""100%"" height=""100%"" version=""1.1"" xmlns = ""http://www.w3.org/2000/svg"">");
130+
svgFile.AppendLine($@"<image x=""{50 - (logo.GetIconSizePercent() / 2)}%"" y=""{50 - (logo.GetIconSizePercent() / 2)}%"" width=""{logo.GetIconSizePercent()}%"" height=""{logo.GetIconSizePercent()}%"" href=""{logo.GetDataUri()}"" />");
131+
svgFile.AppendLine(@"</svg>");
132+
}
133+
125134
svgFile.Append(@"</svg>");
126135
return svgFile.ToString();
127136
}
@@ -137,16 +146,64 @@ public enum SizingMode
137146
WidthHeightAttribute,
138147
ViewBoxAttribute
139148
}
149+
150+
public class SvgLogo
151+
{
152+
private string _logoData;
153+
private string _mediaType;
154+
private int _iconSizePercent;
155+
156+
/// <summary>
157+
/// Create a logo object to be used in SvgQRCode renderer
158+
/// </summary>
159+
/// <param name="iconRasterized">Logo to be rendered as Bitmap/rasterized graphic</param>
160+
/// <param name="iconSizePercent">Degree of percentage coverage of the QR code by the logo</param>
161+
public SvgLogo(Bitmap iconRasterized, int iconSizePercent = 15)
162+
{
163+
_iconSizePercent = iconSizePercent;
164+
using (var ms = new System.IO.MemoryStream())
165+
{
166+
using (var bitmap = new Bitmap(iconRasterized))
167+
{
168+
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
169+
_logoData = Convert.ToBase64String(ms.GetBuffer(), Base64FormattingOptions.None);
170+
}
171+
}
172+
_mediaType = "image/png";
173+
}
174+
175+
/// <summary>
176+
/// Create a logo object to be used in SvgQRCode renderer
177+
/// </summary>
178+
/// <param name="iconVectorized">Logo to be rendered as SVG/vectorized graphic/string</param>
179+
/// <param name="iconSizePercent">Degree of percentage coverage of the QR code by the logo</param>
180+
public SvgLogo(string iconVectorized, int iconSizePercent = 15)
181+
{
182+
_iconSizePercent = iconSizePercent;
183+
_logoData = Convert.ToBase64String(Encoding.UTF8.GetBytes(iconVectorized), Base64FormattingOptions.None);
184+
_mediaType = "image/svg+xml";
185+
}
186+
187+
public string GetDataUri()
188+
{
189+
return $"data:{_mediaType};base64,{_logoData}";
190+
}
191+
192+
public int GetIconSizePercent()
193+
{
194+
return _iconSizePercent;
195+
}
196+
}
140197
}
141198

142199
public static class SvgQRCodeHelper
143200
{
144-
public static string GetQRCode(string plainText, int pixelsPerModule, string darkColorHex, string lightColorHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute)
201+
public static string GetQRCode(string plainText, int pixelsPerModule, string darkColorHex, string lightColorHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
145202
{
146203
using (var qrGenerator = new QRCodeGenerator())
147204
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
148205
using (var qrCode = new SvgQRCode(qrCodeData))
149-
return qrCode.GetGraphic(pixelsPerModule, darkColorHex, lightColorHex, drawQuietZones, sizingMode);
206+
return qrCode.GetGraphic(pixelsPerModule, darkColorHex, lightColorHex, drawQuietZones, sizingMode, logo);
150207
}
151208
}
152209
}

0 commit comments

Comments
 (0)