-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Base64QRCode.cs
154 lines (139 loc) · 5.78 KB
/
Base64QRCode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#if !NETSTANDARD1_3
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using static QRCoder.Base64QRCode;
using static QRCoder.QRCodeGenerator;
namespace QRCoder
{
public class Base64QRCode : AbstractQRCode, IDisposable
{
/// <summary>
/// Constructor without params to be used in COM Objects connections
/// </summary>
public Base64QRCode() {
}
public Base64QRCode(QRCodeData data) : base(data) {
}
public string GetGraphic(int pixelsPerModule)
{
return this.GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
}
public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
{
return this.GetGraphic(pixelsPerModule, ColorTranslator.FromHtml(darkColorHtmlHex), ColorTranslator.FromHtml(lightColorHtmlHex), drawQuietZones, imgType);
}
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
{
if (imgType == ImageType.Png)
{
var pngCoder = new PngByteQRCode(QrCodeData);
byte[] pngData;
if (darkColor == Color.Black && lightColor == Color.White)
{
pngData = pngCoder.GetGraphic(pixelsPerModule, drawQuietZones);
}
else
{
byte[] darkColorBytes;
byte[] lightColorBytes;
if (darkColor.A != 255 || lightColor.A != 255)
{
darkColorBytes = new byte[] { darkColor.R, darkColor.G, darkColor.B, darkColor.A };
lightColorBytes = new byte[] { lightColor.R, lightColor.G, lightColor.B, lightColor.A };
}
else
{
darkColorBytes = new byte[] { darkColor.R, darkColor.G, darkColor.B };
lightColorBytes = new byte[] { lightColor.R, lightColor.G, lightColor.B };
}
pngData = pngCoder.GetGraphic(pixelsPerModule, darkColorBytes, lightColorBytes, drawQuietZones);
}
return Convert.ToBase64String(pngData, Base64FormattingOptions.None);
}
#if SYSTEM_DRAWING
#pragma warning disable CA1416 // Validate platform compatibility
var qr = new QRCode(QrCodeData);
var base64 = string.Empty;
using (Bitmap bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, drawQuietZones))
{
base64 = BitmapToBase64(bmp, imgType);
}
return base64;
#pragma warning restore CA1416 // Validate platform compatibility
#else
throw new PlatformNotSupportedException("Only the PNG image type is supported on this platform.");
#endif
}
#if SYSTEM_DRAWING
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
{
var qr = new QRCode(QrCodeData);
var base64 = string.Empty;
using (Bitmap bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, icon, iconSizePercent, iconBorderWidth, drawQuietZones))
{
base64 = BitmapToBase64(bmp, imgType);
}
return base64;
}
#endif
#if SYSTEM_DRAWING
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
private string BitmapToBase64(Bitmap bmp, ImageType imgType)
{
var base64 = string.Empty;
ImageFormat iFormat;
switch (imgType) {
case ImageType.Png:
iFormat = ImageFormat.Png;
break;
case ImageType.Jpeg:
iFormat = ImageFormat.Jpeg;
break;
case ImageType.Gif:
iFormat = ImageFormat.Gif;
break;
default:
iFormat = ImageFormat.Png;
break;
}
using (MemoryStream memoryStream = new MemoryStream())
{
bmp.Save(memoryStream, iFormat);
base64 = Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
}
return base64;
}
#endif
public enum ImageType
{
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
Gif,
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
Jpeg,
Png
}
}
public static class Base64QRCodeHelper
{
public static string GetQRCode(string plainText, int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
{
using (var qrGenerator = new QRCodeGenerator())
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
using (var qrCode = new Base64QRCode(qrCodeData))
return qrCode.GetGraphic(pixelsPerModule, darkColorHtmlHex, lightColorHtmlHex, drawQuietZones, imgType);
}
}
}
#endif