public void EncodeAtList(int index) { try { string source = resultList[index].ToString(); Image image = QRcodeHelper.Encode(source); string imageName = "QRcode_" + (index + 1).ToString() + ".jpg"; FileStream fs = new FileStream(this.createDirectory + "\\" + imageName, FileMode.OpenOrCreate, FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); mainForm.Invoke(new MethodInvoker(() => { mainForm.lvBatchEncode.Items[index].SubItems[2].Text = "已生成"; })); } catch { mainForm.Invoke(new MethodInvoker(() => { mainForm.lvBatchEncode.Items[index].SubItems[2].Text = "失败"; })); } }
//多线程编码,待调试 public void EncodeAll3() { readStart.WaitOne(); int resultNum = 0; int index = 0; string source = ""; for (int i = 0; ;) { try { start: lock (this) { resultNum = resultList.Count; if (i >= resultNum) { if (reading == true) { //Monitor.Wait(this); Monitor.Exit(this); readLineFinish.WaitOne(); //Thread.Sleep(1000); goto start; } else { return; } } else { index = i; //source = resultList[index].ToString(); } } int encodeNum = (resultNum - index) < 10 ? (resultNum - index) : 10; try { string[] strResult = new string[encodeNum]; CountdownEvent encodeHandler = new CountdownEvent(encodeNum); for (int j = 0; j < encodeNum; j++) { source = resultList[index + j].ToString(); int fileNum = j + index + 1; Thread Encode = new Thread(() => { try { Image image = QRcodeHelper.Encode(source); string imageName = "QRcode_" + fileNum.ToString() + ".jpg"; FileStream fs = new FileStream(this.createDirectory + "\\" + imageName, FileMode.OpenOrCreate, FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); lock (strResult) { strResult[j] = "已生成"; } } catch { lock (strResult) { strResult[j] = "失败"; } } finally { encodeHandler.Signal(); } }); Encode.IsBackground = true; Encode.Name = "编码线程" + (j + 1).ToString();; Encode.Start(); } encodeHandler.Wait(); for (int j = 0; j < encodeNum; j++) { mainForm.Invoke(new MethodInvoker(() => { mainForm.lvBatchEncode.Items[j + index].SubItems[2].Text = strResult[j]; })); } } finally { i += encodeNum; } } catch { continue; } } }
//读取文件和编码并行 public void EncodeAll2() { readStart.WaitOne(); int resultNum = 0; int index = 0; string source = ""; for (int i = 0; ;) { try { start: lock (this) { // mainForm.Invoke(new MethodInvoker(() => // { // resultNum = mainForm.listFileReaded.Items.Count; // })); resultNum = resultList.Count; if (i >= resultNum) { if (reading == true) { //Monitor.Wait(this); Monitor.Exit(this); readLineFinish.WaitOne(); //Thread.Sleep(1000); goto start; } else { return; } } else { // string threadName = Thread.CurrentThread.Name; // mainForm.Invoke(new MethodInvoker(() => // { // mainForm.toolStripStatusLabel2.Text = threadName + "正在生成"; // })); index = i; source = resultList[index].ToString(); } } try { Image image = QRcodeHelper.Encode(source); string imageName = "QRcode_" + (index + 1).ToString() + ".png"; string imagePath = this.createDirectory + "\\" + imageName; FileStream fs = new FileStream(imagePath, FileMode.OpenOrCreate, FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Png); fs.Close(); mainForm.Invoke(new MethodInvoker(() => { mainForm.lvBatchEncode.Items[index].SubItems[2].Text = "已生成"; })); } catch { mainForm.Invoke(new MethodInvoker(() => { mainForm.lvBatchEncode.Items[index].SubItems[2].Text = "失败"; })); } finally { i++; } } catch { continue; } } }
private void btnEncode_Click(object sender, EventArgs e) { if (txtEncodeData.Text.Trim() == String.Empty) { MessageBox.Show("Data must not be empty."); return; } try { useHybridEncryption = false; RSAKey = ""; ShowStatusLabel(sender, e); string data = txtEncodeData.Text; string key = txtEncodeKey.Text; data = data.TrimEnd(); #region 加密 //此处可以对data进行加密 int encryption = cboEncryptAlgo.SelectedIndex; switch (encryption) { case 0: //混合 string tempKey = new Random().Next(10000000, 99999999).ToString(); RSAKey = (Encryption.RSA.Operate.Encrypt(tempKey, cspPas))[0]; useHybridEncryption = true; data = Encryption.DES.Operate.Encrypt(data, tempKey); break; case 1: //des //密钥为8位 if (String.IsNullOrEmpty(key)) { key = fixedKey[0]; } data = Encryption.DES.Operate.Encrypt(data, key); break; case 2: //RSA data = Encryption.RSA.Operate.Encrypt(data, cspPas)[0]; break; default: break; } #endregion txtEncodeData.Text = data; picEncode.Image = QRcodeHelper.Encode(new QRcodeHelper.QRCodeInput() { Source = data, Width = picEncode.Width, Height = picEncode.Height, }); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }