155 lines
5.6 KiB
C#
155 lines
5.6 KiB
C#
using QRCoder;
|
|
using System.Reflection.Emit;
|
|
using static QRCoder.PayloadGenerator;
|
|
|
|
namespace QR_Code_Generator
|
|
{
|
|
public partial class Main : Form
|
|
{
|
|
int pixlesPerModul = 20;
|
|
public enum EncryptionType
|
|
{
|
|
WPA,
|
|
WEP,
|
|
NoPass
|
|
}
|
|
public Main()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void bt_gen_QR_URL_Click(object sender, EventArgs e)
|
|
{
|
|
Url generator = new Url(tb_URL.Text);
|
|
string payload = generator.ToString();
|
|
|
|
|
|
|
|
QRCodeGenerator qrg = new QRCodeGenerator();
|
|
QRCodeData qrCodeData = qrg.CreateQrCode(payload, QRCodeGenerator.ECCLevel.H);
|
|
generateQR(qrCodeData);
|
|
|
|
void generateQR(QRCodeData qrCodeData)
|
|
{
|
|
Bitmap? icon = null;
|
|
if (pb_icon.Image != null)
|
|
{
|
|
icon = (Bitmap)pb_icon.Image;
|
|
}
|
|
using (QRCode qrCode = new QRCode(qrCodeData))
|
|
{
|
|
Bitmap qrCodeImage = qrCode.GetGraphic(pixlesPerModul, Color.Black, Color.White, icon, 25, 3);
|
|
pb_QRC.Image = qrCodeImage;
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
using var qrGenerator = new QRCodeGenerator();
|
|
var qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
|
|
var qrCode = new QRCode(qrCodeData);
|
|
|
|
// Rückgabe als Bitmap
|
|
return qrCode.GetGraphic(20);
|
|
}
|
|
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)
|
|
{
|
|
SaveQRCodeFromPictureBox(pb_QRC);
|
|
}
|
|
}
|
|
}
|