二维码是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,国外对二维码技术的研究始于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. /// <summary>
  6. /// 必需的设计器变量。
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// 清理所有正在使用的资源。
  12. /// </summary>
  13. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  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. /// <summary>
  26. /// 设计器支持所需的方法 - 不要
  27. /// 使用代码编辑器修改此方法的内容。
  28. /// </summary>
  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(, );
  44. this.button1.Margin = new System.Windows.Forms.Padding(, , , );
  45. this.button1.Name = "button1";
  46. this.button1.Size = new System.Drawing.Size(, );
  47. this.button1.TabIndex = ;
  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(, );
  55. this.button2.Margin = new System.Windows.Forms.Padding(, , , );
  56. this.button2.Name = "button2";
  57. this.button2.Size = new System.Drawing.Size(, );
  58. this.button2.TabIndex = ;
  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(, );
  66. this.pictureBox1.Margin = new System.Windows.Forms.Padding(, , , );
  67. this.pictureBox1.Name = "pictureBox1";
  68. this.pictureBox1.Size = new System.Drawing.Size(, );
  69. this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  70. this.pictureBox1.TabIndex = ;
  71. this.pictureBox1.TabStop = false;
  72. //
  73. // label1
  74. //
  75. this.label1.AutoSize = true;
  76. this.label1.Location = new System.Drawing.Point(, );
  77. this.label1.Margin = new System.Windows.Forms.Padding(, , , );
  78. this.label1.Name = "label1";
  79. this.label1.Size = new System.Drawing.Size(, );
  80. this.label1.TabIndex = ;
  81. this.label1.Text = "信息:";
  82. //
  83. // textBox1
  84. //
  85. this.textBox1.Location = new System.Drawing.Point(, );
  86. this.textBox1.Margin = new System.Windows.Forms.Padding(, , , );
  87. this.textBox1.Name = "textBox1";
  88. this.textBox1.Size = new System.Drawing.Size(, );
  89. this.textBox1.TabIndex = ;
  90. //
  91. // label2
  92. //
  93. this.label2.AutoSize = true;
  94. this.label2.Location = new System.Drawing.Point(, );
  95. this.label2.Margin = new System.Windows.Forms.Padding(, , , );
  96. this.label2.Name = "label2";
  97. this.label2.Size = new System.Drawing.Size(, );
  98. this.label2.TabIndex = ;
  99. this.label2.Text = "二维码:";
  100. //
  101. // lbl解码信息
  102. //
  103. this.lbl解码信息.AutoSize = true;
  104. this.lbl解码信息.Location = new System.Drawing.Point(, );
  105. this.lbl解码信息.Margin = new System.Windows.Forms.Padding(, , , );
  106. this.lbl解码信息.Name = "lbl解码信息";
  107. this.lbl解码信息.Size = new System.Drawing.Size(, );
  108. this.lbl解码信息.TabIndex = ;
  109. //
  110. // Form1
  111. //
  112. this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
  113. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  114. this.ClientSize = new System.Drawing.Size(, );
  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)()));
  123. this.Margin = new System.Windows.Forms.Padding(, , , );
  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. #endregion
  133.  
  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 WinQRCode
  11. {
  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 : Form
  18. {
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23. /// <summary>
  24. /// 编码
  25. /// </summary>
  26. /// <param name="sender"></param>
  27. /// <param name="e"></param>
  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 = ;
  41. qrCodeEncoder.QRCodeVersion = ;
  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. /// <summary>
  54. /// 解码
  55. /// </summary>
  56. /// <param name="sender"></param>
  57. /// <param name="e"></param>
  58. private void button2_Click(object sender, EventArgs e)
  59. {
  60. Cursor.Current = Cursors.WaitCursor;
  61. Bitmap b = this.pictureBox1.Image as Bitmap;
  62. try
  63. {
  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、编译运行,如下图:

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

  1. 如何用C代码生成二维码

    如何用C代码生成二维码 当下因微信和支付宝等手机应用广泛使用,而基于二维码/一维条码的移动支付,也借助手机移动端席卷全国,使得越来越多的人知道有"二维码"这么一种东西. 对于普通用 ...

  2. Javascript生成二维码(QR)

    网络上已经有非常多的二维码编码和解码工具和代码,很多都是服务器端的,也就是说需要一台服务器才能提供二维码的生成.本着对服务器性能的考虑,这种小事情都让服务器去做,感觉对不住服务器,尤其是对于大流量的网 ...

  3. 使用jquery.qrcode生成二维码(转)

    jQuery 的 qrcode 插件就可以在浏览器端生成二维码图片. 这个插件的使用非常简单: 1.首先在页面中加入jquery库文件和qrcode插件. <script type=" ...

  4. iOS 生成二维码

    首先先下载生成二维码的支持文件 libqrencode 添加依赖库 CoreGraphics.framework. QuartzCore.framework.AVFoundation.framewor ...

  5. QR code 扩展生成二维码

    include './phpqrcode/phpqrcode.php';  //引入QR库 QRcode::png("leo", 'qrcode.png', 'L', 10);  ...

  6. Python 创建本地服务器环境生成二维码

    一. 需求 公司要做一个H5手机端适配页面,因技术问题所以H5是外包的,每次前端给我们源码,我们把源码传到服务器让其他人访问看是否存在bug,这个不是很麻烦吗?有人说,可以让前端在他们的服务器上先托管 ...

  7. C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)

    用C#如何生成二维码,我们可以通过现有的第三方dll直接来实现,下面列出几种不同的生成方法: 1):通过QrCodeNet(Gma.QrCodeNet.Encoding.dll)来实现 1.1):首先 ...

  8. 使用Spire.Barcode程序库生成二维码

    使用Spire.Barcode程序库生成二维码 某天浏览网页发现了一个二维码的程序库.它的描述说他可以扫描二维码图像.我很感兴趣,想试试他是不是会有用.所以我就用了些方法扫描二维码图像来测试一下.结果 ...

  9. C#运用ThoughtWorks生成二维码

    在现在的项目中,较多的使用到二维码,前面介绍过一篇使用Gma生成二维码的操作,现在介绍一个第三方组件,主要介绍生成二维码,二维码的解析,以及对二维码的相关信息的选择,现在介绍ThoughtWorks用 ...

随机推荐

  1. Python字符串的encode与decode

    首先要搞清楚,字符串在Python内部的表示是unicode编码. 因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unic ...

  2. struts2标签

    一.通用标签 1.property     Name Required Default Evaluated Type Description default false   false String ...

  3. android target unknown and state offline解决办法

    没有错,将adb的版本升级一下就好了! 下载地址为:http://files.cnblogs.com/files/hujunzheng/adb1.0.32.zip

  4. 重置Oracle密码

    在系统运行中输入: sqlplus /nolog 在命令窗口分别执行: conn /as sysdba alter user scott identified by tiger; alter user ...

  5. [Node.js] 使用TypeScript编写Node项目

    原文地址:http://www.moye.me/2015/04/25/using_typescript/ 入 TypeScript 坑有一段时间了,由衷的看好,所以决定在自留地絮叨一番. 是什么 Ty ...

  6. Git的奇技淫巧🙈

    Git的奇技淫巧

  7. CSS3 介绍

    什么是CSS3 CSS即层叠样式表(Cascading Stylesheet). 在网页制作时采用CSS技术,可以有效地对页面的布局.字体.颜色.背景和其它效果实现更加精确的控制. 只要对相应的代码做 ...

  8. 深入分析MVC中通过IOC实现Controller依赖注入的原理

    这几天利用空闲时间,我将ASP.NET反编译后的源代码并结合园子里几位大侠的写的文章认真的看了一遍,收获颇丰,同时也摘要了一些学习内容,存入了该篇文章:<ASP.NET运行机制图解>,在对 ...

  9. 【Android】你应该知道的调试神器----adb

    最近跟着一个前辈在做TV应用,因为不能通过usb连接调试,接触到了adb,突然间觉得自己似乎发现了另外一个世界,借助adb shell命令对应用进行调试,简直方便得不行.更重要的是,这是命令行操作啊! ...

  10. a[href^=""]的解释

    a[href^="绝对路径"]这个算一种属性选择器,在这里是用来选择链接属性为“绝对路径”的a标签,其中的^其实是"以什么为开始"(begin with)的意思 ...