Difference between revisions of "CSharp:QRCode"
From Aino Wiki
(→Esempio Completo) |
(No difference)
|
Latest revision as of 15:14, 1 October 2022
Contents
QRCoder
E' una libreria gratuita che consente SOLO la generazione di QRCode, disponibile sotto NuGet.
Autore: Raffael Herrmann.
Esempio Completo
Segue il codice della classe main di una Windows application ad hoc.
Il QRCode generato viene inviato come bitmap ad un controllo di tipo System.Windows.Forms.PictureBox
. Consente di scegliere tra divwerse tipologie di PlayLoad ovvero di tipi di QRCode destinati ciascuno ad uno scopo diverso.
Consente l'esportazione sia su File che su Clipboard.
NON E' UN ESEMPIO COMPLETO ma è a scopo didattico.
using QRCoder; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static QRCoder.PayloadGenerator; namespace QRCode_Gen { public partial class FrmMain : Form { private bool m_FormLoaded = false; private Bitmap m_qrCodeImage; private Dictionary<string, string> m_dctCmbBoxPlayloadType = new Dictionary<string, string>(); private Dictionary<string, string> m_dctCmbBoxFileDataType = new Dictionary<string, string>(); private Dictionary<string, PictureBoxSizeMode> m_dctCmbBoxPictureSizeMode = new Dictionary<string, PictureBoxSizeMode>(); public FrmMain() { InitializeComponent(); } private void FrmMain_Load(object sender, EventArgs e) { try { Populate_CmbBox_PlayLoadType(); Populate_CmbBox_SaveFileType(); Populate_CmbBox_PictureSizeMode(PictureBoxSizeMode.StretchImage.ToString()); CmbBox_ImageFormat.Text = "Bmp"; m_FormLoaded = true; } catch (Exception ex) { RTxt_Log.Text += string.Format("\r\n{0}", ex.Message); MessageBox.Show(ex.Message, "FrmMain_Load. Error" , MessageBoxButtons.OK, MessageBoxIcon.Error); } } #region Main Event Handlers private void BtnGeneteQR_Click(object sender, EventArgs e) { string txtToBeEncoded = string.Empty; string payload = string.Empty; int pixelsPerModule = 0; try { txtToBeEncoded = RTxt_Totranslate.Text; pixelsPerModule = int.Parse(TxtPixelsPerModule.Text); string playLoadSelect = ((KeyValuePair<string, string>)CmbBox_PlayloadType.SelectedItem).Key; QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData; switch (playLoadSelect) { case "bookmark": string title = TxtExtraInfo1.Text.Trim(); //"http://code-bude.net" Bookmark generatorBookmark = new Bookmark(txtToBeEncoded, title); payload = generatorBookmark.ToString(); qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); break; case "calendarEvent": qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); break; case "contactData": qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); break; case "geolocation": qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); break; case "email": string emailSubject = txtToBeEncoded; string emailObject = TxtExtraInfo2.Text; string emailAddress = LblExtraInfo1.Text; Mail generatorMail = new Mail(emailAddress, emailObject, emailSubject); payload = generatorMail.ToString(); qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); break; case "mms": qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); break; case "url": Url generator = new Url(txtToBeEncoded); payload = generator.ToString(); qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); break; case "sms": string phoneNumber = TxtExtraInfo1.Text.Trim(); //Es.: +393478165901 SMS generatorSMS = new SMS(phoneNumber, txtToBeEncoded); payload = generatorSMS.ToString(); qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); break; case "wifi": WiFi generatorWiFi = new WiFi(txtToBeEncoded, TxtExtraInfo1.Text.Trim(), WiFi.Authentication.WPA); payload = generatorWiFi.ToString(); qrCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q); break; default: //= "default" qrCodeData = qrGenerator.CreateQrCode(txtToBeEncoded, QRCodeGenerator.ECCLevel.Q); break; } QRCode qrCode = new QRCode(qrCodeData); m_qrCodeImage = qrCode.GetGraphic(pixelsPerModule); SetQRCodePictureSizeMode(); PctBx_QRCode.Image = m_qrCodeImage; RTxt_Log.Text += string.Format("\r\nTesto generato: \"{0}\"\r\nPixelPerModulo: {1}\r\n" , txtToBeEncoded, pixelsPerModule); } catch (Exception ex) { RTxt_Log.Text += string.Format("\r\n{0}", ex.Message); MessageBox.Show(ex.Message, "BtnGeneteQR_Click. Error" , MessageBoxButtons.OK, MessageBoxIcon.Error); } /* Original code: QRCodeData qrCodeData = qrGenerator.CreateQrCode(txtToBeEncoded , QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(20); ------------------info--------------------- QRCodeGenerator.ECCLevel The error correction level. Either L (7%), M (15%), Q (25%) or H (30%). Tells how much of the QR Code can get corrupted before the code isn't readable any longer. */ } private void CmbBox_PlayloadType_SelectedIndexChanged(object sender, EventArgs e) { try { LblMainText.Text = "Testo da tradurre:"; RTxt_Totranslate.Text = string.Empty; LblExtraInfo1.Visible = false; LblExtraInfo2.Visible = false; TxtExtraInfo1.Visible = false; TxtExtraInfo2.Visible = false; string playLoadSelect = ((KeyValuePair<string, string>)CmbBox_PlayloadType.SelectedItem).Key; switch (playLoadSelect) { case "bookmark": LblExtraInfo1.Visible = true; LblExtraInfo2.Visible = false; TxtExtraInfo1.Visible = true; TxtExtraInfo2.Visible = false; LblMainText.Text = "URL:"; LblExtraInfo1.Text = "Descrizione bookmark:"; RTxt_Totranslate.Text = "http://"; break; case "calendarEvent": MessageBox.Show("Non ancora implementato", "CmbBox_PlayloadType_SelectedIndexChanged" , MessageBoxButtons.OK, MessageBoxIcon.Information); break; case "contactData": MessageBox.Show("Non ancora implementato", "CmbBox_PlayloadType_SelectedIndexChanged" , MessageBoxButtons.OK, MessageBoxIcon.Information); break; case "geolocation": MessageBox.Show("Non ancora implementato", "CmbBox_PlayloadType_SelectedIndexChanged" , MessageBoxButtons.OK, MessageBoxIcon.Information); break; case "email": LblExtraInfo1.Visible = true; LblExtraInfo2.Visible = true; TxtExtraInfo1.Visible = true; TxtExtraInfo2.Visible = true; LblMainText.Text = "Testo della e-mail:"; LblExtraInfo1.Text = "e-mail:"; //Receiver-Destinatario LblExtraInfo2.Text = "Oggetto:"; break; case "mms": MessageBox.Show("Non ancora implementato", "CmbBox_PlayloadType_SelectedIndexChanged" , MessageBoxButtons.OK, MessageBoxIcon.Information); break; case "url": RTxt_Totranslate.Text = "https://"; break; case "sms": LblMainText.Text = "Testo SMS:"; LblExtraInfo1.Visible = true; TxtExtraInfo1.Visible = true; LblExtraInfo1.Text = "Cell. dest.:"; break; case "wifi": LblMainText.Text = "WiFi SSID:"; LblExtraInfo1.Visible = true; LblExtraInfo1.Text = "WiFi pw:"; TxtExtraInfo1.Visible = true; break; default: break; } } catch (Exception ex) { RTxt_Log.Text += string.Format("\r\n{0}", ex.Message); MessageBox.Show(ex.Message, "CmbBox_PlayloadType_SelectedIndexChanged. Error" , MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void BtnToBmp_Click(object sender, EventArgs e) { try { string txtToBeEncoded = RTxt_Totranslate.Text; string imageFormat = ((KeyValuePair<string, string>)CmbBox_ImageFormat.SelectedItem).Key; switch (imageFormat) { case "jpeg": break; case "png": break; case "bmp": break; case "gif": break; default: break; } txtToBeEncoded = txtToBeEncoded.Replace(" ", "_"); SaveFileDialog dialog = new SaveFileDialog(); dialog.Title = "Scegli dove salvare il QR Code generato"; dialog.InitialDirectory = "D:\\Documenti"; dialog.DefaultExt = "." + imageFormat; dialog.FileName = string.Format("{0}.{1}", txtToBeEncoded, imageFormat); if (dialog.ShowDialog() == DialogResult.OK) { m_qrCodeImage.Save(dialog.FileName, ImageFormat.Jpeg); //Jpeg Clipboard.SetImage(m_qrCodeImage); RTxt_Log.Text += string.Format("Salvato il file: {0}\r\n" , dialog.FileName); } } catch (Exception ex) { RTxt_Log.Text += string.Format("\r\n{0}", ex.Message); MessageBox.Show(ex.Message, "BtnToBmp_Click. Error" , MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void LblTextToTranslate_Click(object sender, EventArgs e) { PctBx_QRCode.Image = null; } private void Btn_QR2Clipboard_Click(object sender, EventArgs e) { try { string txtToBeEncoded = RTxt_Totranslate.Text; Clipboard.SetImage(m_qrCodeImage); RTxt_Log.Text += string.Format("QR code per {0} è in Clipboard\r\n" , txtToBeEncoded); } catch (Exception ex) { RTxt_Log.Text += string.Format("\r\n{0}", ex.Message); MessageBox.Show(ex.Message, "Btn_QR2Clipboard_Click. Error" , MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void BtnReset_Click(object sender, EventArgs e) { RTxt_Totranslate.Text = string.Empty; TxtExtraInfo1.Text = string.Empty; TxtExtraInfo2.Text = string.Empty; PctBx_QRCode.Image = null; CmbBox_PlayloadType.SelectedIndex = 0; CmbBox_ImageFormat.Text = "Bmp"; } private void CmbBox_PictureSizeMode_SelectedIndexChanged(object sender, EventArgs e) { try { if (m_FormLoaded) { SetQRCodePictureSizeMode(); } } catch (Exception ex) { RTxt_Log.Text += string.Format("\r\n{0}", ex.Message); MessageBox.Show(ex.Message, "CmbBox_PictureSizeMode_SelectedIndexChanged. Error" , MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion #region Private methods private void Populate_CmbBox_PlayLoadType() { m_dctCmbBoxPlayloadType.Add("default", "Default"); m_dctCmbBoxPlayloadType.Add("bookmark", "Bookmark"); m_dctCmbBoxPlayloadType.Add("calendarEvent", "CalendarEvent"); m_dctCmbBoxPlayloadType.Add("contactData", "ContactData"); m_dctCmbBoxPlayloadType.Add("geolocation", "Geolocation"); m_dctCmbBoxPlayloadType.Add("email", "Email"); m_dctCmbBoxPlayloadType.Add("mms", "MMS"); m_dctCmbBoxPlayloadType.Add("url", "URL"); m_dctCmbBoxPlayloadType.Add("sms", "SMS"); m_dctCmbBoxPlayloadType.Add("wifi", "WiFi"); BindingSource bndSrcCmbPlayloadType = new BindingSource(); bndSrcCmbPlayloadType.DataSource = m_dctCmbBoxPlayloadType; CmbBox_PlayloadType.DataSource = bndSrcCmbPlayloadType; CmbBox_PlayloadType.DisplayMember = "Key"; CmbBox_PlayloadType.ValueMember = "Value"; } private void Populate_CmbBox_SaveFileType() { m_dctCmbBoxFileDataType.Add("jpeg", "Jpeg"); m_dctCmbBoxFileDataType.Add("png", "Png"); m_dctCmbBoxFileDataType.Add("bmp", "Bmp"); m_dctCmbBoxFileDataType.Add("gif", "Gif"); BindingSource bndSrcCmbDBFileDataType = new BindingSource(); bndSrcCmbDBFileDataType.DataSource = m_dctCmbBoxFileDataType; CmbBox_ImageFormat.DataSource = bndSrcCmbDBFileDataType; CmbBox_ImageFormat.DisplayMember = "Key"; CmbBox_ImageFormat.ValueMember = "Value"; } private void Populate_CmbBox_PictureSizeMode(string defaultKeyText = "") { m_dctCmbBoxPictureSizeMode.Add("Normal", PictureBoxSizeMode.Normal); m_dctCmbBoxPictureSizeMode.Add("StretchImage", PictureBoxSizeMode.StretchImage); m_dctCmbBoxPictureSizeMode.Add("AutoSize", PictureBoxSizeMode.AutoSize); m_dctCmbBoxPictureSizeMode.Add("CenterImage", PictureBoxSizeMode.CenterImage); m_dctCmbBoxPictureSizeMode.Add("Zoom", PictureBoxSizeMode.Zoom); BindingSource bndSrcCmbPictureSizeMode = new BindingSource(); bndSrcCmbPictureSizeMode.DataSource = m_dctCmbBoxPictureSizeMode; CmbBox_PictureSizeMode.DataSource = bndSrcCmbPictureSizeMode; CmbBox_PictureSizeMode.DisplayMember = "Key"; CmbBox_PictureSizeMode.ValueMember = "Value"; if (defaultKeyText != string.Empty) { int i = Array.IndexOf(m_dctCmbBoxPictureSizeMode.Keys.ToArray(), defaultKeyText); CmbBox_PictureSizeMode.SelectedIndex = i; } } private void SetQRCodePictureSizeMode() { PictureBoxSizeMode sizeMode = ((KeyValuePair<string, PictureBoxSizeMode>)CmbBox_PictureSizeMode.SelectedItem).Value; PctBx_QRCode.SizeMode = sizeMode; RTxt_Log.Text += string.Format("\r\nImpostato SizeMode: {0}", sizeMode.ToString()); } #endregion } }
etc
using System; private void Prova() { for (int i = 0; i < 4; i++) { // Commento } }
Mappa e Link
C# | Librerie di terze parti PlugIn
Visual Studio | MS SQL | Dizionario
Parole chiave: