From d6a3ee4ff44d26a2c0c9185470563a0cb61945b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Sun, 8 Dec 2024 12:44:17 +0100 Subject: [PATCH] dies und das --- QR-Code-Generator/Main.Designer.cs | 2 +- QR-Code-Generator/Main.cs | 319 ++++++++++++++--------------- 2 files changed, 154 insertions(+), 167 deletions(-) diff --git a/QR-Code-Generator/Main.Designer.cs b/QR-Code-Generator/Main.Designer.cs index 3edda7f..accea57 100644 --- a/QR-Code-Generator/Main.Designer.cs +++ b/QR-Code-Generator/Main.Designer.cs @@ -133,7 +133,7 @@ bt_createQRAdress.TabIndex = 0; bt_createQRAdress.Text = "generiere QR"; bt_createQRAdress.UseVisualStyleBackColor = true; - bt_createQRAdress.Click += bt_createQRAdress_Click; + // // tabPage3 // diff --git a/QR-Code-Generator/Main.cs b/QR-Code-Generator/Main.cs index 3b7ed87..d55c6b1 100644 --- a/QR-Code-Generator/Main.cs +++ b/QR-Code-Generator/Main.cs @@ -1,160 +1,71 @@ using Newtonsoft.Json; using QRCoder; -using System.Reflection.Emit; -using static QRCoder.PayloadGenerator; -using static QRCoder.QRCodeGenerator; +using System.Drawing; +using System.Windows.Forms; namespace QR_Code_Generator { public partial class Main : Form { - int pixlesPerModul = 20; - Color darkColor = Color.Black; - Color lightColor = Color.White; - bool drawQuietZones = true; - QRCodeGenerator.ECCLevel eCCLevel = ECCLevel.Q; - string colorConfigFile = "colors.json"; + #region Fields + private int pixlesPerModul = 20; + private Color darkColor = Color.Black; + private Color lightColor = Color.White; + private bool drawQuietZones = true; + private QRCodeGenerator.ECCLevel eCCLevel = QRCodeGenerator.ECCLevel.Q; + private string colorConfigFile = "colors.json"; + private int iconSizePercent = 15; + private int iconBorderWidth = 5; + #endregion + + #region Enums public enum EncryptionType { WPA, WEP, NoPass } + #endregion + + #region Constructor public Main() { InitializeComponent(); panel_ColorDark.BackColor = darkColor; panel_ColorLight.BackColor = lightColor; } + #endregion + + #region Event Handlers + + private void Main_Load(object sender, EventArgs e) + { + LoadColors(colorConfigFile); + } + + private void Main_FormClosing(object sender, FormClosingEventArgs e) + { + SaveColors(colorConfigFile); + } private void bt_gen_QR_URL_Click(object sender, EventArgs e) { - // Überprüfe, ob ein Link eingegeben wurde - if (string.IsNullOrWhiteSpace(textBox_URL.Text)) - { - MessageBox.Show("Bitte geben Sie eine gültige URL ein.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - // Erstelle die URL-Payload - string url = textBox_URL.Text; - var urlPayload = new PayloadGenerator.Url(url).ToString(); - - // Generiere den QR-Code mit der URL-Payload - Bitmap qrCode = GenerateQRCode(urlPayload, darkColor, lightColor, eCCLevel); - - // Setze den generierten QR-Code in die PictureBox - pb_QRC.Image = qrCode; + GenerateAndDisplayQRCodeFromUrl(); } private void button1_Click(object sender, EventArgs e) { pb_icon.Image = SelectImage(); } - private Image? SelectImage() - { - using (OpenFileDialog openFileDialog = new OpenFileDialog()) - { - // Filter für Bilddateien festlegen - openFileDialog.Filter = "Bilddateien (*.jpg;*.jpeg;*.png;*.bmp;*.gif)|*.jpg;*.jpeg;*.png;*.bmp;*.gif"; - openFileDialog.Title = "Bild auswählen"; - - // Wenn der Benutzer eine Datei auswählt und auf OK klickt - if (openFileDialog.ShowDialog() == DialogResult.OK) - { - // Bild aus dem ausgewählten Pfad laden und zurückgeben - return Image.FromFile(openFileDialog.FileName); - } - else - { - // Gibt null zurück, wenn keine Datei ausgewählt wurde - return null; - } - } - } private void bt_clear_Icon_Click(object sender, EventArgs e) { pb_icon.Image = null; } - private void bt_createQRAdress_Click(object sender, EventArgs e) - { - string name = string.Empty; - string nachname = string.Empty; - string strasse = string.Empty; - - } - private void bt_createQR_WIFI_Click(object sender, EventArgs e) { - EncryptionType selectedType = - radioButton_WPA.Checked ? EncryptionType.WPA : - radioButton_WEP.Checked ? EncryptionType.WEP : - EncryptionType.NoPass; - - pb_QRC.Image = GenerateWiFiQRCode(textBox_ssid.Text, textBox_password.Text, checkBox_isHidden.Checked, selectedType); - } - public Bitmap GenerateWiFiQRCode(string ssid, string password, bool isHidden, EncryptionType encryptionType) - { - // Erstelle das WiFi-Payload basierend auf dem ausgewählten EncryptionType - PayloadGenerator.WiFi.Authentication authType = encryptionType switch - { - EncryptionType.WPA => PayloadGenerator.WiFi.Authentication.WPA, - EncryptionType.WEP => PayloadGenerator.WiFi.Authentication.WEP, - EncryptionType.NoPass => PayloadGenerator.WiFi.Authentication.nopass, - _ => throw new ArgumentOutOfRangeException(nameof(encryptionType), "Ungültiger Verschlüsselungstyp") - }; - - var wifiPayload = new PayloadGenerator.WiFi(ssid, password, authType, isHidden); - string payload = wifiPayload.ToString(); - - // Generiere den QR-Code - - Bitmap qrCode = GenerateQRCode(payload, darkColor, lightColor, eCCLevel); - return qrCode; - } - - private Bitmap GenerateQRCode(string payload, Color dark, Color light, ECCLevel eCC) - { - QRCodeGenerator qrGenerator = new QRCodeGenerator(); - var qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); - var qrCode = new QRCode(qrCodeData); - - // Rückgabe als Bitmap - return qrCode.GetGraphic(20, dark, light, drawQuietZones); - } - - public void SaveQRCodeFromPictureBox(PictureBox pictureBox) - { - // Überprüfen, ob ein Bild in der PictureBox vorhanden ist - if (pictureBox.Image == null) - { - MessageBox.Show("Es befindet sich kein Bild in der PictureBox.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - // Zeige einen Speicherdialog an - using (SaveFileDialog saveFileDialog = new SaveFileDialog()) - { - saveFileDialog.Filter = "PNG-Datei|*.png"; - saveFileDialog.Title = "QR-Code speichern"; - saveFileDialog.FileName = "QRCode.png"; - - // Wenn der Benutzer einen Dateipfad auswählt, speichere das Bild - if (saveFileDialog.ShowDialog() == DialogResult.OK) - { - try - { - pictureBox.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Png); - MessageBox.Show("QR-Code erfolgreich gespeichert!", "Erfolg", MessageBoxButtons.OK, MessageBoxIcon.Information); - } - catch (Exception ex) - { - MessageBox.Show($"Fehler beim Speichern: {ex.Message}", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - } + GenerateAndDisplayWiFiQRCode(); } private void button_Save_QRCode_Click(object sender, EventArgs e) @@ -164,35 +75,129 @@ namespace QR_Code_Generator private void button_ChangeColorLight_Click(object sender, EventArgs e) { - using (ColorDialog colorDialog = new ColorDialog()) - { - // Optional: Standardfarbe festlegen - colorDialog.Color = lightColor; - - // Dialog anzeigen - if (colorDialog.ShowDialog() == DialogResult.OK) - { - // Ausgewählte Farbe anwenden - lightColor = colorDialog.Color; - panel_ColorLight.BackColor = lightColor; - } - } + ChangeLightColor(); } private void button_ChangeColorDark_Click(object sender, EventArgs e) { - using (ColorDialog colorDialog = new ColorDialog()) - { - // Optional: Standardfarbe festlegen - colorDialog.Color = darkColor; + ChangeDarkColor(); + } + #endregion - // Dialog anzeigen - if (colorDialog.ShowDialog() == DialogResult.OK) + #region QR Code Generation + + private void GenerateAndDisplayQRCodeFromUrl() + { + if (string.IsNullOrWhiteSpace(textBox_URL.Text)) + { + MessageBox.Show("Bitte geben Sie eine gültige URL ein.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + string urlPayload = new PayloadGenerator.Url(textBox_URL.Text).ToString(); + pb_QRC.Image = GenerateQRCode(urlPayload, darkColor, lightColor, eCCLevel); + } + + private void GenerateAndDisplayWiFiQRCode() + { + EncryptionType selectedType = radioButton_WPA.Checked ? EncryptionType.WPA : + radioButton_WEP.Checked ? EncryptionType.WEP : + EncryptionType.NoPass; + + pb_QRC.Image = GenerateWiFiQRCode(textBox_ssid.Text, textBox_password.Text, checkBox_isHidden.Checked, selectedType); + } + + private Bitmap GenerateWiFiQRCode(string ssid, string password, bool isHidden, EncryptionType encryptionType) + { + PayloadGenerator.WiFi.Authentication authType = encryptionType switch + { + EncryptionType.WPA => PayloadGenerator.WiFi.Authentication.WPA, + EncryptionType.WEP => PayloadGenerator.WiFi.Authentication.WEP, + EncryptionType.NoPass => PayloadGenerator.WiFi.Authentication.nopass, + _ => throw new ArgumentOutOfRangeException(nameof(encryptionType), "Ungültiger Verschlüsselungstyp") + }; + + string payload = new PayloadGenerator.WiFi(ssid, password, authType, isHidden).ToString(); + return GenerateQRCode(payload, darkColor, lightColor, eCCLevel); + } + + private Bitmap GenerateQRCode(string payload, Color dark, Color light, QRCodeGenerator.ECCLevel eCC) + { + using QRCodeGenerator qrGenerator = new QRCodeGenerator(); + QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, eCC); + using QRCode qrCode = new QRCode(qrCodeData); + + // Prüfen, ob ein Icon vorhanden ist + Bitmap? icon = pb_icon.Image != null ? new Bitmap(pb_icon.Image) : null; + + return qrCode.GetGraphic( + pixlesPerModul, + dark, + light, icon, iconSizePercent, iconBorderWidth, drawQuietZones, null); + + } + #endregion + + #region Image Utilities + private Image? SelectImage() + { + using OpenFileDialog openFileDialog = new OpenFileDialog + { + Filter = "Bilddateien (*.jpg;*.jpeg;*.png;*.bmp;*.gif)|*.jpg;*.jpeg;*.png;*.bmp;*.gif", + Title = "Bild auswählen" + }; + + return openFileDialog.ShowDialog() == DialogResult.OK ? Image.FromFile(openFileDialog.FileName) : null; + } + + public void SaveQRCodeFromPictureBox(PictureBox pictureBox) + { + if (pictureBox.Image == null) + { + MessageBox.Show("Es befindet sich kein Bild in der PictureBox.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + using SaveFileDialog saveFileDialog = new SaveFileDialog + { + Filter = "PNG-Datei|*.png", + Title = "QR-Code speichern", + FileName = "QRCode.png" + }; + + if (saveFileDialog.ShowDialog() == DialogResult.OK) + { + try { - // Ausgewählte Farbe anwenden - darkColor = colorDialog.Color; - panel_ColorDark.BackColor = darkColor; + pictureBox.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Png); + MessageBox.Show("QR-Code erfolgreich gespeichert!", "Erfolg", MessageBoxButtons.OK, MessageBoxIcon.Information); } + catch (Exception ex) + { + MessageBox.Show($"Fehler beim Speichern: {ex.Message}", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + #endregion + + #region Color Utilities + private void ChangeLightColor() + { + using ColorDialog colorDialog = new ColorDialog { Color = lightColor }; + if (colorDialog.ShowDialog() == DialogResult.OK) + { + lightColor = colorDialog.Color; + panel_ColorLight.BackColor = lightColor; + } + } + + private void ChangeDarkColor() + { + using ColorDialog colorDialog = new ColorDialog { Color = darkColor }; + if (colorDialog.ShowDialog() == DialogResult.OK) + { + darkColor = colorDialog.Color; + panel_ColorDark.BackColor = darkColor; } } @@ -203,44 +208,26 @@ namespace QR_Code_Generator DarkColor = darkColor.ToArgb(), LightColor = lightColor.ToArgb() }; - File.WriteAllText(filePath, JsonConvert.SerializeObject(colorConfig)); - // MessageBox.Show("Farben wurden erfolgreich gespeichert!", "Erfolg", MessageBoxButtons.OK, MessageBoxIcon.Information); } + private void LoadColors(string filePath) { - if (!File.Exists(filePath)) - { - MessageBox.Show("Die Konfigurationsdatei wurde nicht gefunden.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } + if (!File.Exists(filePath)) return; try { var colorConfig = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); darkColor = Color.FromArgb((int)colorConfig.DarkColor); lightColor = Color.FromArgb((int)colorConfig.LightColor); - - // Aktualisiere die UI, z. B. Farbpanels panel_ColorDark.BackColor = darkColor; panel_ColorLight.BackColor = lightColor; - - // MessageBox.Show("Farben wurden erfolgreich geladen!", "Erfolg", MessageBoxButtons.OK, MessageBoxIcon.Information); } - catch (Exception ex) + catch { - MessageBox.Show($"Fehler beim Laden der Farben: {ex.Message}", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Fehler beim Laden der Farben.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - - private void Main_FormClosing(object sender, FormClosingEventArgs e) - { - SaveColors(colorConfigFile); - } - - private void Main_Load(object sender, EventArgs e) - { - LoadColors(colorConfigFile); - } + #endregion } }