用.NET如何生成二维码

所属分类: 网络编程 / ASP.NET 阅读数: 758
收藏 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生成二维码的大概思路,还有许多不足之处,希望大家多提建议,或者自己进行创新修改。

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

运行page页面时的事件执行顺序及页面的回发与否深度了解

page页面时的事件执行顺序的了解对于一些.net开发者起到者尤关重要的作用;页面的回发与否会涉及到某些事件执行与不执行,在本文中会详细介绍,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

Web.config(应用程序的配置信息)总结

Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中,接下来详细介绍一下配置情况,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

C#时间格式化(Datetime)用法详解

C#时间格式化Datetime.ToString参数format格式详细用法,本文将进行介绍,感兴趣的朋友可以了解下啊
收藏 0 赞 0 分享

实现onmouseover和onmouseout应用于RadioButtonList或CheckBoxList控件上

一直想实现onmouseover和onmouseout应用于RadioButtonList或CheckBoxList控件上。此功能就是当鼠标经过时RadioButtonList或CheckBoxList每一个Item时,让Item有特效显示,离开时,恢复原样有演示动画,感兴趣的朋
收藏 0 赞 0 分享

Asp.net 图片文件防盗链(尊重劳动成果)及BeginRequest事件学习

关于图片盗链这个问题,毕竟是自己的劳动成功,很多人不希望别人就那么轻易地偷走了;反盗链的程序其实很简单,熟悉ASP.NET 应用程序生命周期的话很容易就可以写一个,运用HttpModule在BeginRequest事件中拦截请求就ok了
收藏 0 赞 0 分享

asp.net Grid 导出Excel实现程序代码

看了FineUI中的将Grid导出为Excel的实现方法,实际上是可以非常简单。看来很难的问题,变换一种思路就可以非常简单
收藏 0 赞 0 分享

使用C#处理WebBrowser控件在不同域名中的跨域问题

我们在做web测试时,经常会使用WebBrowser来进行一些自动化的任务而有些网页上面会用IFrame去嵌套别的页面,这些页面可能不是在相同域名下的,这时就会出现跨域问题,无法直接在WebBrowser中获取到IFrame中的元素,接下来介绍如何解决此问题,需要了解的朋友可以参
收藏 0 赞 0 分享

.NET 下运用策略模式(组合行为和实体的一种模式)

我简单的理解策略模式就是把行为(方法)单独的抽象出来,并采用组合(Has-a)的方式,来组合行为和实体的一种模式比如,.NET中对数组排序的Sort的方法就是一个策略模式的实现模板
收藏 0 赞 0 分享

使用Entity Framework(4.3.1版本)遇到的问题整理

在这里记录一下之前使用Entity Framework(4.3.1版本)遇到的问题:更新没有设置主键的表、更改Code-First的默认连接、检测字符串截断错误,需要的朋友可以参考下
收藏 0 赞 0 分享

Asp.net利用JQuery AJAX实现无刷新评论思路与代码

Asp.net利用JQuery AJAX实现无刷新评论,此功能是每一个从事asp.net开发者的朋友都希望实现的,本文利用闲暇时间整理了一些,有需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多