1
- #if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
1
+ #if ! NETSTANDARD1_3
2
2
using System ;
3
3
using System . Drawing ;
4
4
using System . Drawing . Imaging ;
5
5
using System . IO ;
6
+ using System . Runtime . InteropServices ;
6
7
using static QRCoder . Base64QRCode ;
7
8
using static QRCoder . QRCodeGenerator ;
8
9
9
10
namespace QRCoder
10
11
{
11
- #if NET6_0_WINDOWS
12
- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
13
- #endif
14
12
public class Base64QRCode : AbstractQRCode , IDisposable
15
13
{
16
- private QRCode qr ;
17
-
18
14
/// <summary>
19
15
/// Constructor without params to be used in COM Objects connections
20
16
/// </summary>
21
17
public Base64QRCode ( ) {
22
- qr = new QRCode ( ) ;
23
18
}
24
19
25
20
public Base64QRCode ( QRCodeData data ) : base ( data ) {
26
- qr = new QRCode ( data ) ;
27
- }
28
-
29
- public override void SetQRCodeData ( QRCodeData data ) {
30
- this . qr . SetQRCodeData ( data ) ;
31
21
}
32
22
33
23
public string GetGraphic ( int pixelsPerModule )
@@ -43,25 +33,70 @@ public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string li
43
33
44
34
public string GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , bool drawQuietZones = true , ImageType imgType = ImageType . Png )
45
35
{
36
+ if ( imgType == ImageType . Png )
37
+ {
38
+ var pngCoder = new PngByteQRCode ( QrCodeData ) ;
39
+
40
+ byte [ ] pngData ;
41
+ if ( darkColor == Color . Black && lightColor == Color . White )
42
+ {
43
+ pngData = pngCoder . GetGraphic ( pixelsPerModule , drawQuietZones ) ;
44
+ }
45
+ else
46
+ {
47
+ byte [ ] darkColorBytes ;
48
+ byte [ ] lightColorBytes ;
49
+ if ( darkColor . A != 255 || lightColor . A != 255 )
50
+ {
51
+ darkColorBytes = new byte [ ] { darkColor . R , darkColor . G , darkColor . B , darkColor . A } ;
52
+ lightColorBytes = new byte [ ] { lightColor . R , lightColor . G , lightColor . B , lightColor . A } ;
53
+ }
54
+ else
55
+ {
56
+ darkColorBytes = new byte [ ] { darkColor . R , darkColor . G , darkColor . B } ;
57
+ lightColorBytes = new byte [ ] { lightColor . R , lightColor . G , lightColor . B } ;
58
+ }
59
+ pngData = pngCoder . GetGraphic ( pixelsPerModule , darkColorBytes , lightColorBytes , drawQuietZones ) ;
60
+ }
61
+
62
+ return Convert . ToBase64String ( pngData , Base64FormattingOptions . None ) ;
63
+ }
64
+
65
+ #if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
66
+ #pragma warning disable CA1416 // Validate platform compatibility
67
+ var qr = new QRCode ( QrCodeData ) ;
46
68
var base64 = string . Empty ;
47
69
using ( Bitmap bmp = qr . GetGraphic ( pixelsPerModule , darkColor , lightColor , drawQuietZones ) )
48
70
{
49
71
base64 = BitmapToBase64 ( bmp , imgType ) ;
50
72
}
51
73
return base64 ;
74
+ #pragma warning restore CA1416 // Validate platform compatibility
75
+ #else
76
+ throw new PlatformNotSupportedException ( "Only the PNG image type is supported on this platform." ) ;
77
+ #endif
52
78
}
53
79
80
+ #if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
81
+ #if NET6_0_OR_GREATER
82
+ [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
83
+ #endif
54
84
public string GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , Bitmap icon , int iconSizePercent = 15 , int iconBorderWidth = 6 , bool drawQuietZones = true , ImageType imgType = ImageType . Png )
55
85
{
86
+ var qr = new QRCode ( QrCodeData ) ;
56
87
var base64 = string . Empty ;
57
88
using ( Bitmap bmp = qr . GetGraphic ( pixelsPerModule , darkColor , lightColor , icon , iconSizePercent , iconBorderWidth , drawQuietZones ) )
58
89
{
59
90
base64 = BitmapToBase64 ( bmp , imgType ) ;
60
91
}
61
92
return base64 ;
62
93
}
94
+ #endif
63
95
64
-
96
+ #if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
97
+ #if NET6_0_OR_GREATER
98
+ [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
99
+ #endif
65
100
private string BitmapToBase64 ( Bitmap bmp , ImageType imgType )
66
101
{
67
102
var base64 = string . Empty ;
@@ -87,19 +122,23 @@ private string BitmapToBase64(Bitmap bmp, ImageType imgType)
87
122
}
88
123
return base64 ;
89
124
}
125
+ #endif
90
126
91
127
public enum ImageType
92
128
{
129
+ #if NET6_0_OR_GREATER
130
+ [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
131
+ #endif
93
132
Gif ,
133
+ #if NET6_0_OR_GREATER
134
+ [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
135
+ #endif
94
136
Jpeg ,
95
137
Png
96
138
}
97
139
98
140
}
99
141
100
- #if NET6_0_WINDOWS
101
- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
102
- #endif
103
142
public static class Base64QRCodeHelper
104
143
{
105
144
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 )
0 commit comments