博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何用.NET生成二维码?
阅读量:7237 次
发布时间:2019-06-29

本文共 9219 字,大约阅读时间需要 30 分钟。

  二维码是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,国外对二维码技术的研究始于20世纪80年代末,在二维码符号表示技术研究方面已研制出多种码制,常见的有PDF417、QR Code、Code 49、Code 16K、Code One等。二维码技术已经被广泛应用于公安、外交、军事等部门对各类证件的管理,而且也将二维码应用于海关、税务等部门对各类报表和票据的管理,商业、交通运输等部门对商品及货物运输的管理、邮政部门对邮政包裹的管理、工业生产领域对工业生产线的自动化管理。

  下面介绍一下如何用.NET生成二维码(QR Code码制),下面给出详细步骤:

1、新建一个window应用程序,然后引入.NET二维码类库(开源的类库,可从网上下载):

2、构建UI界面,代码如下:

1 namespace WinQRCode  2 {  3     partial class Form1  4     {  5         ///   6         /// 必需的设计器变量。  7         ///   8         private System.ComponentModel.IContainer components = null;  9  10         ///  11         /// 清理所有正在使用的资源。 12         ///  13         /// 如果应释放托管资源,为 true;否则为 false。 14         protected override void Dispose(bool disposing) 15         { 16             if (disposing && (components != null)) 17             { 18                 components.Dispose(); 19             } 20             base.Dispose(disposing); 21         } 22  23         #region Windows 窗体设计器生成的代码 24  25         ///  26         /// 设计器支持所需的方法 - 不要 27         /// 使用代码编辑器修改此方法的内容。 28         ///  29         private void InitializeComponent() 30         { 31             this.button1 = new System.Windows.Forms.Button(); 32             this.button2 = new System.Windows.Forms.Button(); 33             this.pictureBox1 = new System.Windows.Forms.PictureBox(); 34             this.label1 = new System.Windows.Forms.Label(); 35             this.textBox1 = new System.Windows.Forms.TextBox(); 36             this.label2 = new System.Windows.Forms.Label(); 37             this.lbl解码信息 = new System.Windows.Forms.Label(); 38             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 39             this.SuspendLayout(); 40             //  41             // button1 42             //  43             this.button1.Location = new System.Drawing.Point(446, 31); 44             this.button1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 45             this.button1.Name = "button1"; 46             this.button1.Size = new System.Drawing.Size(103, 37); 47             this.button1.TabIndex = 0; 48             this.button1.Text = "编码"; 49             this.button1.UseVisualStyleBackColor = true; 50             this.button1.Click += new System.EventHandler(this.button1_Click); 51             //  52             // button2 53             //  54             this.button2.Location = new System.Drawing.Point(446, 81); 55             this.button2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 56             this.button2.Name = "button2"; 57             this.button2.Size = new System.Drawing.Size(103, 37); 58             this.button2.TabIndex = 1; 59             this.button2.Text = "解码"; 60             this.button2.UseVisualStyleBackColor = true; 61             this.button2.Click += new System.EventHandler(this.button2_Click); 62             //  63             // pictureBox1 64             //  65             this.pictureBox1.Location = new System.Drawing.Point(23, 117); 66             this.pictureBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 67             this.pictureBox1.Name = "pictureBox1"; 68             this.pictureBox1.Size = new System.Drawing.Size(279, 253); 69             this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 70             this.pictureBox1.TabIndex = 2; 71             this.pictureBox1.TabStop = false; 72             //  73             // label1 74             //  75             this.label1.AutoSize = true; 76             this.label1.Location = new System.Drawing.Point(18, 39); 77             this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); 78             this.label1.Name = "label1"; 79             this.label1.Size = new System.Drawing.Size(69, 25); 80             this.label1.TabIndex = 3; 81             this.label1.Text = "信息:"; 82             //  83             // textBox1 84             //  85             this.textBox1.Location = new System.Drawing.Point(95, 37); 86             this.textBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 87             this.textBox1.Name = "textBox1"; 88             this.textBox1.Size = new System.Drawing.Size(333, 31); 89             this.textBox1.TabIndex = 4; 90             //  91             // label2 92             //  93             this.label2.AutoSize = true; 94             this.label2.Location = new System.Drawing.Point(18, 81); 95             this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); 96             this.label2.Name = "label2"; 97             this.label2.Size = new System.Drawing.Size(88, 25); 98             this.label2.TabIndex = 5; 99             this.label2.Text = "二维码:";100             // 101             // lbl解码信息102             // 103             this.lbl解码信息.AutoSize = true;104             this.lbl解码信息.Location = new System.Drawing.Point(101, 81);105             this.lbl解码信息.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);106             this.lbl解码信息.Name = "lbl解码信息";107             this.lbl解码信息.Size = new System.Drawing.Size(0, 25);108             this.lbl解码信息.TabIndex = 6;109             // 110             // Form1111             // 112             this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);113             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;114             this.ClientSize = new System.Drawing.Size(572, 384);115             this.Controls.Add(this.lbl解码信息);116             this.Controls.Add(this.label2);117             this.Controls.Add(this.textBox1);118             this.Controls.Add(this.label1);119             this.Controls.Add(this.pictureBox1);120             this.Controls.Add(this.button2);121             this.Controls.Add(this.button1);122             this.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));123             this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);124             this.Name = "Form1";125             this.Text = "二维码";126             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();127             this.ResumeLayout(false);128             this.PerformLayout();129 130         }131 132         #endregion133 134         private System.Windows.Forms.Button button1;135         private System.Windows.Forms.Button button2;136         private System.Windows.Forms.PictureBox pictureBox1;137         private System.Windows.Forms.Label label1;138         private System.Windows.Forms.TextBox textBox1;139         private System.Windows.Forms.Label label2;140         private System.Windows.Forms.Label lbl解码信息;141     }142 }

图形如下所示:

3、编写 编码和解码按钮事件,进行编码和解码处理:

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace WinQRCode11 {12     using ThoughtWorks.QRCode.Codec;13     using ThoughtWorks.QRCode.Codec.Data;14     using ThoughtWorks.QRCode.Codec.Util;15     using System.IO;16     using PdfToImage;17     public partial class Form1 : Form18     {19         public Form1()20         {21             InitializeComponent();22         }23         /// 24         /// 编码25         /// 26         /// 27         /// 28         private void button1_Click(object sender, EventArgs e)29         {30             if (this.textBox1.Text != "")31             {32                 Cursor.Current = Cursors.WaitCursor;33                 if (textBox1.Text.Trim() == String.Empty)34                 {35                     MessageBox.Show("不能为空.");36                     return;37                 }38                 QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();               39                 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;40                 qrCodeEncoder.QRCodeScale = 4;41                 qrCodeEncoder.QRCodeVersion = 7;             42                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;               43                 System.Drawing.Image image;44                 String data = textBox1.Text;45                 //编码46                 image = qrCodeEncoder.Encode(data);47                 //图片显示48                 this.pictureBox1.Image = image;49                 Cursor.Current = Cursors.Default;50 51             }52         }53         /// 54         /// 解码55         /// 56         /// 57         /// 58         private void button2_Click(object sender, EventArgs e)59         {60             Cursor.Current = Cursors.WaitCursor;61             Bitmap b = this.pictureBox1.Image as Bitmap;62             try63             {64                 QRCodeDecoder decoder = new QRCodeDecoder();65                 //解码66                 String decodedString = decoder.decode(new QRCodeBitmapImage(b));67                 //显示解码信息68                 this.lbl解码信息.Text += decodedString ;69             }70             catch (Exception ex)71             {72                 MessageBox.Show(ex.Message);73             }74             Cursor.Current = Cursors.Default;75         }76 77         private void Form1_Load(object sender, EventArgs e)78         {79 80         }81     }82 }

4、编译运行,如下图:

 

转载地址:http://warfm.baihongyu.com/

你可能感兴趣的文章
使用使用Zopfli优化PNG图片
查看>>
德扑 AI 之父托马斯·桑德霍姆:扑克 AI 如何完虐人类,和 AlphaGo 大不同
查看>>
最常用的四种大数据分析方法
查看>>
极进网络石奇海:向802.11ac技术智能迁移的八大考虑要素
查看>>
中央财经大学创新创业中心主任尚超:大数据技术在防范虚假发票中的应用
查看>>
测试硬件,不妨试试这5款应用
查看>>
《数字逻辑设计与计算机组成》一3.8 实数算术
查看>>
把恶意程序存储到DNA上?黑客们的又一新发现
查看>>
ARM Tech Day:加速释放ARM IP,代号DynamIQ向人工智能进击
查看>>
介绍几种常见的提权方法,你用过几个?
查看>>
IT服务以人为本--感于护航十周年庆典
查看>>
网上转账全免?多家银行网上转账免费
查看>>
《数据虚拟化:商务智能系统的数据架构与管理》一 1.6 数据虚拟化的定义
查看>>
戴尔发布面向制造、生命科学和研究的高性能计算系统
查看>>
赛迪顾问:大数据带来大机会 运营商需关注四大课题
查看>>
双11阿里核心交易系统上云 为全球首次
查看>>
互联网金融 最不该放松安全这根神经
查看>>
既然无法击败AI,何不投身其中:Elon Musk重返脑机接口业务
查看>>
黑客可通过 USB 3.0 端口完全控制使用英特尔第六/七代处理器的PC
查看>>
大数据特区风起张北“中国数坝”
查看>>