11using System . Drawing ;
2- using System . Drawing . Imaging ;
32using System . Runtime . InteropServices ;
43using static QRCoder . Base64QRCode ;
54using static QRCoder . QRCodeGenerator ;
@@ -35,6 +34,17 @@ public Base64QRCode(QRCodeData data) : base(data)
3534 public string GetGraphic ( int pixelsPerModule )
3635 => GetGraphic ( pixelsPerModule , Color . Black , Color . White , true ) ;
3736
37+ /// <summary>
38+ /// Returns a base64-encoded string that contains the resulting QR code as a PNG image.
39+ /// </summary>
40+ /// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
41+ /// <param name="darkColorHtmlHex">The color of the dark modules in HTML hex format.</param>
42+ /// <param name="lightColorHtmlHex">The color of the light modules in HTML hex format.</param>
43+ /// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
44+ /// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
45+ public string GetGraphic ( int pixelsPerModule , string darkColorHtmlHex , string lightColorHtmlHex , bool drawQuietZones = true )
46+ => GetGraphic ( pixelsPerModule , ColorTranslator . FromHtml ( darkColorHtmlHex ) , ColorTranslator . FromHtml ( lightColorHtmlHex ) , drawQuietZones ) ;
47+
3848 /// <summary>
3949 /// Returns a base64-encoded string that contains the resulting QR code as an image.
4050 /// </summary>
@@ -44,7 +54,8 @@ public string GetGraphic(int pixelsPerModule)
4454 /// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
4555 /// <param name="imgType">The type of image to generate (PNG, JPEG, GIF).</param>
4656 /// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
47- public string GetGraphic ( int pixelsPerModule , string darkColorHtmlHex , string lightColorHtmlHex , bool drawQuietZones = true , ImageType imgType = ImageType . Png )
57+ [ Obsolete ( "The imgType parameter is obsolete. Only PNG format is supported. Use the overload without the imgType parameter." ) ]
58+ public string GetGraphic ( int pixelsPerModule , string darkColorHtmlHex , string lightColorHtmlHex , bool drawQuietZones , ImageType imgType )
4859 => GetGraphic ( pixelsPerModule , ColorTranslator . FromHtml ( darkColorHtmlHex ) , ColorTranslator . FromHtml ( lightColorHtmlHex ) , drawQuietZones , imgType ) ;
4960
5061 /// <summary>
@@ -56,121 +67,80 @@ public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string li
5667 /// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
5768 /// <param name="imgType">The type of image to generate (PNG, JPEG, GIF).</param>
5869 /// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
59- public string GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , bool drawQuietZones = true , ImageType imgType = ImageType . Png )
70+ [ Obsolete ( "The imgType parameter is obsolete. Only PNG format is supported. Use the overload without the imgType parameter." ) ]
71+ public string GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , bool drawQuietZones , ImageType imgType )
6072 {
61- if ( imgType = = ImageType . Png )
73+ if ( imgType ! = ImageType . Png )
6274 {
63- var pngCoder = new PngByteQRCode ( QrCodeData ) ;
64-
65- byte [ ] pngData ;
66- if ( darkColor == Color . Black && lightColor == Color . White )
67- {
68- pngData = pngCoder . GetGraphic ( pixelsPerModule , drawQuietZones ) ;
69- }
70- else
71- {
72- byte [ ] darkColorBytes ;
73- byte [ ] lightColorBytes ;
74- if ( darkColor . A != 255 || lightColor . A != 255 )
75- {
76- darkColorBytes = new byte [ ] { darkColor . R , darkColor . G , darkColor . B , darkColor . A } ;
77- lightColorBytes = new byte [ ] { lightColor . R , lightColor . G , lightColor . B , lightColor . A } ;
78- }
79- else
80- {
81- darkColorBytes = new byte [ ] { darkColor . R , darkColor . G , darkColor . B } ;
82- lightColorBytes = new byte [ ] { lightColor . R , lightColor . G , lightColor . B } ;
83- }
84- pngData = pngCoder . GetGraphic ( pixelsPerModule , darkColorBytes , lightColorBytes , drawQuietZones ) ;
85- }
86-
87- return Convert . ToBase64String ( pngData , Base64FormattingOptions . None ) ;
75+ throw new NotSupportedException ( $ "Only PNG format is supported. { imgType } format is no longer supported.") ;
8876 }
8977
90- #pragma warning disable CA1416 // Validate platform compatibility
91- var qr = new QRCode ( QrCodeData ) ;
92- var base64 = string . Empty ;
93- using ( var bmp = qr . GetGraphic ( pixelsPerModule , darkColor , lightColor , drawQuietZones ) )
94- {
95- base64 = BitmapToBase64 ( bmp , imgType ) ;
96- }
97- return base64 ;
98- #pragma warning restore CA1416 // Validate platform compatibility
78+ return GetGraphic ( pixelsPerModule , darkColor , lightColor , drawQuietZones ) ;
9979 }
10080
10181 /// <summary>
102- /// Returns a base64-encoded string that contains the resulting QR code as an image with an embedded icon .
82+ /// Returns a base64-encoded string that contains the resulting QR code as a PNG image .
10383 /// </summary>
10484 /// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
10585 /// <param name="darkColor">The color of the dark modules.</param>
10686 /// <param name="lightColor">The color of the light modules.</param>
107- /// <param name="icon">The icon to embed in the center of the QR code.</param>
108- /// <param name="iconSizePercent">The size of the icon as a percentage of the QR code.</param>
109- /// <param name="iconBorderWidth">The width of the border around the icon.</param>
11087 /// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
111- /// <param name="imgType">The type of image to generate (PNG, JPEG, GIF).</param>
11288 /// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
113- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
114- public string GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , Bitmap icon , int iconSizePercent = 15 , int iconBorderWidth = 6 , bool drawQuietZones = true , ImageType imgType = ImageType . Png )
89+ public string GetGraphic ( int pixelsPerModule , Color darkColor , Color lightColor , bool drawQuietZones = true )
11590 {
116- var qr = new QRCode ( QrCodeData ) ;
117- var base64 = string . Empty ;
118- using ( var bmp = qr . GetGraphic ( pixelsPerModule , darkColor , lightColor , icon , iconSizePercent , iconBorderWidth , drawQuietZones ) )
119- {
120- base64 = BitmapToBase64 ( bmp , imgType ) ;
121- }
122- return base64 ;
123- }
124-
125- /// <summary>
126- /// Converts a bitmap to a base64-encoded string.
127- /// </summary>
128- /// <param name="bmp">The bitmap to convert.</param>
129- /// <param name="imgType">The type of image (PNG, JPEG, GIF).</param>
130- /// <returns>Returns the base64-encoded string representation of the bitmap.</returns>
131- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
132- private static string BitmapToBase64 ( Bitmap bmp , ImageType imgType )
133- {
134- var iFormat = imgType switch
135- {
136- ImageType . Png => ImageFormat . Png ,
137- ImageType . Jpeg => ImageFormat . Jpeg ,
138- ImageType . Gif => ImageFormat . Gif ,
139- _ => ImageFormat . Png ,
140- } ;
141- using var memoryStream = new MemoryStream ( ) ;
142- bmp . Save ( memoryStream , iFormat ) ;
143- return Convert . ToBase64String ( memoryStream . ToArray ( ) , Base64FormattingOptions . None ) ;
91+ using var pngCoder = new PngByteQRCode ( QrCodeData ) ;
92+ var pngData = pngCoder . GetGraphic ( pixelsPerModule , darkColor , lightColor , drawQuietZones ) ;
93+ return Convert . ToBase64String ( pngData , Base64FormattingOptions . None ) ;
14494 }
14595
14696 /// <summary>
14797 /// Specifies the type of image to generate.
14898 /// </summary>
99+ [ Obsolete ( "ImageType enum is obsolete. Only PNG format is supported." ) ]
149100 public enum ImageType
150101 {
151102 /// <summary>
152103 /// Graphics Interchange Format (GIF) image format, a bitmap image format with limited color support
153104 /// </summary>
154- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
155105 Gif ,
156106 /// <summary>
157107 /// Joint Photographic Experts Group (JPEG) image format, a lossy compressed image format
158108 /// </summary>
159- [ System . Runtime . Versioning . SupportedOSPlatform ( "windows" ) ]
160109 Jpeg ,
161110 /// <summary>
162111 /// Portable Network Graphics (PNG) image format, a lossless raster graphics format
163112 /// </summary>
164113 Png
165114 }
166-
167115}
168116
169117/// <summary>
170118/// Provides static methods for creating base64-encoded QR codes.
171119/// </summary>
172120public static class Base64QRCodeHelper
173121{
122+ /// <summary>
123+ /// Creates a base64-encoded QR code as a one-shot operation.
124+ /// </summary>
125+ /// <param name="plainText">The text or payload to be encoded inside the QR code.</param>
126+ /// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
127+ /// <param name="darkColorHtmlHex">The color of the dark modules in HTML hex format.</param>
128+ /// <param name="lightColorHtmlHex">The color of the light modules in HTML hex format.</param>
129+ /// <param name="eccLevel">The level of error correction data.</param>
130+ /// <param name="forceUtf8">Specifies whether the generator should be forced to work in UTF-8 mode.</param>
131+ /// <param name="utf8BOM">Specifies whether the byte-order-mark should be used.</param>
132+ /// <param name="eciMode">Specifies which ECI mode should be used.</param>
133+ /// <param name="requestedVersion">Sets the fixed QR code target version.</param>
134+ /// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
135+ /// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
136+ 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 )
137+ {
138+ using var qrGenerator = new QRCodeGenerator ( ) ;
139+ using var qrCodeData = qrGenerator . CreateQrCode ( plainText , eccLevel , forceUtf8 , utf8BOM , eciMode , requestedVersion ) ;
140+ using var qrCode = new Base64QRCode ( qrCodeData ) ;
141+ return qrCode . GetGraphic ( pixelsPerModule , darkColorHtmlHex , lightColorHtmlHex , drawQuietZones ) ;
142+ }
143+
174144 /// <summary>
175145 /// Creates a base64-encoded QR code with a single function call.
176146 /// </summary>
@@ -186,7 +156,8 @@ public static class Base64QRCodeHelper
186156 /// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
187157 /// <param name="imgType">The type of image to generate (PNG, JPEG, GIF).</param>
188158 /// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
189- 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 )
159+ [ Obsolete ( "The imgType parameter is obsolete. Only PNG format is supported. Use the overload without the imgType parameter." ) ]
160+ public static string GetQRCode ( string plainText , int pixelsPerModule , string darkColorHtmlHex , string lightColorHtmlHex , ECCLevel eccLevel , bool forceUtf8 , bool utf8BOM , EciMode eciMode , int requestedVersion , bool drawQuietZones , ImageType imgType )
190161 {
191162 using var qrGenerator = new QRCodeGenerator ( ) ;
192163 using var qrCodeData = qrGenerator . CreateQrCode ( plainText , eccLevel , forceUtf8 , utf8BOM , eciMode , requestedVersion ) ;
0 commit comments