1- #if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
1+ #if ! NETSTANDARD1_3
22using System ;
33using System . Drawing ;
44using System . Drawing . Imaging ;
55using System . IO ;
6+ using System . Runtime . InteropServices ;
67using static QRCoder . Base64QRCode ;
78using static QRCoder . QRCodeGenerator ;
89
910namespace QRCoder
1011{
11- #if NET6_0_WINDOWS
12- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
13- #endif
1412 public class Base64QRCode : AbstractQRCode , IDisposable
1513 {
16- private QRCode qr ;
17-
1814 /// <summary>
1915 /// Constructor without params to be used in COM Objects connections
2016 /// </summary>
2117 public Base64QRCode ( ) {
22- qr = new QRCode ( ) ;
2318 }
2419
2520 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 ) ;
3121 }
3222
3323 public string GetGraphic ( int pixelsPerModule )
@@ -43,25 +33,70 @@ public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string li
4333
4434 public string GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , bool drawQuietZones = true , ImageType imgType = ImageType . Png )
4535 {
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 ) ;
4668 var base64 = string . Empty ;
4769 using ( Bitmap bmp = qr . GetGraphic ( pixelsPerModule , darkColor , lightColor , drawQuietZones ) )
4870 {
4971 base64 = BitmapToBase64 ( bmp , imgType ) ;
5072 }
5173 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
5278 }
5379
80+ #if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
81+ #if NET6_0_OR_GREATER
82+ [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
83+ #endif
5484 public string GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , Bitmap icon , int iconSizePercent = 15 , int iconBorderWidth = 6 , bool drawQuietZones = true , ImageType imgType = ImageType . Png )
5585 {
86+ var qr = new QRCode ( QrCodeData ) ;
5687 var base64 = string . Empty ;
5788 using ( Bitmap bmp = qr . GetGraphic ( pixelsPerModule , darkColor , lightColor , icon , iconSizePercent , iconBorderWidth , drawQuietZones ) )
5889 {
5990 base64 = BitmapToBase64 ( bmp , imgType ) ;
6091 }
6192 return base64 ;
6293 }
94+ #endif
6395
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
65100 private string BitmapToBase64 ( Bitmap bmp , ImageType imgType )
66101 {
67102 var base64 = string . Empty ;
@@ -87,19 +122,23 @@ private string BitmapToBase64(Bitmap bmp, ImageType imgType)
87122 }
88123 return base64 ;
89124 }
125+ #endif
90126
91127 public enum ImageType
92128 {
129+ #if NET6_0_OR_GREATER
130+ [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
131+ #endif
93132 Gif ,
133+ #if NET6_0_OR_GREATER
134+ [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
135+ #endif
94136 Jpeg ,
95137 Png
96138 }
97139
98140 }
99141
100- #if NET6_0_WINDOWS
101- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
102- #endif
103142 public static class Base64QRCodeHelper
104143 {
105144 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