二轮冲刺第三天

今天我的任务是继续编写代码

今天我编写的代码是读取二维码

代码如下:

//读取二维码-读取
        private void button9_Click(object sender, EventArgs e)
        {
            DecodingOptions decodeOption = new DecodingOptions();
            decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
            //读取二维码
            ZXing.BarcodeReader br = new BarcodeReader();
            br.Options = decodeOption;
            ZXing.Result rs = br.Decode(pictureBox4.Image as Bitmap);
            if (rs == null)
            {
                MessageBox.Show("读取失败");
            }
            else
            {
                //读取成功
                textBox7.Text = rs.Text;
            }
        }
        //保存图片
        private void saveImage(PictureBox imagePb, string filename)
        {
            try
            {
                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);  //默认保存目录
                fileDialog.RestoreDirectory = true;  //对话框记忆之前打开的目录
                fileDialog.Filter = "图片文件(*.jpg)|*.jpg";  //显示的文件类型
                fileDialog.FileName = filename + ".jpg";    //默认文件名
                if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string filePath = fileDialog.FileName;
                    imagePb.Image.Save(filePath);   //保存
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(filename + " 保存失败: " + ex.Message);
            }
        }
        //打开图片
        private void openImage(TextBox textBox, PictureBox imagePb)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);  //默认打开目录
            ofd.Filter = "图片文件|*.jpg|所有文件|*.*";  //显示的文件类型
            ofd.RestoreDirectory = true;  //对话框记忆之前打开的目录
            ofd.FilterIndex = 1;  //文件类型的显示顺序
            if (ofd.ShowDialog() == DialogResult.OK)  //选中文件
            {
                textBox.Text = ofd.FileName; //选中的音乐文件的路径
                imagePb.Image = new Bitmap(ofd.FileName);
            }
        }
        private void textBox4_TextChanged(object sender, EventArgs e)
        {
        }
    }
}
posted @ 2018-12-07 21:07  刘冇  阅读(103)  评论(0编辑  收藏  举报