/// <summary> /// Could return bigger bitmap /// </summary> /// <param name="sAddress"></param> /// <param name="iSize"></param> /// <returns></returns> private static Bitmap CreateQRCode(string sAddress, int iSize) { // Adding new QRCode encoding system int iMinimumSize = 33; iSize = Math.Max(iSize, iMinimumSize); QREncoder enc = new QREncoder(); enc.ErrorCorrection = ErrorCorrection.H; // this leads to a 33 x 33 qrcode enc.ModuleSize = 1; enc.QuietZone = 4; int iModuleSize = Math.Max(1, (int)Math.Ceiling((double)iSize / (double)iMinimumSize)); enc.QuietZone = 4 * iModuleSize; enc.ModuleSize = iModuleSize; enc.Encode(sAddress); return(enc.CreateQRCodeBitmap()); }
private void OnEncode(object sender, EventArgs e) { // get error correction code ErrorCorrection ErrorCorrection = (ErrorCorrection)ErrorCorrectionComboBox.SelectedIndex; // get module size string ModuleStr = ModuleSizeTextBox.Text.Trim(); if (!int.TryParse(ModuleStr, out int ModuleSize) || ModuleSize < 1 || ModuleSize > 100) { MessageBox.Show("Module size error."); return; } // get quiet zone string QuietStr = QuietZoneTextBox.Text.Trim(); if (!int.TryParse(QuietStr, out int QuietZone) || QuietZone < 1 || QuietZone > 100) { MessageBox.Show("Quiet zone error."); return; } // get data for QR Code string Data = DataTextBox.Text.Trim(); if (Data.Length == 0) { MessageBox.Show("Data must not be empty."); return; } // disable buttons EnableButtons(false); try { QRCodeEncoder.ErrorCorrection = ErrorCorrection; QRCodeEncoder.ModuleSize = ModuleSize; QRCodeEncoder.QuietZone = QuietZone; // multi segment if (SeparatorCheckBox.Checked && Data.IndexOf('|') >= 0) { string[] Segments = Data.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); // encode data QRCodeEncoder.Encode(Segments); } // single segment else { // encode data QRCodeEncoder.Encode(Data); } // create bitmap QRCodeImage = QRCodeEncoder.CreateQRCodeBitmap(); } catch (Exception Ex) { MessageBox.Show("Encoding exception.\r\n" + Ex.Message); } // enable buttons EnableButtons(true); // repaint panel Invalidate(); return; }