需要ThoughtWorks.QRCode.dll
项目
HtmlPage.html代码
class="brush:csharp;gutter:true;"><!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="js/jquery-1.11.2.min.js"></script> </head> <body> <input type="text" value="网址" id="txtMessage" /> <input type="button" value="生成" id="btn"/> <img src="" /> <script> $("#btn").click(function () { var a = $("#txtMessage").val(); $.ajax({ url: "/Handler.ashx", data: { msg: a }, type: "get", dataType: "json", success: function (data) { $("img").attr("src", data.msg); } }); }); </script> </body> </html>
Handler.aspx代码
<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; using ThoughtWorks.QRCode.Codec; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { //要生成的文本信息 如:https://www.baidu.com/ string a = context.Request.QueryString["msg"]; QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); //编码模式 qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeScale = 8;// // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大 qrCodeEncoder.QRCodeVersion = 8;// // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L; System.Drawing.Image image = qrCodeEncoder.Encode(a); string imgpath = "/img/1.jpg";//存储的路径 string filepath = context.Server.MapPath("~" + imgpath); System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);//文本的保存 fs.Close(); image.Dispose(); fs.Dispose(); context.Response.Write("{\"status\":1,\"msg\":\"" + imgpath + "\"}"); context.Response.End(); } public bool IsReusable { get { return false; } } }