using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using ThoughtWorks.QRCode.Codec.Util; using System.IO; using System.Text; using System.Net; using System.Drawing; using System.Drawing.Drawing2D; public partial class 测试 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click+=Button1_Click; } void Button1_Click(object sender, EventArgs e) { try { string mypath = ""; //创建新对象 QRCodeEncoder qr = new QRCodeEncoder(); //字节:文字,英文,数字都可以 qr.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qr.QRCodeScale = 3;//尺寸 qr.QRCodeVersion = 5;//点数 qr.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//容错率 string data = TextBox1.Text.ToString();//要转换成二维码的信息 System.Drawing.Bitmap img = qr.Encode(data); System.IO.MemoryStream ms = new System.IO.MemoryStream(); img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); string path = mypath + "img/";//data.Trim().ToString()、Guid.NewGuid().ToString() string tempname = data.Trim().ToString() + ".png";//图片名称 Directory.CreateDirectory(Server.MapPath(path)); FileStream fs = new FileStream(Server.MapPath(path + tempname), FileMode.CreateNew, FileAccess.ReadWrite); BinaryWriter bw = new BinaryWriter(fs, UTF8Encoding.UTF8); byte[] by = ms.ToArray(); for (int i = 0; i < ms.ToArray().Length; i++) { bw.Write(by[i]); } fs.Close(); Image1.Src = path + tempname; } catch { } } }
bin文件夹需要添加的文件:ThoughtWorks.QRCode.dll 和 ThoughtWorks.QRCode.pdb
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ceshi.aspx.cs" Inherits="测试" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="生成二维码及相关信息" OnClick="Button1_Click"/> <br /><br /> <div style="width: 500px; height: 200px;border:1px solid gray;"> <img id="Image1" src="" runat="server" /> </div> </form> </body> </html>
后台:
时间: 2024-11-04 00:34:25