1、需要引入Gma.QrCodeNet.Encoding.Net35;
2、先看下图片素材
3、结果
4、前台代码
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>测试</title> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script> <style> #btn { border:1px solid #ddd; color:#333; padding:5px 20px; } #img { width:375px; height:667px; } </style> </head> <body> <div> <input type="button" value="下载" id="btn"/> <img id="img"/> <div id="tip"> </div> </div> <script> $("#btn").click(function () { $.post("/Home/GetQrCode", {}, function (data) { if (data.Success) { $("#tip").append("<p>" + data.Message + "</p>"); $("#img").attr("src", data.Url); location.href = "/Home/DownFileQrCode/" + data.Message; } else { alert("生成失败!"); } }); }); </script> </body> </html>
5、后台代码
using System.Web.Mvc; using Gma.QrCodeNet.Encoding; using Gma.QrCodeNet.Encoding.Windows.Render; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System; namespace QeCodeTest.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { return View(); } /// <summary> /// 图片下载 /// </summary> /// <param name="id">文件名</param> /// <returns></returns> public FilePathResult DownFileQrCode(string id) { var path = Server.MapPath("/QrCode/" + id + ".png"); var filename = Path.GetFileName(path); return File(path, "application/octet-stream", filename); } /// <summary> /// 请求生成图片 /// </summary> /// <returns></returns> [HttpPost] public ActionResult GetQrCode() { string content = "你好啊";//二维码生成所需的内容文字 string name = DateTime.Now.ToString("yyyyMMddHHmmss");//二维码名称 //开始生成二维码 var encoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = encoder.Encode(content); GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(14, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); //将二维码信息画在GDI+位图上面 Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix); //将GDI+位图转换为流数据 MemoryStream ms = new MemoryStream(); map.Save(ms, ImageFormat.Png); //流数据可以保存 //if (!Directory.Exists(Server.MapPath("/QrCode/"))) //{ // Directory.CreateDirectory(Server.MapPath("/QrCode/"));//不存在就创建目录 //} //map.Save(Server.MapPath("/QrCode/" + name + ".png"), ImageFormat.Png); //获取背景图片 Image qrcodebgimg = Image.FromFile(Server.MapPath("/Comm/001.jpg")); //获取二维码的图片 Image qrcodeimg = Image.FromStream(ms); //获取头像的图片 Image img = Image.FromFile(Server.MapPath("/Comm/002.png")); //将背景转换为GID+位图 准备操作 Bitmap qrcodebgmap = new Bitmap(qrcodebgimg); //位图转换为画板 Graphics qrcodebgg = Graphics.FromImage(qrcodebgmap); //开始画二维码和头像 //初始化一个位置 Point qrcodePoint = new Point((qrcodebgmap.Width - qrcodeimg.Width) / 2, (qrcodebgmap.Height - qrcodeimg.Height) / 2); qrcodebgg.DrawImage(qrcodeimg, qrcodePoint.X, qrcodePoint.Y, qrcodeimg.Width, qrcodeimg.Height); //初始化一个位置 Point iconPoint = new Point((qrcodebgmap.Width - img.Width) / 2, (qrcodebgmap.Height - img.Height) / 2); qrcodebgg.DrawImage(img, iconPoint.X, iconPoint.Y, img.Width, img.Height); //写字 //画刷颜色 SolidBrush txtBrush = new SolidBrush(ColorTranslator.FromHtml("#FFFFFF")); //格式居中 StringFormat txtFromat = new StringFormat(); txtFromat.Alignment = StringAlignment.Center; //开始画 文字 字体 画笔 位置中间 qrcodebgg.DrawString("你好啊,这段话是文字", new Font("黑体", 20), txtBrush, new Point(qrcodebgmap.Width/2, 20),txtFromat); qrcodebgg.DrawString("点击关注", new Font("黑体", 20), txtBrush, new Point(qrcodebgmap.Width/2, 60), txtFromat); //可转换为流数据进行操作 //MemoryStream qrcodebgms = new MemoryStream(); //qrcodebgmap.Save(qrcodebgms, ImageFormat.Png); string Path = Server.MapPath("/QrCode/" + name + ".png"); qrcodebgmap.Save(Path, ImageFormat.Png); return Json(new {Success=true,Message= name, Url= "/QrCode/" + name + ".png" }); } } }
留下您的脚步
最近评论