c#---windows窗体GUI--软件模型4--二维码编码和解码
2.二维码使用的类
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using ThoughtWorks; using ThoughtWorks.QRCode; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using WindowsFormsApplication1.classtools; namespace WindowsFormsApplication1.classtools { class ErWeiMa { CurrencyTool ct = new CurrencyTool(); /// //代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform //1.绝对路径: //this.pictureBox2.Image=Image.FromFile("D:\\001.jpg"); //2.相对路径: //Application.StartupPath; //可以得到程序根目录 //string picPath=Application.StartupPath+"\\1.gif"; //3.获得网络图片的路径 //this.pictureBox2.Image= Image.FromStream(System.Net.WebRequest.Create(http://www.xyhhxx.com/images/logo/logo.gif).GetResponse().GetResponseStream()); /// //二维码生成函数 public void create_two(PictureBox pictureBox1,TextBox textBox7,string nr) { try { Bitmap bt; string enCodeString = nr; if (enCodeString == "" || enCodeString == null) { MessageBox.Show("信息不能为空!!"); return; } QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8); string filename = string.Format(DateTime.Now.ToString(), "yyyymmddhhmmss") + ".jpg"; filename = filename.Replace(" ", ""); filename = filename.Replace(":", ""); filename = filename.Replace("-", ""); filename = filename.Replace("/", ""); filename = filename.Replace(".", ""); bt.Save(ct.IsFlagPath(Application.StartupPath + "\\image\\") + filename + ".jpg"); pictureBox1.ImageLocation = Application.StartupPath + "/image/" + filename + ".jpg"; textBox7.Text = ct.IsFlagPath(Application.StartupPath + "\\image\\") + filename + ".jpg"; }catch(Exception e){ MessageBox.Show("您输入的内容过长!"); } } //生成二维码方法一 private void CreateCode_Simple(string nr) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeScale = 4; qrCodeEncoder.QRCodeVersion = 8; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; //System.Drawing.Image image = qrCodeEncoder.Encode("ssssssssssst test"); System.Drawing.Image image = qrCodeEncoder.Encode(nr); string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg"; string filepath = Application.StartupPath + "\\image" + "\\" + filename; System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); image.Dispose(); //二维码解码 var codeDecoder = CodeDecoder(filepath); } /// <summary> /// 二维码解码 /// </summary> /// <param name="filePath">图片路径</param> /// <returns></returns> public string CodeDecoder(string filePath) { if (!System.IO.File.Exists(filePath)) return null; Bitmap myBitmap = new Bitmap(Image.FromFile(filePath)); QRCodeDecoder decoder = new QRCodeDecoder(); string decodedString = decoder.decode(new QRCodeBitmapImage(myBitmap)); return decodedString; } } }
3.使用
string erstr = textBox6.Text.Trim();
//生成二维码
ewm.create_two(pictureBox1,textBox7,erstr);