用.NET如何生成二维码

所属分类: 网络编程 / ASP.NET 阅读数: 745
收藏 0 赞 0 分享

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

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

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

namespace WinQRCode 
{ 
  partial class Form1 
  { 
    /// <summary> 
    /// 必需的设计器变量。 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 
 
    /// <summary> 
    /// 清理所有正在使用的资源。 
    /// </summary> 
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 
    protected override void Dispose(bool disposing) 
    { 
      if (disposing && (components != null)) 
      { 
        components.Dispose(); 
      } 
      base.Dispose(disposing); 
    } 
 
    #region Windows 窗体设计器生成的代码 
 
    /// <summary> 
    /// 设计器支持所需的方法 - 不要 
    /// 使用代码编辑器修改此方法的内容。 
    /// </summary> 
    private void InitializeComponent() 
    { 
      this.button1 = new System.Windows.Forms.Button(); 
      this.button2 = new System.Windows.Forms.Button(); 
      this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
      this.label1 = new System.Windows.Forms.Label(); 
      this.textBox1 = new System.Windows.Forms.TextBox(); 
      this.label2 = new System.Windows.Forms.Label(); 
      this.lbl解码信息 = new System.Windows.Forms.Label(); 
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 
      this.SuspendLayout(); 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(446, 31); 
      this.button1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(103, 37); 
      this.button1.TabIndex = 0; 
      this.button1.Text = "编码"; 
      this.button1.UseVisualStyleBackColor = true; 
      this.button1.Click += new System.EventHandler(this.button1_Click); 
      // 
      // button2 
      // 
      this.button2.Location = new System.Drawing.Point(446, 81); 
      this.button2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 
      this.button2.Name = "button2"; 
      this.button2.Size = new System.Drawing.Size(103, 37); 
      this.button2.TabIndex = 1; 
      this.button2.Text = "解码"; 
      this.button2.UseVisualStyleBackColor = true; 
      this.button2.Click += new System.EventHandler(this.button2_Click); 
      // 
      // pictureBox1 
      // 
      this.pictureBox1.Location = new System.Drawing.Point(23, 117); 
      this.pictureBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 
      this.pictureBox1.Name = "pictureBox1"; 
      this.pictureBox1.Size = new System.Drawing.Size(279, 253); 
      this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 
      this.pictureBox1.TabIndex = 2; 
      this.pictureBox1.TabStop = false; 
      // 
      // label1 
      // 
      this.label1.AutoSize = true; 
      this.label1.Location = new System.Drawing.Point(18, 39); 
      this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); 
      this.label1.Name = "label1"; 
      this.label1.Size = new System.Drawing.Size(69, 25); 
      this.label1.TabIndex = 3; 
      this.label1.Text = "信息:"; 
      // 
      // textBox1 
      // 
      this.textBox1.Location = new System.Drawing.Point(95, 37); 
      this.textBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 
      this.textBox1.Name = "textBox1"; 
      this.textBox1.Size = new System.Drawing.Size(333, 31); 
      this.textBox1.TabIndex = 4; 
      // 
      // label2 
      // 
      this.label2.AutoSize = true; 
      this.label2.Location = new System.Drawing.Point(18, 81); 
      this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); 
      this.label2.Name = "label2"; 
      this.label2.Size = new System.Drawing.Size(88, 25); 
      this.label2.TabIndex = 5; 
      this.label2.Text = "二维码:"; 
      // 
      // lbl解码信息 
      // 
      this.lbl解码信息.AutoSize = true; 
      this.lbl解码信息.Location = new System.Drawing.Point(101, 81); 
      this.lbl解码信息.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); 
      this.lbl解码信息.Name = "lbl解码信息"; 
      this.lbl解码信息.Size = new System.Drawing.Size(0, 25); 
      this.lbl解码信息.TabIndex = 6; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(572, 384); 
      this.Controls.Add(this.lbl解码信息); 
      this.Controls.Add(this.label2); 
      this.Controls.Add(this.textBox1); 
      this.Controls.Add(this.label1); 
      this.Controls.Add(this.pictureBox1); 
      this.Controls.Add(this.button2); 
      this.Controls.Add(this.button1); 
      this.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 
      this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); 
      this.Name = "Form1"; 
      this.Text = "二维码"; 
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); 
      this.ResumeLayout(false); 
      this.PerformLayout(); 
 
    } 
 
    #endregion 
 
    private System.Windows.Forms.Button button1; 
    private System.Windows.Forms.Button button2; 
    private System.Windows.Forms.PictureBox pictureBox1; 
    private System.Windows.Forms.Label label1; 
    private System.Windows.Forms.TextBox textBox1; 
    private System.Windows.Forms.Label label2; 
    private System.Windows.Forms.Label lbl解码信息; 
  } 
} 

