-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
369 lines (314 loc) · 14.3 KB
/
Form1.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using Newtonsoft.Json;
using QRCoder;
using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Net;
using System.Reflection;
using System.Windows.Forms;
using ZXing;
using ZXing.QrCode;
using ZXing.Windows.Compatibility;
using static QRCoder.PayloadGenerator;
namespace QRCodeMaker
{
public partial class Form1 : Form
{
#pragma warning disable CS8601 // Possible null reference assignment.
private readonly Version _version = Assembly.GetExecutingAssembly().GetName().Version;
#pragma warning restore CS8601 // Possible null reference assignment.
private string currentVersion;
private string repoOwner = "PackJC"; // Replace with your GitHub username
private string repoName = "QRCodeMaker"; // Replace with your repository name
#pragma warning disable CS0169 // The field 'Form1.logo' is never used
private string logo;
#pragma warning restore CS0169 // The field 'Form1.logo' is never used
Color qrCodeColor = Color.Black; // Default color
Color qrCodeBackgroundColor = Color.White; // Default color
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public Form1()
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
InitializeComponent();
#pragma warning disable CS8602 // Dereference of a possibly null reference.
currentVersion = _version.ToString();
#pragma warning restore CS8602 // Dereference of a possibly null reference.
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_Enter(object sender, EventArgs e)
{
if (stringBox.Text == "Enter your text to be put into QR Code")
{
stringBox.Text = "";
stringBox.ForeColor = Color.Black;
}
}
private void button1_Click(object sender, EventArgs e)
{
string inputString = stringBox.Text;
// Assuming qrCodeColor is your user-selected color for the QR code and is defined elsewhere
try
{
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
{
QRCodeData qrCodeData = qrGenerator.CreateQrCode(inputString, QRCodeGenerator.ECCLevel.Q);
using (QRCode qrCode = new QRCode(qrCodeData))
{
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
Bitmap qrCodeImage = null;
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
// Check if a logo has been selected and loaded into pictureBoxLogo
if (logoPictureBox.Image != null)
{
// Assuming pictureBoxLogo contains the logo
Bitmap logoImage = new Bitmap(logoPictureBox.Image);
// Generate QR code with logo. Adjust logo size ratio as needed.
qrCodeImage = qrCode.GetGraphic(20, qrCodeColor, qrCodeBackgroundColor, logoImage, 15);
}
else
{
// Generate QR code without a logo, but with custom color
qrCodeImage = qrCode.GetGraphic(20, qrCodeColor, qrCodeBackgroundColor, true);
}
// Ensure previous QR code image is disposed
if (qrCodeBox.Image != null)
{
qrCodeBox.Image.Dispose(); // Dispose directly as we're going to replace it immediately
}
qrCodeBox.Image = qrCodeImage; // Assign the newly created QR code image
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error generating QR code: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public string CleanFileName(string fileName)
{
// Remove http:// or https:// from the string
string cleanedFileName = fileName.Replace("http://", "").Replace("https://", "").Replace(".com", "").Replace(".net", "");
// Replace invalid filename characters with underscores
char[] invalidChars = System.IO.Path.GetInvalidFileNameChars();
foreach (char c in invalidChars)
{
cleanedFileName = cleanedFileName.Replace(c, '_');
}
// Replace multiple consecutive underscores with a single underscore
while (cleanedFileName.Contains("__")) // Two underscores
{
cleanedFileName = cleanedFileName.Replace("__", "_");
}
return cleanedFileName;
}
private void downloadButton_Click(object sender, EventArgs e)
{
// Check if there's an image in the PictureBox
if (qrCodeBox.Image == null)
{
MessageBox.Show("There is no QR code to save.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
// Configure save file dialog box
saveFileDialog.FileName = "QRCode_" + CleanFileName(stringBox.Text);
saveFileDialog.DefaultExt = ".png"; // Default file extension
saveFileDialog.Filter = "PNG files (*.png)|*.png|All files (*.*)|*.*"; // Filter files by extension
// Show save file dialog box
DialogResult result = saveFileDialog.ShowDialog();
// Process save file dialog box results
if (result == DialogResult.OK)
{
// Save Image
string filename = saveFileDialog.FileName;
qrCodeBox.Image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
MessageBox.Show($"QR Code saved to {filename}", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void copyButton_Click(object sender, EventArgs e)
{
// Check if there's an image in the PictureBox
if (qrCodeBox.Image == null)
{
MessageBox.Show("There is no QR code to copy.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
// Copy the image to the clipboard
Clipboard.SetImage(new Bitmap(qrCodeBox.Image));
MessageBox.Show("QR Code copied to clipboard.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
var aboutForm = new AboutBox1();
aboutForm.Show();
}
private void checkForUpdatesToolStripMenuItem1_Click(object sender, EventArgs e)
{
string latestReleaseUrl = $"https://api.github.com/repos/{repoOwner}/{repoName}/releases/latest";
string json;
using (WebClient wc = new WebClient())
{
wc.Headers.Add("User-Agent", "request"); // GitHub API requires a user-agent
json = wc.DownloadString(latestReleaseUrl);
}
var release = JsonConvert.DeserializeObject<dynamic>(json);
#pragma warning disable CS8602 // Dereference of a possibly null reference.
string latestVersion = release.tag_name;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
string downloadUrl = release.assets[0].browser_download_url;
if (Version.Parse(latestVersion) > Version.Parse(currentVersion))
{
if (MessageBox.Show($"Update available: {latestVersion}\nDo you want to download it?", "Update Available", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Process.Start(new ProcessStartInfo(downloadUrl) { UseShellExecute = true });
}
}
else
{
MessageBox.Show("Your application is up to date.");
}
}
private void btnChangeColor_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.AllowFullOpen = true;
cd.Color = colorPanel.BackColor;
cd.FullOpen = true;
cd.AnyColor = true;
if (cd.ShowDialog() == DialogResult.OK)
{
colorPanel.BackColor = cd.Color;
qrCodeColor = cd.Color;
}
}
private void btnUploadLogo_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Title = "Select a logo";
openFileDialog.Filter = "Image files (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Load and display the selected image in the PictureBox
logoPictureBox.ImageLocation = openFileDialog.FileName;
}
}
}
private void clearLogoButton_Click(object sender, EventArgs e)
{
// Check if there's currently an image displayed in the pictureBoxLogo
if (logoPictureBox.Image != null)
{
logoPictureBox.Image.Dispose(); // Dispose the image to free resources
logoPictureBox.Image = null; // Remove the image from the PictureBox
}
}
private void btnChangeBackgroundColor_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.AllowFullOpen = true;
cd.Color = colorPanel.BackColor;
cd.FullOpen = true;
cd.AnyColor = true;
if (cd.ShowDialog() == DialogResult.OK)
{
backgroundColorPanel.BackColor = cd.Color;
qrCodeBackgroundColor = cd.Color;
}
}
private void uploadImageToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
DecodeQRCode(openFileDialog.FileName);
}
}
private void DecodeQRCode(string imagePath)
{
var reader = new BarcodeReader
{
AutoRotate = true,
TryInverted = true,
Options = new ZXing.Common.DecodingOptions
{
PossibleFormats = new[] { ZXing.BarcodeFormat.QR_CODE },
TryHarder = true
}
};
using (var bitmap = (Bitmap)Bitmap.FromFile(imagePath))
{
LuminanceSource source = new BitmapLuminanceSource(bitmap);
var result = reader.Decode(bitmap);
if (result != null)
{
MessageBox.Show("QR Code Content: " + result.Text);
qrResultBox.Text = result.ToString();
}
else
{
MessageBox.Show("No QR Code found. Ensure the QR code is clear and well-positioned.");
}
}
}
private void clearButton_Click(object sender, EventArgs e)
{
stringBox.Text = "Enter your text to be put into QR Code";
qrResultBox.Text = "Import a QR Code and the data will be displayed here!";
qrCodeBox.Image = null;
logoPictureBox.Image = null;
backgroundColorPanel.BackColor = Color.White;
colorPanel.BackColor = Color.White;
}
private void geoCoordinatesToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "geo:<latitude>,<longitude>";
}
private void emailToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "MATMSG:TO:<email_address>;SUB:<subject>;BODY:<message>;;";
}
private void sMSToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "SMSTO:< phone_number >:< message >";
}
private void callToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "TEL:<phone_number>";
}
private void contactToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "BEGIN:VCARD\r\nVERSION:3.0\r\nN:<last_name>;<first_name>;;;\r\nFN:<first_name> <last_name>\r\nORG:<organization>\r\nTEL:<phone_number>\r\nEMAIL:<email_address>\r\nEND:VCARD\r\n";
}
private void eventToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "BEGIN:VEVENT\r\nSUMMARY:<event_title>\r\nDTSTART:<start_date_time>\r\nDTEND:<end_date_time>\r\nLOCATION:<location>\r\nDESCRIPTION:<description>\r\nEND:VEVENT\r\n";
}
private void wPAWPA2ToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "WIFI:T:WPA;S:MyNetworkName;P:MyPassword;;\r\n";
}
private void wEPToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "WIFI:T:WEP;S:MyNetworkName;P:MyPassword;;\r\n";
}
private void hiddenWPAWPA2ToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "WIFI:T:WPA;S:HiddenNetworkName;P:MyPassword;H:true;;\r\n";
}
private void openNetworkToolStripMenuItem_Click(object sender, EventArgs e)
{
stringBox.Text = "WIFI:T:nopass;S:OpenNetworkName;;\r\n";
}
}
}