diff --git a/QRCoder/Extensions/StringValueAttribute.cs b/QRCoder/Extensions/StringValueAttribute.cs
new file mode 100644
index 00000000..cc6e1bf4
--- /dev/null
+++ b/QRCoder/Extensions/StringValueAttribute.cs
@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+
+namespace QRCoder.Extensions
+{
+ ///
+ /// Used to represent a string value for a value in an enum
+ ///
+ public class StringValueAttribute : Attribute
+ {
+
+ #region Properties
+
+ ///
+ /// Holds the alue in an enum
+ ///
+ public string StringValue { get; protected set; }
+
+ #endregion
+
+ ///
+ /// Init a StringValue Attribute
+ ///
+ ///
+ public StringValueAttribute(string value)
+ {
+ this.StringValue = value;
+ }
+ }
+
+ public static class CustomExtensions
+ {
+ ///
+ /// Will get the string value for a given enum's value
+ ///
+ ///
+ ///
+ public static string GetStringValue(this Enum value)
+ {
+#if NETSTANDARD1_3
+ var fieldInfo = value.GetType().GetRuntimeField(value.ToString());
+#else
+ var fieldInfo = value.GetType().GetField(value.ToString());
+#endif
+ var attr = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
+ return attr.Length > 0 ? attr[0].StringValue : null;
+ }
+ }
+}
diff --git a/QRCoder/SvgQRCode.cs b/QRCoder/SvgQRCode.cs
index f0d01909..3dc0c097 100644
--- a/QRCoder/SvgQRCode.cs
+++ b/QRCoder/SvgQRCode.cs
@@ -1,8 +1,11 @@
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0
+using QRCoder.Extensions;
using System;
using System.Collections;
+using System.Collections.Generic;
using System.Drawing;
using System.Text;
+using System.Text.RegularExpressions;
using static QRCoder.QRCodeGenerator;
using static QRCoder.SvgQRCode;
@@ -16,11 +19,27 @@ public class SvgQRCode : AbstractQRCode, IDisposable
public SvgQRCode() { }
public SvgQRCode(QRCodeData data) : base(data) { }
+ ///
+ /// Returns a QR code as SVG string
+ ///
+ /// The pixel size each b/w module is drawn
+ /// SVG as string
public string GetGraphic(int pixelsPerModule)
{
var viewBox = new Size(pixelsPerModule*this.QrCodeData.ModuleMatrix.Count, pixelsPerModule * this.QrCodeData.ModuleMatrix.Count);
return this.GetGraphic(viewBox, Color.Black, Color.White);
}
+
+ ///
+ /// Returns a QR code as SVG string with custom colors, optional quietzone and logo
+ ///
+ /// The pixel size each b/w module is drawn
+ /// Color of the dark modules
+ /// Color of the light modules
+ /// If true a white border is drawn around the whole QR Code
+ /// Defines if width/height or viewbox should be used for size definition
+ /// A (optional) logo to be rendered on the code (either Bitmap or SVG)
+ /// SVG as string
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
{
var offset = drawQuietZones ? 0 : 4;
@@ -29,6 +48,16 @@ public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
return this.GetGraphic(viewBox, darkColor, lightColor, drawQuietZones, sizingMode, logo);
}
+ ///
+ /// Returns a QR code as SVG string with custom colors (in HEX syntax), optional quietzone and logo
+ ///
+ /// The pixel size each b/w module is drawn
+ /// The color of the dark/black modules in hex (e.g. #000000) representation
+ /// The color of the light/white modules in hex (e.g. #ffffff) representation
+ /// If true a white border is drawn around the whole QR Code
+ /// Defines if width/height or viewbox should be used for size definition
+ /// A (optional) logo to be rendered on the code (either Bitmap or SVG)
+ /// SVG as string
public string GetGraphic(int pixelsPerModule, string darkColorHex, string lightColorHex, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
{
var offset = drawQuietZones ? 0 : 4;
@@ -37,16 +66,44 @@ public string GetGraphic(int pixelsPerModule, string darkColorHex, string lightC
return this.GetGraphic(viewBox, darkColorHex, lightColorHex, drawQuietZones, sizingMode, logo);
}
+ ///
+ /// Returns a QR code as SVG string with optional quietzone and logo
+ ///
+ /// The viewbox of the QR code graphic
+ /// If true a white border is drawn around the whole QR Code
+ /// Defines if width/height or viewbox should be used for size definition
+ /// A (optional) logo to be rendered on the code (either Bitmap or SVG)
+ /// SVG as string
public string GetGraphic(Size viewBox, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
{
return this.GetGraphic(viewBox, Color.Black, Color.White, drawQuietZones, sizingMode, logo);
}
+ ///
+ /// Returns a QR code as SVG string with custom colors and optional quietzone and logo
+ ///
+ /// The viewbox of the QR code graphic
+ /// Color of the dark modules
+ /// Color of the light modules
+ /// If true a white border is drawn around the whole QR Code
+ /// Defines if width/height or viewbox should be used for size definition
+ /// A (optional) logo to be rendered on the code (either Bitmap or SVG)
+ /// SVG as string
public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
{
return this.GetGraphic(viewBox, ColorTranslator.ToHtml(Color.FromArgb(darkColor.ToArgb())), ColorTranslator.ToHtml(Color.FromArgb(lightColor.ToArgb())), drawQuietZones, sizingMode, logo);
}
+ ///
+ /// Returns a QR code as SVG string with custom colors (in HEX syntax), optional quietzone and logo
+ ///
+ /// The viewbox of the QR code graphic
+ /// The color of the dark/black modules in hex (e.g. #000000) representation
+ /// The color of the light/white modules in hex (e.g. #ffffff) representation
+ /// If true a white border is drawn around the whole QR Code
+ /// Defines if width/height or viewbox should be used for size definition
+ /// A (optional) logo to be rendered on the code (either Bitmap or SVG)
+ /// SVG as string
public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute, SvgLogo logo = null)
{
int offset = drawQuietZones ? 0 : 4;
@@ -54,6 +111,9 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
double pixelsPerModule = Math.Min(viewBox.Width, viewBox.Height) / (double)drawableModulesCount;
double qrSize = drawableModulesCount * pixelsPerModule;
string svgSizeAttributes = (sizingMode == SizingMode.WidthHeightAttribute) ? $@"width=""{viewBox.Width}"" height=""{viewBox.Height}""" : $@"viewBox=""0 0 {viewBox.Width} {viewBox.Height}""";
+ ImageAttributes? logoAttr = null;
+ if (logo != null)
+ logoAttr = GetLogoAttributes(logo, viewBox);
// Merge horizontal rectangles
int[,] matrix = new int[drawableModulesCount, drawableModulesCount];
@@ -66,7 +126,7 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
for (int xi = 0; xi < drawableModulesCount; xi += 1)
{
matrix[yi, xi] = 0;
- if (bitArray[xi+offset])
+ if (bitArray[xi+offset] && (logo == null || !logo.FillLogoBackground() || !IsBlockedByLogo((xi+offset)*pixelsPerModule, (yi+offset) * pixelsPerModule, logoAttr, pixelsPerModule)))
{
if(x0 == -1)
{
@@ -91,7 +151,7 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
}
}
- StringBuilder svgFile = new StringBuilder($@"