ASP.NET生成二维码

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

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

using ThoughtWorks.QRCode.Codec;

using ThoughtWorks.QRCode.Codec.Data;

using ThoughtWorks.QRCode.Codec.Util;

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

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

aspx页面代码

 
HTML 代码   复制
ASP.NET生成二维码
ASP.NET生成二维码<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
ASP.NET生成二维码ASP.NET生成二维码
ASP.NET生成二维码<html>
ASP.NET生成二维码<head runat="server">
ASP.NET生成二维码 <title>无标题页</title>
ASP.NET生成二维码</head>
ASP.NET生成二维码<body>
ASP.NET生成二维码 <form id="form1" runat="server">
ASP.NET生成二维码 <div>
ASP.NET生成二维码 请输入内容:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
ASP.NET生成二维码 <br/>
ASP.NET生成二维码 <asp:Button ID="Button1" runat="server" Text="生成二维码" onclick="Button1_Click" />
ASP.NET生成二维码 </div>
ASP.NET生成二维码 </form>
ASP.NET生成二维码</body>
ASP.NET生成二维码</html>
ASP.NET生成二维码
上一篇:WPF中父子窗口的层次关系


下一篇:Linux入门之常用命令(11) 系统监控 vmstat top