Skip to content

Commit f490971

Browse files
committed
File-scoped namespace
1 parent 3c6e7e5 commit f490971

File tree

2 files changed

+114
-120
lines changed

2 files changed

+114
-120
lines changed

Directory.Build.props

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<PropertyGroup>
33
<LangVersion>10</LangVersion>
44
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
5+
<WarningsNotAsErrors>$(WarningsNotAsErrors);IDE0005</WarningsNotAsErrors>
56
</PropertyGroup>
67
</Project>

QRCoderDemo/Form1.cs

+113-120
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,154 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using System.Data;
52
using System.Drawing;
6-
using System.Linq;
7-
using System.Text;
3+
using System.Drawing.Imaging;
84
using System.Windows.Forms;
95
using QRCoder;
10-
using System.Drawing.Imaging;
11-
using System.IO;
126

13-
namespace QRCoderDemo
7+
namespace QRCoderDemo;
8+
9+
public partial class Form1 : Form
1410
{
15-
public partial class Form1 : Form
11+
public Form1()
1612
{
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+
}
2121

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)
2347
{
24-
comboBoxECC.SelectedIndex = 0; //Pre-select ECC level "L"
25-
RenderQrCode();
48+
return null;
2649
}
27-
28-
private void buttonGenerate_Click(object sender, EventArgs e)
50+
try
2951
{
30-
RenderQrCode();
52+
return new Bitmap(iconPath.Text);
3153
}
32-
33-
private void RenderQrCode()
54+
catch (Exception)
3455
{
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;
4857
}
58+
}
4959

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)
5167
{
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)
6170
{
62-
return null;
71+
iconSize.Value = 15;
6372
}
6473
}
65-
66-
private void selectIconBtn_Click(object sender, EventArgs e)
74+
else
6775
{
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 = "";
8477
}
78+
}
8579

8680

87-
private void btn_save_Click(object sender, EventArgs e)
88-
{
81+
private void btn_save_Click(object sender, EventArgs e)
82+
{
8983

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();
9589

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
98101
{
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);
116109
}
110+
}
117111

118-
public void ExportToBmp(string path)
119-
{
112+
public void ExportToBmp(string path)
113+
{
120114

121-
}
115+
}
122116

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+
}
127121

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+
}
132126

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)
134130
{
135-
if (colorDialogPrimaryColor.ShowDialog() == DialogResult.OK)
136-
{
137-
panelPreviewPrimaryColor.BackColor = colorDialogPrimaryColor.Color;
138-
RenderQrCode();
139-
}
131+
panelPreviewPrimaryColor.BackColor = colorDialogPrimaryColor.Color;
132+
RenderQrCode();
140133
}
134+
}
141135

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)
143139
{
144-
if (colorDialogBackgroundColor.ShowDialog() == DialogResult.OK)
145-
{
146-
panelPreviewBackgroundColor.BackColor = colorDialogBackgroundColor.Color;
147-
RenderQrCode();
148-
}
140+
panelPreviewBackgroundColor.BackColor = colorDialogBackgroundColor.Color;
141+
RenderQrCode();
149142
}
143+
}
150144

151-
private Color GetPrimaryColor()
152-
{
153-
return panelPreviewPrimaryColor.BackColor;
154-
}
145+
private Color GetPrimaryColor()
146+
{
147+
return panelPreviewPrimaryColor.BackColor;
148+
}
155149

156-
private Color GetBackgroundColor()
157-
{
158-
return panelPreviewBackgroundColor.BackColor;
159-
}
150+
private Color GetBackgroundColor()
151+
{
152+
return panelPreviewBackgroundColor.BackColor;
160153
}
161154
}

0 commit comments

Comments
 (0)