www.2527.com_澳门新葡8455手机版_新京葡娱乐场网址_
做最好的网站

生成和解析二维码代码,NET生成二维码

2019-09-28 10:47 来源:未知

下面使用ThoughtWorks.QRCode.dll这个类库,实现生成二维码

类库文件我们在文件最后面下载

使用时需要增加:下面三个命名空间

【ThoughtWorks.QRCode.dll 就是类库】

using ThoughtWorks.QRCode.Codec;

使用时需要增加:

using ThoughtWorks.QRCode.Codec.Data;

using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;

using ThoughtWorks.QRCode.Codec.Util;

主要源代码:

下面代码主要是在asp.net中,生成一张二维码图片,在网页中显示,而且,将二维码图片保存在了,D:zlfzz。png;

1.生成二维码

C# 代码 复制

代码如下

using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Drawing.Imaging;using ThoughtWorks.QRCode.Codec;using ThoughtWorks.QRCode.Codec.Data;using ThoughtWorks.QRCode.Codec.Util;using System.IO;using System.Text;public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {           }    protected void Button1_Click(object sender, EventArgs e)    {        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;        qrCodeEncoder.QRCodeScale = 4;        qrCodeEncoder.QRCodeVersion = 8;        qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;        //String data = "Hello 二维码!";        String data = TextBox1.Text.ToString();        Response.Write;        System.Drawing.Bitmap image = qrCodeEncoder.Encode;        System.IO.MemoryStream MStream = new System.IO.MemoryStream();        image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);        Response.ClearContent();        Response.ContentType = "image/Png";        Response.BinaryWrite(MStream.ToArray;        //FileInfo f = new FileInfo(@"d:\zl.png");        Directory.CreateDirectory("d:\\zlf");        FileStream fs = new FileStream("d:\\zlf\\zz。png", FileMode.CreateNew, FileAccess.ReadWrite);        BinaryWriter bw = new BinaryWriter(fs, UTF8Encoding.UTF8);        byte[] by = MStream.ToArray();        for (int i = 0; i < MStream.ToArray().Length; i  )            bw.Write;        fs.Close();    }}

复制代码

aspx页面代码

QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
String encoding = cboEncoding.Text;
if (encoding == "Byte")
{
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
}
else if (encoding == "AlphaNumeric")
{
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
}
else if (encoding == "Numeric")
{
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
}
try
{
int scale = Convert.ToInt16(txtSize.Text);
qrCodeEncoder.QRCodeScale = scale;
}
catch (Exception ex)
{
MessageBox.Show("Invalid size!");
return;
}
try
{
int version = Convert.ToInt16(cboVersion.Text);
qrCodeEncoder.QRCodeVersion = version;
}
catch (Exception ex)
{
MessageBox.Show("Invalid version www.111cn.net !");
}

HTML 代码 复制

string errorCorrect = cboCorrectionLevel.Text;
if (errorCorrect == "L")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
else if (errorCorrect == "M")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
else if (errorCorrect == "Q")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
else if (errorCorrect == "H")
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
Image image;
String data = txtEncodeData.Text;
image = qrCodeEncoder.Encode;
pb_view.Image = image;

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><html><head runat="server">    <title>无标题页</title></head><body>    <form id="form1" runat="server">    <div>        请输入内容:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>        <br/>        <asp:Button ID="Button1" runat="server" Text="生成二维码" onclick="Button1_Click" />    </div>    </form></body></html>

2.解析二维码

代码如下

复制代码

QRCodeDecoder decoder = new QRCodeDecoder();
String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(pb_view.Image)));
txtEncodeData.Text = decodedString;

Winform下载:QRCode.rar

Web下载:webQRCode.rar

TAG标签:
版权声明:本文由澳门新葡8455手机版发布于www.2527.com,转载请注明出处:生成和解析二维码代码,NET生成二维码