.net中如何利用Gma.QrCodeNet生成二维码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.Drawing.Imaging;
using System.Text;

/**
 * PS:有 Gma.QrCodeNet的可以直接引入
 * 如果没有Gma.QrCodeNet的话,可以在vs的NuGet包管理器里面下载
 **/

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult About()
        {
            return View();
        }

        public ActionResult Excute()
        {
            string path = CreateQRCode("啦啦啦,啦啦啦,我是卖报的小行家", Server.MapPath("/Icon/lll_small.jpg"), 9);
            //string path = CreateQRCode("啦啦啦,啦啦啦,我是卖报的小行家",9);
            ViewBag.img = path;
            return View();
        }

        /// 
        /// 生成二维码
        /// 
        /// 内容
        /// 二维码大小
        /// 返回二维码图片路径
        public string CreateQRCode(string content, int moduleSize = 9)
        {
            /**
             * ErrorCorrectionLevel 误差校正水平
             * QuietZoneModules     空白区域
             **/

            var encoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode = encoder.Encode(content);

            GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);

            //MemoryStream memoryStream = new MemoryStream();
            //render.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, memoryStream);

            //生成图片的代码       
            DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
            Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
            Graphics g = Graphics.FromImage(map);
            render.Draw(g, qrCode.Matrix);
            string fileName = Guid.NewGuid().ToString().Replace("-", "") + "." + ImageFormat.Jpeg;
            string savePath = Server.MapPath(@"/Img/") + fileName;
            map.Save(savePath);

            return "/Img/" + fileName;
        }


        /// 
        /// 生成带logo的二维码
        /// 
        /// 内容
        /// logo路径
        /// 二维码大小
        /// 返回二维码图片路径
        public string CreateQRCode(string content, string logoPath, int moduleSize = 9)
        {
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode = qrEncoder.Encode(content);

            GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);

            DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
            Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
            Graphics g = Graphics.FromImage(map);
            render.Draw(g, qrCode.Matrix);

            /**
             * 添加Logo图片 ,注意控制Logo图片大小和二维码大小的比例
             *添加的图片过大超过二维码的容错率会导致信息丢失,无法被识别
             **/
            Image img = Image.FromFile(logoPath);

            Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2);
            g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height);

            string fileName = Guid.NewGuid().ToString().Replace("-", "") + "." + ImageFormat.Jpeg;
            string savePath = Server.MapPath(@"/Img/") + fileName;
            map.Save(savePath);
            return "/Img/" + fileName;
        }
    }
}
未带logo的二维码效果图:
.net中如何利用Gma.QrCodeNet生成二维码_第1张图片

带logo的二维码效果图:
.net中如何利用Gma.QrCodeNet生成二维码_第2张图片

你可能感兴趣的:(.net)