-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathQrCodeControl.cs
145 lines (127 loc) · 3.78 KB
/
QrCodeControl.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
//
// 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.Windows.Forms;
namespace Net.Codecrete.QrCodeGenerator
{
/// <summary>
/// Custom control for displaying a QR code
/// </summary>
public class QrCodeControl : Control
{
private string _textData;
private byte[] _binaryData;
private int _errorCorrection;
private int _borderWidth;
public QrCodeControl()
{
_textData = "Test";
_errorCorrection = 2;
_borderWidth = 3;
ResizeRedraw = true;
}
public string TextData
{
get
{
if (_binaryData != null)
{
return "binary data";
}
return _textData;
}
set
{
_textData = value;
_binaryData = null;
if (_textData == null)
{
_textData = "";
}
Invalidate();
}
}
public byte[] BinaryData
{
get { return _binaryData; }
set
{
_binaryData = value;
_textData = null;
if (_binaryData == null)
{
_binaryData = new byte[0];
}
Invalidate();
}
}
public int ErrorCorrection
{
get { return _errorCorrection; }
set
{
_errorCorrection = Math.Min(Math.Max(value, 0), 3);
Invalidate();
}
}
public int BorderWidth
{
get { return _borderWidth; }
set
{
_borderWidth = Math.Max(value, 0);
Invalidate();
}
}
private static readonly QrCode.Ecc[] errorCorrectionLevels = { QrCode.Ecc.Low, QrCode.Ecc.Medium, QrCode.Ecc.Quartile, QrCode.Ecc.High };
/// <summary>
/// Creates the <c>QrCode</c> instance with the current settings.
/// </summary>
/// <returns></returns>
private QrCode CreateQrCode()
{
QrCode qrCode;
var ecc = errorCorrectionLevels[_errorCorrection];
if (_binaryData != null)
{
qrCode = QrCode.EncodeBinary(_binaryData, ecc);
}
else
{
qrCode = QrCode.EncodeText(_textData, ecc);
}
return qrCode;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
int size = Math.Min(Width, Height);
var graphics = e.Graphics;
var qrCode = CreateQrCode();
graphics.TranslateTransform((Width - size) / 2, (Height - size) / 2);
qrCode.Draw(graphics, scale: size / (float)(qrCode.Size + 2 * _borderWidth), border: _borderWidth,
foreground: Color.Black, background: Color.White);
}
/// <summary>
/// Copy the QR code to the clipboard.
/// <para>
/// The QR code is copied as a bitmap. It uses a scaling factor of 20 to
/// prevent a blurry result from upscaling.
/// </para>
/// </summary>
public void CopyToClipboard()
{
DataObject dataObject = new DataObject();
var qrCode = CreateQrCode();
dataObject.SetData(DataFormats.Bitmap, qrCode.ToBitmap(20, _borderWidth));
Clipboard.SetDataObject(dataObject);
}
}
}