dies und das

This commit is contained in:
2024-12-08 12:44:17 +01:00
parent 3e4ad1c9f4
commit d6a3ee4ff4
2 changed files with 154 additions and 167 deletions
+1 -1
View File
@@ -133,7 +133,7 @@
bt_createQRAdress.TabIndex = 0; bt_createQRAdress.TabIndex = 0;
bt_createQRAdress.Text = "generiere QR"; bt_createQRAdress.Text = "generiere QR";
bt_createQRAdress.UseVisualStyleBackColor = true; bt_createQRAdress.UseVisualStyleBackColor = true;
bt_createQRAdress.Click += bt_createQRAdress_Click;
// //
// tabPage3 // tabPage3
// //
+112 -125
View File
@@ -1,103 +1,114 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using QRCoder; using QRCoder;
using System.Reflection.Emit; using System.Drawing;
using static QRCoder.PayloadGenerator; using System.Windows.Forms;
using static QRCoder.QRCodeGenerator;
namespace QR_Code_Generator namespace QR_Code_Generator
{ {
public partial class Main : Form public partial class Main : Form
{ {
int pixlesPerModul = 20; #region Fields
Color darkColor = Color.Black; private int pixlesPerModul = 20;
Color lightColor = Color.White; private Color darkColor = Color.Black;
bool drawQuietZones = true; private Color lightColor = Color.White;
QRCodeGenerator.ECCLevel eCCLevel = ECCLevel.Q; private bool drawQuietZones = true;
string colorConfigFile = "colors.json"; 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 public enum EncryptionType
{ {
WPA, WPA,
WEP, WEP,
NoPass NoPass
} }
#endregion
#region Constructor
public Main() public Main()
{ {
InitializeComponent(); InitializeComponent();
panel_ColorDark.BackColor = darkColor; panel_ColorDark.BackColor = darkColor;
panel_ColorLight.BackColor = lightColor; 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) private void bt_gen_QR_URL_Click(object sender, EventArgs e)
{ {
// Überprüfe, ob ein Link eingegeben wurde GenerateAndDisplayQRCodeFromUrl();
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;
} }
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
pb_icon.Image = SelectImage(); 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) private void bt_clear_Icon_Click(object sender, EventArgs e)
{ {
pb_icon.Image = null; 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) private void bt_createQR_WIFI_Click(object sender, EventArgs e)
{ {
EncryptionType selectedType = GenerateAndDisplayWiFiQRCode();
radioButton_WPA.Checked ? EncryptionType.WPA : }
private void button_Save_QRCode_Click(object sender, EventArgs e)
{
SaveQRCodeFromPictureBox(pb_QRC);
}
private void button_ChangeColorLight_Click(object sender, EventArgs e)
{
ChangeLightColor();
}
private void button_ChangeColorDark_Click(object sender, EventArgs e)
{
ChangeDarkColor();
}
#endregion
#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 : radioButton_WEP.Checked ? EncryptionType.WEP :
EncryptionType.NoPass; EncryptionType.NoPass;
pb_QRC.Image = GenerateWiFiQRCode(textBox_ssid.Text, textBox_password.Text, checkBox_isHidden.Checked, selectedType); 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)
private 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 PayloadGenerator.WiFi.Authentication authType = encryptionType switch
{ {
EncryptionType.WPA => PayloadGenerator.WiFi.Authentication.WPA, EncryptionType.WPA => PayloadGenerator.WiFi.Authentication.WPA,
@@ -106,42 +117,54 @@ namespace QR_Code_Generator
_ => throw new ArgumentOutOfRangeException(nameof(encryptionType), "Ungültiger Verschlüsselungstyp") _ => throw new ArgumentOutOfRangeException(nameof(encryptionType), "Ungültiger Verschlüsselungstyp")
}; };
var wifiPayload = new PayloadGenerator.WiFi(ssid, password, authType, isHidden); string payload = new PayloadGenerator.WiFi(ssid, password, authType, isHidden).ToString();
string payload = wifiPayload.ToString(); return GenerateQRCode(payload, darkColor, lightColor, eCCLevel);
// Generiere den QR-Code
Bitmap qrCode = GenerateQRCode(payload, darkColor, lightColor, eCCLevel);
return qrCode;
} }
private Bitmap GenerateQRCode(string payload, Color dark, Color light, ECCLevel eCC) private Bitmap GenerateQRCode(string payload, Color dark, Color light, QRCodeGenerator.ECCLevel eCC)
{ {
QRCodeGenerator qrGenerator = new QRCodeGenerator(); using QRCodeGenerator qrGenerator = new QRCodeGenerator();
var qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); QRCodeData qrCodeData = qrGenerator.CreateQrCode(payload, eCC);
var qrCode = new QRCode(qrCodeData); using QRCode qrCode = new QRCode(qrCodeData);
// Rückgabe als Bitmap // Prüfen, ob ein Icon vorhanden ist
return qrCode.GetGraphic(20, dark, light, drawQuietZones); 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) public void SaveQRCodeFromPictureBox(PictureBox pictureBox)
{ {
// Überprüfen, ob ein Bild in der PictureBox vorhanden ist
if (pictureBox.Image == null) if (pictureBox.Image == null)
{ {
MessageBox.Show("Es befindet sich kein Bild in der PictureBox.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Es befindet sich kein Bild in der PictureBox.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
// Zeige einen Speicherdialog an using SaveFileDialog saveFileDialog = new SaveFileDialog
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{ {
saveFileDialog.Filter = "PNG-Datei|*.png"; Filter = "PNG-Datei|*.png",
saveFileDialog.Title = "QR-Code speichern"; Title = "QR-Code speichern",
saveFileDialog.FileName = "QRCode.png"; FileName = "QRCode.png"
};
// Wenn der Benutzer einen Dateipfad auswählt, speichere das Bild
if (saveFileDialog.ShowDialog() == DialogResult.OK) if (saveFileDialog.ShowDialog() == DialogResult.OK)
{ {
try try
@@ -155,46 +178,28 @@ namespace QR_Code_Generator
} }
} }
} }
} #endregion
private void button_Save_QRCode_Click(object sender, EventArgs e) #region Color Utilities
private void ChangeLightColor()
{ {
SaveQRCodeFromPictureBox(pb_QRC); using ColorDialog colorDialog = new ColorDialog { Color = lightColor };
}
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) if (colorDialog.ShowDialog() == DialogResult.OK)
{ {
// Ausgewählte Farbe anwenden
lightColor = colorDialog.Color; lightColor = colorDialog.Color;
panel_ColorLight.BackColor = lightColor; panel_ColorLight.BackColor = lightColor;
} }
} }
}
private void button_ChangeColorDark_Click(object sender, EventArgs e) private void ChangeDarkColor()
{ {
using (ColorDialog colorDialog = new ColorDialog()) using ColorDialog colorDialog = new ColorDialog { Color = darkColor };
{
// Optional: Standardfarbe festlegen
colorDialog.Color = darkColor;
// Dialog anzeigen
if (colorDialog.ShowDialog() == DialogResult.OK) if (colorDialog.ShowDialog() == DialogResult.OK)
{ {
// Ausgewählte Farbe anwenden
darkColor = colorDialog.Color; darkColor = colorDialog.Color;
panel_ColorDark.BackColor = darkColor; panel_ColorDark.BackColor = darkColor;
} }
} }
}
private void SaveColors(string filePath) private void SaveColors(string filePath)
{ {
@@ -203,44 +208,26 @@ namespace QR_Code_Generator
DarkColor = darkColor.ToArgb(), DarkColor = darkColor.ToArgb(),
LightColor = lightColor.ToArgb() LightColor = lightColor.ToArgb()
}; };
File.WriteAllText(filePath, JsonConvert.SerializeObject(colorConfig)); File.WriteAllText(filePath, JsonConvert.SerializeObject(colorConfig));
// MessageBox.Show("Farben wurden erfolgreich gespeichert!", "Erfolg", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
private void LoadColors(string filePath) private void LoadColors(string filePath)
{ {
if (!File.Exists(filePath)) if (!File.Exists(filePath)) return;
{
MessageBox.Show("Die Konfigurationsdatei wurde nicht gefunden.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try try
{ {
var colorConfig = JsonConvert.DeserializeObject<dynamic>(File.ReadAllText(filePath)); var colorConfig = JsonConvert.DeserializeObject<dynamic>(File.ReadAllText(filePath));
darkColor = Color.FromArgb((int)colorConfig.DarkColor); darkColor = Color.FromArgb((int)colorConfig.DarkColor);
lightColor = Color.FromArgb((int)colorConfig.LightColor); lightColor = Color.FromArgb((int)colorConfig.LightColor);
// Aktualisiere die UI, z. B. Farbpanels
panel_ColorDark.BackColor = darkColor; panel_ColorDark.BackColor = darkColor;
panel_ColorLight.BackColor = lightColor; 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);
} }
} }
#endregion
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
SaveColors(colorConfigFile);
}
private void Main_Load(object sender, EventArgs e)
{
LoadColors(colorConfigFile);
}
} }
} }