用.NET如何生成二維碼
下面介紹一下如何用.NET生成二維碼(QR Code碼制),下面給出詳細(xì)步驟:
1、新建一個(gè)window應(yīng)用程序,然后引入.NET二維碼類(lèi)庫(kù)(開(kāi)源的類(lèi)庫(kù),可從網(wǎng)上下載):

2、構(gòu)建UI界面,代碼如下:
namespace WinQRCode
{
partial class Form1
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </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、編寫(xiě) 編碼和解碼按鈕事件,進(jìn)行編碼和解碼處理:
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、編譯運(yùn)行,如下圖:

以上就是關(guān)于使用.NET生成二維碼的大概思路,還有許多不足之處,希望大家多提建議,或者自己進(jìn)行創(chuàng)新修改。
相關(guān)文章
js插件類(lèi)庫(kù)組織與管理(基于asp.net管理)
在開(kāi)發(fā)一個(gè)較大規(guī)模的網(wǎng)站,js插件類(lèi)庫(kù)應(yīng)用的是相當(dāng)多。那么自然在一個(gè)頁(yè)面里就存在不少script和script相關(guān)的link標(biāo)記,這樣js組織與管理自然成了一大問(wèn)題。2010-07-07
asp.net 從POST的數(shù)據(jù)流中提取參數(shù)和文件
按理,F(xiàn)orm提交的數(shù)據(jù),無(wú)論是application/x-www-form-urlencoded還是multipart/form-data(有附件時(shí)),都可在服務(wù)端通過(guò)Request.Form["name"]和Request.Files["name"]獲取到參數(shù)和上傳的文件。2010-02-02
asp.net根據(jù)計(jì)算機(jī)MAC地址限定每臺(tái)機(jī)子只能領(lǐng)取一次賬號(hào)
這里只做簡(jiǎn)單演示過(guò)程,請(qǐng)根據(jù)您的實(shí)際情況作適當(dāng)修改!另外我的博客只做自己參考查詢方便用,請(qǐng)各位大神不要沒(méi)事噴我,知道您的技術(shù)高,我是新手正在努力學(xué)習(xí)當(dāng)中,謝謝2012-06-06
asp.net ListView 數(shù)據(jù)綁定
asp.net ListView 數(shù)據(jù)綁定 實(shí)現(xiàn)代碼2009-01-01
Entity Framework Core更新時(shí)間映射
這篇文章介紹了Entity Framework Core更新時(shí)間映射的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
asp.net實(shí)現(xiàn)在XmlTextWriter中寫(xiě)入一個(gè)CDATA的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)在XmlTextWriter中寫(xiě)入一個(gè)CDATA的方法,結(jié)合實(shí)例形式分析了XmlTextWriter寫(xiě)入CDATA的步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04
在SQL Server中使用CLR調(diào)用.NET方法實(shí)現(xiàn)思路
在.NET中新建一個(gè)類(lèi),并在這個(gè)類(lèi)里新建一個(gè)方法,然后在SQL Server中調(diào)用這個(gè)方法,接下來(lái)我們將實(shí)現(xiàn)這個(gè)功能做了以下幾個(gè)步驟,詳細(xì)看下本文,感興趣的你可不要錯(cuò)過(guò)了哈2013-02-02
把ASP.NET MVC項(xiàng)目部署到本地IIS上的完整步驟
最近會(huì)經(jīng)常修改一些網(wǎng)站前端的內(nèi)容,為了方便跟UI和產(chǎn)品交流,需要將自己修改過(guò)的頁(yè)面及時(shí)發(fā)布到測(cè)試機(jī)或者是本地的IIS上。下面這篇文章主要給大家介紹了關(guān)于如何把ASP.NET MVC項(xiàng)目部署到本地IIS上的相關(guān)資料,需要的朋友可以參考下2018-06-06

