首先得引用一个文件。直接看代码吧!
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 : Form 14 { 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 else 28 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 try 40 { 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 try 70 { 71 Invoke((EventHandler)delegate 72 { 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)delegate 82 { 83 button1.Text = "识别"; 84 }); 85 } 86 System.Threading.Thread.CurrentThread.Abort(); 87 } 88 } 89 }
时间: 2024-11-08 21:57:57