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的二维码效果图:

本文介绍了如何在.NET环境中利用Gma.QrCodeNet库高效生成二维码,包括安装库、创建二维码实例、设置数据和错误校正级别等步骤。
5117

被折叠的 条评论
为什么被折叠?



