笔记12:简易的二维码生成解析程序

来源:互联网 发布:vb趣味程序集锦 编辑:程序博客网 时间:2024/06/06 05:12
首先得引用一个文件。直接看代码吧!

 

 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Text; 7 using System.Windows.Forms; 8 using ThoughtWorks.QRCode.Codec; 9 using ThoughtWorks.QRCode.Codec.Data;10 11 namespace 二维码生成器12 {13     public partial class Form1 : Form14     {15         public Form1()16         {17             InitializeComponent();18         }19         private void button1_Click(object sender, EventArgs e)20         {21             if (textBox1.Text.Trim() != "")22             {23                 string enCodeString = textBox1.Text;24                 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();25                 pictureBox1.Image = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);26             }27             else28                 MessageBox.Show("请输入内容"); 29         }30 31         private void button2_Click(object sender, EventArgs e)32         {33             if (pictureBox1.Image != null)34             {35                 SaveFileDialog s = new SaveFileDialog();36                 s.Title = "保存二维码图片";37                 s.Filter = "图片文件(*.jpg)|*.jpg";38                 if (s.ShowDialog() == DialogResult.OK)39                     try40                     {41                         pictureBox1.Image.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);42                         MessageBox.Show("保存成功");43                     }44                     catch { MessageBox.Show("保存失败"); }45             }46         }47 48         //解析二维码49         string filepath = "";50         private void button4_Click(object sender, EventArgs e)51         {52             OpenFileDialog p = new OpenFileDialog();53             p.Title = "请选择二维码文件";54             p.Filter = "图片文件(*.jpg)|*.jpg";55             p.Multiselect = false;56             if (p.ShowDialog() == DialogResult.OK)57             {58                 filepath = p.FileName;59                 System.Threading.Thread t = new System.Threading.Thread(ss);60                 t.IsBackground = true;61                 t.Start();62             }63         }64         private void ss()65         {66             if (filepath != "")67             {68                 string tt = "";69                 try70                 {71                     Invoke((EventHandler)delegate72                     {73                         pictureBox1.Image = new Bitmap(filepath);74                     });75                     //pictureBox1.Size = new Size(new Bitmap(filepath).Size.Width, new Bitmap(filepath).Size.Height); 76                     QRCodeDecoder qrDecoder = new QRCodeDecoder();77                     string txtMsg = qrDecoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image)), Encoding.UTF8);78                     this.textBox2.Text = txtMsg;79                 }80                 catch { tt = "识别失败"; }81                 Invoke((EventHandler)delegate82                 {83                     button1.Text = "识别";84                 });85             }86             System.Threading.Thread.CurrentThread.Abort();87         }88     }89 }

 

0 0
原创粉丝点击