@@ -21,33 +21,33 @@ public string GetGraphic(int pixelsPerModule)
21
21
var viewBox = new Size ( pixelsPerModule * this . QrCodeData . ModuleMatrix . Count , pixelsPerModule * this . QrCodeData . ModuleMatrix . Count ) ;
22
22
return this . GetGraphic ( viewBox , Color . Black , Color . White ) ;
23
23
}
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 )
25
25
{
26
26
var offset = drawQuietZones ? 0 : 4 ;
27
27
var edgeSize = this . QrCodeData . ModuleMatrix . Count * pixelsPerModule - ( offset * 2 * pixelsPerModule ) ;
28
28
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 ) ;
30
30
}
31
31
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 )
33
33
{
34
34
var offset = drawQuietZones ? 0 : 4 ;
35
35
var edgeSize = this . QrCodeData . ModuleMatrix . Count * pixelsPerModule - ( offset * 2 * pixelsPerModule ) ;
36
36
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 ) ;
38
38
}
39
39
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 )
41
41
{
42
- return this . GetGraphic ( viewBox , Color . Black , Color . White , drawQuietZones , sizingMode ) ;
42
+ return this . GetGraphic ( viewBox , Color . Black , Color . White , drawQuietZones , sizingMode , logo ) ;
43
43
}
44
44
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 )
46
46
{
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 ) ;
48
48
}
49
49
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 )
51
51
{
52
52
int offset = drawQuietZones ? 0 : 4 ;
53
53
int drawableModulesCount = this . QrCodeData . ModuleMatrix . Count - ( drawQuietZones ? 0 : offset * 2 ) ;
@@ -122,6 +122,15 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
122
122
}
123
123
}
124
124
}
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
+
125
134
svgFile.Append(@"</svg>");
126
135
return svgFile.ToString();
127
136
}
@@ -137,16 +146,64 @@ public enum SizingMode
137
146
WidthHeightAttribute,
138
147
ViewBoxAttribute
139
148
}
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
+ }
140
197
}
141
198
142
199
public static class SvgQRCodeHelper
143
200
{
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 )
145
202
{
146
203
using (var qrGenerator = new QRCodeGenerator())
147
204
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
148
205
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 );
150
207
}
151
208
}
152
209
}
0 commit comments