|
1 | 1 | using System;
|
2 |
| -using System.Collections.Generic; |
3 |
| -using System.ComponentModel; |
4 |
| -using System.Data; |
5 | 2 | using System.Drawing;
|
6 |
| -using System.Linq; |
7 |
| -using System.Text; |
| 3 | +using System.Drawing.Imaging; |
8 | 4 | using System.Windows.Forms;
|
9 | 5 | using QRCoder;
|
10 |
| -using System.Drawing.Imaging; |
11 |
| -using System.IO; |
12 | 6 |
|
13 |
| -namespace QRCoderDemo |
| 7 | +namespace QRCoderDemo; |
| 8 | + |
| 9 | +public partial class Form1 : Form |
14 | 10 | {
|
15 |
| - public partial class Form1 : Form |
| 11 | + public Form1() |
16 | 12 | {
|
17 |
| - public Form1() |
18 |
| - { |
19 |
| - InitializeComponent(); |
20 |
| - } |
| 13 | + InitializeComponent(); |
| 14 | + } |
| 15 | + |
| 16 | + private void Form1_Load(object sender, EventArgs e) |
| 17 | + { |
| 18 | + comboBoxECC.SelectedIndex = 0; //Pre-select ECC level "L" |
| 19 | + RenderQrCode(); |
| 20 | + } |
21 | 21 |
|
22 |
| - private void Form1_Load(object sender, EventArgs e) |
| 22 | + private void buttonGenerate_Click(object sender, EventArgs e) |
| 23 | + { |
| 24 | + RenderQrCode(); |
| 25 | + } |
| 26 | + |
| 27 | + private void RenderQrCode() |
| 28 | + { |
| 29 | + string level = comboBoxECC.SelectedItem.ToString(); |
| 30 | + var eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3); |
| 31 | + using var qrGenerator = new QRCodeGenerator(); |
| 32 | + using var qrCodeData = qrGenerator.CreateQrCode(textBoxQRCode.Text, eccLevel); |
| 33 | + using var qrCode = new QRCode(qrCodeData); |
| 34 | + pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, GetPrimaryColor(), GetBackgroundColor(), |
| 35 | + GetIconBitmap(), (int)iconSize.Value); |
| 36 | + |
| 37 | + pictureBoxQRCode.Size = new System.Drawing.Size(pictureBoxQRCode.Width, pictureBoxQRCode.Height); |
| 38 | + //Set the SizeMode to center the image. |
| 39 | + pictureBoxQRCode.SizeMode = PictureBoxSizeMode.CenterImage; |
| 40 | + |
| 41 | + pictureBoxQRCode.SizeMode = PictureBoxSizeMode.StretchImage; |
| 42 | + } |
| 43 | + |
| 44 | + private Bitmap GetIconBitmap() |
| 45 | + { |
| 46 | + if (iconPath.Text.Length == 0) |
23 | 47 | {
|
24 |
| - comboBoxECC.SelectedIndex = 0; //Pre-select ECC level "L" |
25 |
| - RenderQrCode(); |
| 48 | + return null; |
26 | 49 | }
|
27 |
| - |
28 |
| - private void buttonGenerate_Click(object sender, EventArgs e) |
| 50 | + try |
29 | 51 | {
|
30 |
| - RenderQrCode(); |
| 52 | + return new Bitmap(iconPath.Text); |
31 | 53 | }
|
32 |
| - |
33 |
| - private void RenderQrCode() |
| 54 | + catch (Exception) |
34 | 55 | {
|
35 |
| - string level = comboBoxECC.SelectedItem.ToString(); |
36 |
| - var eccLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3); |
37 |
| - using var qrGenerator = new QRCodeGenerator(); |
38 |
| - using var qrCodeData = qrGenerator.CreateQrCode(textBoxQRCode.Text, eccLevel); |
39 |
| - using var qrCode = new QRCode(qrCodeData); |
40 |
| - pictureBoxQRCode.BackgroundImage = qrCode.GetGraphic(20, GetPrimaryColor(), GetBackgroundColor(), |
41 |
| - GetIconBitmap(), (int)iconSize.Value); |
42 |
| - |
43 |
| - pictureBoxQRCode.Size = new System.Drawing.Size(pictureBoxQRCode.Width, pictureBoxQRCode.Height); |
44 |
| - //Set the SizeMode to center the image. |
45 |
| - pictureBoxQRCode.SizeMode = PictureBoxSizeMode.CenterImage; |
46 |
| - |
47 |
| - pictureBoxQRCode.SizeMode = PictureBoxSizeMode.StretchImage; |
| 56 | + return null; |
48 | 57 | }
|
| 58 | + } |
49 | 59 |
|
50 |
| - private Bitmap GetIconBitmap() |
| 60 | + private void selectIconBtn_Click(object sender, EventArgs e) |
| 61 | + { |
| 62 | + var openFileDlg = new OpenFileDialog(); |
| 63 | + openFileDlg.Title = "Select icon"; |
| 64 | + openFileDlg.Multiselect = false; |
| 65 | + openFileDlg.CheckFileExists = true; |
| 66 | + if (openFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) |
51 | 67 | {
|
52 |
| - if (iconPath.Text.Length == 0) |
53 |
| - { |
54 |
| - return null; |
55 |
| - } |
56 |
| - try |
57 |
| - { |
58 |
| - return new Bitmap(iconPath.Text); |
59 |
| - } |
60 |
| - catch (Exception) |
| 68 | + iconPath.Text = openFileDlg.FileName; |
| 69 | + if (iconSize.Value == 0) |
61 | 70 | {
|
62 |
| - return null; |
| 71 | + iconSize.Value = 15; |
63 | 72 | }
|
64 | 73 | }
|
65 |
| - |
66 |
| - private void selectIconBtn_Click(object sender, EventArgs e) |
| 74 | + else |
67 | 75 | {
|
68 |
| - var openFileDlg = new OpenFileDialog(); |
69 |
| - openFileDlg.Title = "Select icon"; |
70 |
| - openFileDlg.Multiselect = false; |
71 |
| - openFileDlg.CheckFileExists = true; |
72 |
| - if (openFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) |
73 |
| - { |
74 |
| - iconPath.Text = openFileDlg.FileName; |
75 |
| - if (iconSize.Value == 0) |
76 |
| - { |
77 |
| - iconSize.Value = 15; |
78 |
| - } |
79 |
| - } |
80 |
| - else |
81 |
| - { |
82 |
| - iconPath.Text = ""; |
83 |
| - } |
| 76 | + iconPath.Text = ""; |
84 | 77 | }
|
| 78 | + } |
85 | 79 |
|
86 | 80 |
|
87 |
| - private void btn_save_Click(object sender, EventArgs e) |
88 |
| - { |
| 81 | + private void btn_save_Click(object sender, EventArgs e) |
| 82 | + { |
89 | 83 |
|
90 |
| - // Displays a SaveFileDialog so the user can save the Image |
91 |
| - var saveFileDialog1 = new SaveFileDialog(); |
92 |
| - saveFileDialog1.Filter = "Bitmap Image|*.bmp|PNG Image|*.png|JPeg Image|*.jpg|Gif Image|*.gif"; |
93 |
| - saveFileDialog1.Title = "Save an Image File"; |
94 |
| - saveFileDialog1.ShowDialog(); |
| 84 | + // Displays a SaveFileDialog so the user can save the Image |
| 85 | + var saveFileDialog1 = new SaveFileDialog(); |
| 86 | + saveFileDialog1.Filter = "Bitmap Image|*.bmp|PNG Image|*.png|JPeg Image|*.jpg|Gif Image|*.gif"; |
| 87 | + saveFileDialog1.Title = "Save an Image File"; |
| 88 | + saveFileDialog1.ShowDialog(); |
95 | 89 |
|
96 |
| - // If the file name is not an empty string open it for saving. |
97 |
| - if (saveFileDialog1.FileName != "") |
| 90 | + // If the file name is not an empty string open it for saving. |
| 91 | + if (saveFileDialog1.FileName != "") |
| 92 | + { |
| 93 | + // Saves the Image via a FileStream created by the OpenFile method. |
| 94 | + using var fs = (System.IO.FileStream)saveFileDialog1.OpenFile(); |
| 95 | + // Saves the Image in the appropriate ImageFormat based upon the |
| 96 | + // File type selected in the dialog box. |
| 97 | + // NOTE that the FilterIndex property is one-based. |
| 98 | + |
| 99 | + ImageFormat imageFormat = null; |
| 100 | + imageFormat = saveFileDialog1.FilterIndex switch |
98 | 101 | {
|
99 |
| - // Saves the Image via a FileStream created by the OpenFile method. |
100 |
| - using var fs = (System.IO.FileStream)saveFileDialog1.OpenFile(); |
101 |
| - // Saves the Image in the appropriate ImageFormat based upon the |
102 |
| - // File type selected in the dialog box. |
103 |
| - // NOTE that the FilterIndex property is one-based. |
104 |
| - |
105 |
| - ImageFormat imageFormat = null; |
106 |
| - imageFormat = saveFileDialog1.FilterIndex switch |
107 |
| - { |
108 |
| - 1 => ImageFormat.Bmp, |
109 |
| - 2 => ImageFormat.Png, |
110 |
| - 3 => ImageFormat.Jpeg, |
111 |
| - 4 => ImageFormat.Gif, |
112 |
| - _ => throw new NotSupportedException("File extension is not supported"), |
113 |
| - }; |
114 |
| - pictureBoxQRCode.BackgroundImage.Save(fs, imageFormat); |
115 |
| - } |
| 102 | + 1 => ImageFormat.Bmp, |
| 103 | + 2 => ImageFormat.Png, |
| 104 | + 3 => ImageFormat.Jpeg, |
| 105 | + 4 => ImageFormat.Gif, |
| 106 | + _ => throw new NotSupportedException("File extension is not supported"), |
| 107 | + }; |
| 108 | + pictureBoxQRCode.BackgroundImage.Save(fs, imageFormat); |
116 | 109 | }
|
| 110 | + } |
117 | 111 |
|
118 |
| - public void ExportToBmp(string path) |
119 |
| - { |
| 112 | + public void ExportToBmp(string path) |
| 113 | + { |
120 | 114 |
|
121 |
| - } |
| 115 | + } |
122 | 116 |
|
123 |
| - private void textBoxQRCode_TextChanged(object sender, EventArgs e) |
124 |
| - { |
125 |
| - RenderQrCode(); |
126 |
| - } |
| 117 | + private void textBoxQRCode_TextChanged(object sender, EventArgs e) |
| 118 | + { |
| 119 | + RenderQrCode(); |
| 120 | + } |
127 | 121 |
|
128 |
| - private void comboBoxECC_SelectedIndexChanged(object sender, EventArgs e) |
129 |
| - { |
130 |
| - RenderQrCode(); |
131 |
| - } |
| 122 | + private void comboBoxECC_SelectedIndexChanged(object sender, EventArgs e) |
| 123 | + { |
| 124 | + RenderQrCode(); |
| 125 | + } |
132 | 126 |
|
133 |
| - private void panelPreviewPrimaryColor_Click(object sender, EventArgs e) |
| 127 | + private void panelPreviewPrimaryColor_Click(object sender, EventArgs e) |
| 128 | + { |
| 129 | + if (colorDialogPrimaryColor.ShowDialog() == DialogResult.OK) |
134 | 130 | {
|
135 |
| - if (colorDialogPrimaryColor.ShowDialog() == DialogResult.OK) |
136 |
| - { |
137 |
| - panelPreviewPrimaryColor.BackColor = colorDialogPrimaryColor.Color; |
138 |
| - RenderQrCode(); |
139 |
| - } |
| 131 | + panelPreviewPrimaryColor.BackColor = colorDialogPrimaryColor.Color; |
| 132 | + RenderQrCode(); |
140 | 133 | }
|
| 134 | + } |
141 | 135 |
|
142 |
| - private void panelPreviewBackgroundColor_Click(object sender, EventArgs e) |
| 136 | + private void panelPreviewBackgroundColor_Click(object sender, EventArgs e) |
| 137 | + { |
| 138 | + if (colorDialogBackgroundColor.ShowDialog() == DialogResult.OK) |
143 | 139 | {
|
144 |
| - if (colorDialogBackgroundColor.ShowDialog() == DialogResult.OK) |
145 |
| - { |
146 |
| - panelPreviewBackgroundColor.BackColor = colorDialogBackgroundColor.Color; |
147 |
| - RenderQrCode(); |
148 |
| - } |
| 140 | + panelPreviewBackgroundColor.BackColor = colorDialogBackgroundColor.Color; |
| 141 | + RenderQrCode(); |
149 | 142 | }
|
| 143 | + } |
150 | 144 |
|
151 |
| - private Color GetPrimaryColor() |
152 |
| - { |
153 |
| - return panelPreviewPrimaryColor.BackColor; |
154 |
| - } |
| 145 | + private Color GetPrimaryColor() |
| 146 | + { |
| 147 | + return panelPreviewPrimaryColor.BackColor; |
| 148 | + } |
155 | 149 |
|
156 |
| - private Color GetBackgroundColor() |
157 |
| - { |
158 |
| - return panelPreviewBackgroundColor.BackColor; |
159 |
| - } |
| 150 | + private Color GetBackgroundColor() |
| 151 | + { |
| 152 | + return panelPreviewBackgroundColor.BackColor; |
160 | 153 | }
|
161 | 154 | }
|
0 commit comments