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


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

 

using ThoughtWorks.QRCode.Codec;

using ThoughtWorks.QRCode.Codec.Data;

using ThoughtWorks.QRCode.Codec.Util;



ASP.NET生成二维码代码


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(data);
        System.Drawing.Bitmap image = qrCodeEncoder.Encode(data);
        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(by[i]);
        fs.Close();
    }
}




参考资料:  ASP.NET生成二维码   http://www.studyofnet.com/news/536.html