码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET生成二维码

时间:2014-11-04 17:16:53      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:des   http   io   ar   os   使用   for   sp   strong   

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

 

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

 

using ThoughtWorks.QRCode.Codec;

using ThoughtWorks.QRCode.Codec.Data;

using ThoughtWorks.QRCode.Codec.Util;

 

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

 

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

 

aspx页面代码

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

ASP.NET生成二维码

标签:des   http   io   ar   os   使用   for   sp   strong   

原文地址:http://www.cnblogs.com/ranran/p/4073989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!