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
// //
+153 -166
View File
@@ -1,160 +1,71 @@
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 :
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);
}
}
}
} }
private void button_Save_QRCode_Click(object sender, EventArgs e) 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) private void button_ChangeColorLight_Click(object sender, EventArgs e)
{ {
using (ColorDialog colorDialog = new ColorDialog()) ChangeLightColor();
{
// Optional: Standardfarbe festlegen
colorDialog.Color = lightColor;
// Dialog anzeigen
if (colorDialog.ShowDialog() == DialogResult.OK)
{
// Ausgewählte Farbe anwenden
lightColor = colorDialog.Color;
panel_ColorLight.BackColor = lightColor;
}
}
} }
private void button_ChangeColorDark_Click(object sender, EventArgs e) private void button_ChangeColorDark_Click(object sender, EventArgs e)
{ {
using (ColorDialog colorDialog = new ColorDialog()) ChangeDarkColor();
{ }
// Optional: Standardfarbe festlegen #endregion
colorDialog.Color = darkColor;
// Dialog anzeigen #region QR Code Generation
if (colorDialog.ShowDialog() == DialogResult.OK)
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 pictureBox.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Png);
darkColor = colorDialog.Color; MessageBox.Show("QR-Code erfolgreich gespeichert!", "Erfolg", MessageBoxButtons.OK, MessageBoxIcon.Information);
panel_ColorDark.BackColor = darkColor;
} }
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(), 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);
}
} }
} }