如何用.NET生成二维码?

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

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

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

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


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

图形如下所示:

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

4、编译运行,如下图:

水平有限,望各位园友不吝赐教!如果觉得不错,请疯狂的点击推荐和关注!

作者:JackWang-CUMT

来源:51CTO

时间: 2024-08-02 07:01:37

如何用.NET生成二维码?的相关文章

iOS8 生成二维码与条形码

iOS8 生成二维码与条形码 效果图: 源码: // // ViewController.m // CodeCreator // // Created by YouXianMing on 15/3/16. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (nonatomic, stro

PHP在线生成二维码

PHP在线生成二维码 利用google在线生成二维码的api在线生成二维码   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <hea

使用PHP QR Code二维码类生成二维码

二维码就是用在平面上用特定的几何图形记录数据信息的,QR码是常见的一种二维码.QR原理理解起来比较复杂,自己处理的话,估计得花不少时间.这里推荐一个生成QR码的php类库PHP QR Code.这个我自己使用过了,没发现什么问题,分享给大家. 下载地址:http://download.csdn.net/detail/u011986449/6865449 QR码 Data表示要记录的数据,如果是存储utf-8编码的中文,最多984个. ECC表示纠错级别, 纠错级别越高,生成图片会越大. L水平

wps如何生成二维码

  wps生成二维码的方法: 1.在文档工具栏的右侧,有一个特色功能工具按钮,点击一下这个特色功能在下方可以选择文档格式的输出或是生成二维码,如图所示. 2.在文档工具栏的右侧,有一个特殊功能工具按钮,点击一下这个特色功能在下方可以选择文档格式的输出或是生成二维码,如图所示. 3.在生成的时候可以收到选择分享的终端,电脑.手机或是平板,然后点击分享就可以了. 4.然后系统自动进行连接和二维码的生成,如图所示. 5.通过扫描就可以查看完整文档了,互联网时代,是不是方便高科技多了呢.希望能够帮助到大

wps怎么生成二维码?

          Wps文档在使用的过程中,经常需要发送给领导或者朋友进行文档的审核确认,如果单个文件太大或者是避免让其他不想透露给的人员查看,我们可以生成二维码或者是访问链接进行分享的,今天给大家介绍一下如何操作. 1.在文档工具栏的右侧,有一个特色功能工具按钮,点击一下这个特色功能在下方可以选择文档格式的输出或是生成二维码,如图所示. 2.在文档工具栏的右侧,有一个特殊功能工具按钮,点击一下这个特色功能在下方可以选择文档格式的输出或是生成二维码,如图所示. 3.在生成的时候可以收到选择分享

jquery插件qrcode在线生成二维码

  jquery插件qrcode在线生成二维码          jquery.qrcode是一个强大的jquery的插件,可以用来二维码图形的渲染,用于生成二维码,非常的实用,需要的朋友参考下. 随着移动互联网的发展,二维码现在应用得越来越广泛了,随手扫扫就可以浏览网站.加个好友什么的,比起手工输入真的是方便太多了. 前期做了一个综合测评系统,考虑逐步实现移动化,一长串的IP地址用户输入也不方便,借助二维码的话,用户拿起手机扫扫就可以直接进入系统. 基于这个应用场景,就上网研究下了网站二维码的

利用google api生成二维码名片

 二维条码/二维码可以分为堆叠式/行排式二维条码和矩阵式二维条码.堆叠式/行排式二维条码形态上是由多行短截的一维条码堆叠而成:矩阵式二维条码以矩阵的形式组成,在矩阵相应元素位置上用"点"表示二进制"1",用"空"表示二进制"0","点"和"空"的排列组成代码. 堆叠式/行排式二维条码,如,Code 16K.Code 49.PDF417等. 矩阵式二维码,最流行莫过于QR CODE. 矩阵

使用PHP生成二维码的两种方法

 随着科技的进步,二维码应用领域越来越广泛,今天我给大家分享下如何使用PHP生成二维码,以及如何生成中间带LOGO图像的二维码 一.利用Google API生成二维码  Google提供了较为完善的二维码生成接口,调用API接口很简单,以下是调用代码: 代码如下: $urlToEncode="http://www.jb51.net";    generateQRfromGoogle($urlToEncode);    /**    * google api 二维码生成[QRcode可以

php生成二维码中文乱码怎么办

  最近做了个扫描二维码得到vcard的项目,遇到一个问题,有一部分生成完的二维码,用android系统手机扫描后得到的vcard中的中文姓名是乱码,经过比对发现,这部分vcard中ORG这个类型没有内容,随即判断没内容就加上一个固定的字符串,这样乱码的问题得以解决.这样解决算是曲线救国吧,不知道怎么根本解决乱码问题,谁得到方法请留言告知,谢谢. 三. php生成二维码的几种方式 1.google开放api  代码如下   $urlToEncode="http://gz.altmi.com&qu