-
Notifications
You must be signed in to change notification settings - Fork 77
/
QrCodeBitmapExtensions.cs
215 lines (198 loc) · 9.77 KB
/
QrCodeBitmapExtensions.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// QR code generator library (.NET)
// https://github.com/manuelbl/QrCodeGenerator
//
// Copyright (c) 2021 Manuel Bleichenbacher
// Licensed under MIT License
// https://opensource.org/licenses/MIT
//
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Net.Codecrete.QrCodeGenerator
{
/// <summary>
/// <c>QrCode</c> extension for creating bitmaps using <c>System.Drawing</c> classes.
/// <para>
/// In .NET 6 and later versions, this extension will only work on Windows.
/// </para>
/// </summary>
public static class QrCodeBitmapExtensions
{
/// <inheritdoc cref="ToBitmap(QrCode, int, int)"/>
/// <param name="background">The background color.</param>
/// <param name="foreground">The foreground color.</param>
public static Bitmap ToBitmap(this QrCode qrCode, int scale, int border, Color foreground, Color background)
{
// check arguments
if (scale <= 0)
{
throw new ArgumentOutOfRangeException(nameof(scale), "Value out of range");
}
if (border < 0)
{
throw new ArgumentOutOfRangeException(nameof(border), "Value out of range");
}
int size = qrCode.Size;
int dim = (size + border * 2) * scale;
if (dim > short.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large");
}
// create bitmap
Bitmap bitmap = new Bitmap(dim, dim, PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
Draw(qrCode, g, scale, border, foreground, background);
}
return bitmap;
}
/// <summary>
/// Creates a bitmap (raster image) of this QR code.
/// <para>
/// The <paramref name="scale"/> parameter specifies the scale of the image, which is
/// equivalent to the width and height of each QR code module. Additionally, the number
/// of modules to add as a border to all four sides can be specified.
/// </para>
/// <para>
/// For example, <c>ToBitmap(scale: 10, border: 4)</c> means to pad the QR code with 4 white
/// border modules on all four sides, and use 10×10 pixels to represent each module.
/// </para>
/// <para>
/// The resulting bitmap uses the pixel format <see cref="PixelFormat.Format24bppRgb"/>.
/// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF).
/// </para>
/// </summary>
/// <param name="scale">The width and height, in pixels, of each module.</param>
/// <param name="border">The number of border modules to add to each of the four sides.</param>
/// <returns>The created bitmap representing this QR code.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="scale"/> is 0 or negative, <paramref name="border"/> is negative
/// or the resulting image is wider than 32,768 pixels.</exception>
public static Bitmap ToBitmap(this QrCode qrCode, int scale, int border)
{
return qrCode.ToBitmap(scale, border, Color.Black, Color.White);
}
/// <summary>
/// Draws this QR code into the specified graphics context.
/// <para>
/// The QR code is drawn at offset (0, 0). Use <see cref="Graphics.TranslateTransform(float, float)"/>
/// to draw it at a different position.
/// </para>
/// <para>
/// The <paramref name="scale"/> parameter specifies the scale of the image, which is
/// equivalent to the width and height of each QR code module. Additionally, the number
/// of modules to add as a border to all four sides can be specified.
/// </para>
/// <para>
/// For example, <c>Draw(graphics, scale: 10, border: 4)</c> means to pad the QR code with 4 white
/// border modules on all four sides, and use 10×10 pixels to represent each module.
/// </para>
/// <para>
/// </summary>
/// <param name="graphics">The graphics context to draw in.</param>
/// <param name="scale">The width and height, in pixels, of each module.</param>
/// <param name="border">The number of border modules to add to each of the four sides.</param>
public static void Draw(this QrCode qrCode, Graphics graphics, float scale, float border)
{
Draw(qrCode, graphics, scale, border, Color.Black, Color.White);
}
/// <inheritdoc cref="Draw(QrCode, Graphics, float, float)"/>
/// <param name="background">The background color.</param>
/// <param name="foreground">The foreground color.</param>
public static void Draw(this QrCode qrCode, Graphics graphics, float scale, float border, Color foreground, Color background)
{
if (scale <= 0 || border < 0)
{
return;
}
int size = qrCode.Size;
float dim = (size + border * 2) * scale;
// draw background
if (background != null)
{
using SolidBrush brush = new SolidBrush(background);
graphics.FillRectangle(brush, 0, 0, dim, dim);
}
// draw modules
using (SolidBrush brush = new SolidBrush(foreground))
{
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
if (qrCode.GetModule(x, y))
{
graphics.FillRectangle(brush, (x + border) * scale, (y + border) * scale, scale, scale);
}
}
}
}
}
/// <inheritdoc cref="ToPng(QrCode, int, int)"/>
/// <param name="background">The background color.</param>
/// <param name="foreground">The foreground color.</param>
public static byte[] ToPng(this QrCode qrCode, int scale, int border, Color foreground, Color background)
{
using Bitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background);
using MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
/// <summary>
/// Creates a PNG image of this QR code and returns it as a byte array.
/// <para>
/// The <paramref name="scale"/> parameter specifies the scale of the image, which is
/// equivalent to the width and height of each QR code module. Additionally, the number
/// of modules to add as a border to all four sides can be specified.
/// </para>
/// <para>
/// For example, <c>ToPng(scale: 10, border: 4)</c> means to pad the QR code with 4 white
/// border modules on all four sides, and use 10×10 pixels to represent each module.
/// </para>
/// <para>
/// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF).
/// </para>
/// </summary>
/// <param name="scale">The width and height, in pixels, of each module.</param>
/// <param name="border">The number of border modules to add to each of the four sides.</param>
/// <returns>The created bitmap representing this QR code.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="scale"/> is 0 or negative, <paramref name="border"/> is negative
/// or the resulting image is wider than 32,768 pixels.</exception>
public static byte[] ToPng(this QrCode qrCode, int scale, int border)
{
return qrCode.ToPng(scale, border, Color.Black, Color.White);
}
/// <inheritdoc cref="SaveAsPng(QrCode, string, int, int)"/>
/// <param name="background">The background color.</param>
/// <param name="foreground">The foreground color.</param>
public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border, Color foreground, Color background)
{
using Bitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background);
bitmap.Save(filename, ImageFormat.Png);
}
/// <summary>
/// Saves this QR code as a PNG file.
/// <para>
/// The <paramref name="scale"/> parameter specifies the scale of the image, which is
/// equivalent to the width and height of each QR code module. Additionally, the number
/// of modules to add as a border to all four sides can be specified.
/// </para>
/// <para>
/// For example, <c>SaveAsPng("qrcode.png", scale: 10, border: 4)</c> means to pad the QR code with 4 white
/// border modules on all four sides, and use 10×10 pixels to represent each module.
/// </para>
/// <para>
/// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF).
/// </para>
/// </summary>
/// <param name="scale">The width and height, in pixels, of each module.</param>
/// <param name="border">The number of border modules to add to each of the four sides.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="scale"/> is 0 or negative, <paramref name="border"/> is negative
/// or the resulting image is wider than 32,768 pixels.</exception>
public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border)
{
qrCode.SaveAsPng(filename, scale, border, Color.Black, Color.White);
}
}
}