图形如下所示:

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

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
 
namespace WinQRCode 
{ 
  using ThoughtWorks.QRCode.Codec; 
  using ThoughtWorks.QRCode.Codec.Data; 
  using ThoughtWorks.QRCode.Codec.Util; 
  using System.IO; 
  using PdfToImage; 
  public partial class Form1 : Form 
  { 
    public Form1() 
    { 
      InitializeComponent(); 
    } 
    /// <summary> 
    /// 编码 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void button1_Click(object sender, EventArgs e) 
    { 
      if (this.textBox1.Text != "") 
      { 
        Cursor.Current = Cursors.WaitCursor; 
        if (textBox1.Text.Trim() == String.Empty) 
        { 
          MessageBox.Show("不能为空."); 
          return; 
        } 
        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();        
        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; 
        qrCodeEncoder.QRCodeScale = 4; 
        qrCodeEncoder.QRCodeVersion = 7;       
        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;        
        System.Drawing.Image image; 
        String data = textBox1.Text; 
        //编码 
        image = qrCodeEncoder.Encode(data); 
        //图片显示 
        this.pictureBox1.Image = image; 
        Cursor.Current = Cursors.Default; 
 
      } 
    } 
    /// <summary> 
    /// 解码 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void button2_Click(object sender, EventArgs e) 
    { 
      Cursor.Current = Cursors.WaitCursor; 
      Bitmap b = this.pictureBox1.Image as Bitmap; 
      try 
      { 
        QRCodeDecoder decoder = new QRCodeDecoder(); 
        //解码 
        String decodedString = decoder.decode(new QRCodeBitmapImage(b)); 
        //显示解码信息 
        this.lbl解码信息.Text += decodedString ; 
      } 
      catch (Exception ex) 
      { 
        MessageBox.Show(ex.Message); 
      } 
      Cursor.Current = Cursors.Default; 
    } 
 
    private void Form1_Load(object sender, EventArgs e) 
    { 
 
    } 
  } 
} 

4、编译运行,如下图:

以上就是关于使用.NET生成二维码的大概思路,还有许多不足之处,希望大家多提建议,或者自己进行创新修改。

更多精彩内容其他人还在看

asp.net 虚方法、抽象方法、接口疑问

asp.net 虚方法、抽象方法、接口疑问等说明。
收藏 0 赞 0 分享

c#  操作符?? null coalescing operator

?? "null coalescing" operator 是c#新提供的一个操作符,这个操作符提供的功能是判断左侧的操作数是否是null,如果是则返回结果是右侧的操作数;非null则返回左侧的操作数。
收藏 0 赞 0 分享

.net 反序题目的详细解答第1/2页

在各种答案,以及平时面试过程中,这道题总归会有一些非常典型的错误发生。其中给老赵的感觉也非常有意思,不知其中的“思路”是否如老赵猜测那样。
收藏 0 赞 0 分享

implicitly convert type 'int' to 'short'的原因与解决方法

implicitly convert type 'int' to 'short'的原因与解决方法
收藏 0 赞 0 分享

比较完整的 asp.net 学习流程

好多朋友想学习后台编程语言,但请注意的事,学习后台是个循序渐进的过程,不可能一下就到位,其实不只是asp.net其它的编程语言都需要下面的一些知识。
收藏 0 赞 0 分享

官网 Ext direct包中.NET版的问题

下载了官网的 Ext direct 包进行研究,发现服务器端返回结果存在一点小问题。
收藏 0 赞 0 分享

C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)第1/2页

C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等),以下就是操作XML的所有方法,相信可以满足很大一部份的使用了。
收藏 0 赞 0 分享

c# 连接字符串数据库服务器端口号 .net状态服务器端口号

正常的数据库连接字符串配置,这是在MSSQL服务器端口是1433(默认)的情况下。
收藏 0 赞 0 分享

ASP.NET 路径问题的解决方法

相对路径和绝对路径在ASP.NET中可以用~/来解决.
收藏 0 赞 0 分享

asp.net TemplateField模板中的Bind方法和Eval方法

在TemplateField模板中为了能够有限制的或者取出数据库中某列的值时,可以用Bind和Eval方法来实现。以下是Bind方法的格式,Eval的格式也是和Bind一样的。 Bind("列的名称","显示的格式文")
收藏 0 赞 0 分享
查看更多