摘自:http://blog.csdn.net/zlfxy/article/details/8733333
本内容参考网上的资料,自己整理后,练习下,做了一个很简单的二维码生成Demo。
参考博客:http://blog.csdn.net/wangpeng047/article/details/7181217
1、本文主要要实现的效果图为:
上面的二维码是一张图片,里面输入了一些中文的消息,你可以用你的智能手机,扫描下。
2、主要实现的代码:
本代码主要是在asp.net中,生成一张二维码图片,在网页中显示,而且,将二维码图片保存在了,D:\zlf\zz.png;zz.png也就是上面看到的效果图。
- 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();
- }
- }
3、网页中的布局很简单,就是一个TextBox和一个Button。
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <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>
4、本文,在.net中用到了二维码类库,这是别人封装好的QRCode。
我已经将类库引入进了项目了,在bin文件夹下。
QRCode二维码维码支持中文Asp.Net(C#),是VS2010的项目,VS2008、VS2005也可以用,需要在你的项目里引用ThoughtWorks.QRCode.dll,在二维码生成页面内加上下面的代码:
- using System.Drawing.Imaging;
- using ThoughtWorks.QRCode.Codec;
- using ThoughtWorks.QRCode.Codec.Data;
- using ThoughtWorks.QRCode.Codec.Util;
- 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 二维码!";
- System.Drawing.Bitmap image = qrCodeEncoder.Encode(data);
- System.IO.MemoryStream MStream = new System.IO.MemoryStream();
- image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif);
- Response.ClearContent();
- Response.ContentType = "image/Gif";
- Response.BinaryWrite(MStream.ToArray());
如果需要源码练习的朋友,下载链接