示例#1
0
        private void btnOpen2_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFD = new OpenFileDialog();

            OFD.Filter           = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png";
            OFD.FilterIndex      = 1;
            OFD.RestoreDirectory = true;
            OFD.FileName         = string.Empty;

            if (OFD.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            txtQRCodePath.Text = OFD.FileName;

            try
            {
                string txt = QRcodeHelper.Decode(txtQRCodePath.Text);
                txt = txt.Replace(Environment.NewLine, " ");
                if (txt.Length > 50)
                {
                    txt  = txt.Substring(0, 50);
                    txt += "...";
                }
                txtSearch.Text = txt;
            }
            catch (Exception ex)
            {
                txtSearch.Text = "";
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
示例#2
0
        private void listFileFounded_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvFileSearch.SelectedItems.Count <= 0)
            {
                return;
            }

            try
            {
                Thread showThread = new Thread(() =>
                {
                    try
                    {
                        Invoke(new MethodInvoker(() =>
                        {
                            label17.Text = "解码中";
                        }));

                        string path = "";
                        Invoke(new MethodInvoker(() =>
                        {
                            path = lvFileSearch.SelectedItems[0].SubItems[1].Text;
                        }));
                        string txt = QRcodeHelper.Decode(path);
                        txt        = txt.Replace("\r\n", " ");
                        if (txt.Length > 50)
                        {
                            txt  = txt.Substring(0, 50);
                            txt += "...";
                        }
                        Invoke(new MethodInvoker(() =>
                        {
                            label17.Text = txt;
                        }));
                    }
                    catch
                    {
                        Invoke(new MethodInvoker(() =>
                        {
                            label17.Text = "解码失败";
                        }));
                    }
                    finally
                    {
                    }
                });
                showThread.Start();
            }
            catch
            {
                label17.Text = "解码失败";
            }
        }
示例#3
0
        private void btnDecode_Click(object sender, EventArgs e)
        {
            try
            {
                string data = QRcodeHelper.Decode(picDecode.Image);

                //此处可以对decodedString进行解密
                #region 解密
                int    encryption = cboDecryptAlgo.SelectedIndex;
                string key        = txtDecodeKey.Text;

                switch (encryption)
                {
                case 0:
                    //混合
                    string tempRSAKey = Operate.ReadWriteFile.File2String(OpenedFileName + ".key");
                    string tempKey    = Encryption.RSA.Operate.Decrypt(tempRSAKey, cspPas);
                    data = Encryption.DES.Operate.Decrypt(data, tempKey);
                    break;

                case 1:
                    //des
                    //密钥为8位
                    if (String.IsNullOrEmpty(key))
                    {
                        key = fixedKey[0];
                    }
                    data = Encryption.DES.Operate.Decrypt(data, key);
                    data = data.TrimEnd();
                    break;

                case 2:
                    //RSA
                    data = Encryption.RSA.Operate.Decrypt(data, cspPas);
                    break;

                default:
                    break;
                }
                #endregion

                txtDecodedData.Text = data;
                ShowStatusLabel(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
示例#4
0
        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 = "失败";
                }));
            }
        }
示例#5
0
        //多线程编码,待调试
        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;
                }
            }
        }
示例#6
0
        //读取文件和编码并行
        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;
                }
            }
        }
示例#7
0
        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);
            }
        }
示例#8
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtSearch.Text) && String.IsNullOrEmpty(txtQRCodePath.Text))
            {
                return;
            }

            Running = true;

            try
            {
                tabMain.AllowSelect = false;
                btnSearch.Enabled   = false;
                btnRemove.Enabled   = false;
                AutoResetEvent areFinish = new AutoResetEvent(false);
                Thread         waitTime  = new Thread(() =>
                {
                    DateTime dtStart = DateTime.Now;
                    Invoke(new MethodInvoker(() =>
                    {
                        toolStripStatusLabel2.Text = "正在搜索文件和解码二维码";
                    }));
                    areFinish.WaitOne();
                    DateTime dtFinish = DateTime.Now;
                    Invoke(new MethodInvoker(() =>
                    {
                        toolStripStatusLabel2.Text = "搜索完成,所用时间:"
                                                     + ((int)(dtFinish - dtStart).TotalSeconds).ToString("d") + "秒";

                        btnSearch.Enabled   = true;
                        btnRemove.Enabled   = true;
                        tabMain.AllowSelect = true;
                        Running             = false;
                    }));
                });
                waitTime.IsBackground = true;
                waitTime.Name         = "计时线程";
                waitTime.Start();

                string source = String.Empty;
                if (String.IsNullOrEmpty(txtQRCodePath.Text))
                {
                    source = txtSearch.Text;
                }
                else
                {
                    source = QRcodeHelper.Decode(txtQRCodePath.Text);
                }
                lvFileSearch.Items.Clear();
                string     Dir              = txtSelectPath.Text;
                string     SearchPattern    = "*.jpg,*.png,*gif,*.bmp";
                FileSearch fs               = new FileSearch(Dir, SearchPattern, source, this);
                Thread     searchFileThread = new Thread(() =>
                {
                    fs.Start();
                    areFinish.Set();
                });
                searchFileThread.IsBackground = true;
                searchFileThread.Name         = "搜索主线程";
                searchFileThread.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }