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

本文介绍了如何在.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();
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="moduleSize">二维码大小</param>
        /// <returns>返回二维码图片路径</returns>
        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;
        }


        /// <summary>
        /// 生成带logo的二维码
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="iconPath">logo路径</param>
        /// <param name="moduleSize">二维码大小</param>
        /// <returns>返回二维码图片路径</returns>
        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的二维码效果图:

带logo的二维码效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值