技术思绪摘录旅行笔记
asp.net MVC中操作生成二维码,本案例中大致讲解了如何使用组件及其GDI+的相关知识,以及MVC中下载文件。

本示例中我使用的是Gma.QrCodeNet,先写个QRCodeHelper.cs

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

namespace SH.Web.Models
{
 
 // 含有QR码的描述类和包装编码和渲染
public class QRCodeHelper
 {
      public static bool GetQRCode(string strContent, MemoryStream ms, int size)
      {
           ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 
           string Content = strContent;//待编码内容
           QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 
           int ModuleSize = size;//大小
           var encoder = new QrEncoder(Ecl);
           QrCode qr;
           if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵
           {
                var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));
                render.WriteToStream(qr.Matrix, ImageFormat.Png, ms);
           }
           else
           {
                return false;
           }
                return true;
      }

      ///获取二维码并保存
      public static void GetQRCode(string name)
      {
           QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
           QrCode qrCode = qrEncoder.Encode(name);
           //保存成png文件
           if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("/Upload/QrCode/")))
           {
                System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/Upload/QrCode/"));//不存在就创建目录 
           }
           string filename = HttpContext.Current.Server.MapPath("/Upload/QrCode/"+name+".png");
           GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(20, QuietZoneModules.Two), Brushes.Black, Brushes.White);
           using (FileStream stream = new FileStream(filename, FileMode.Create))
           {
                render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
           }
      }


      //生成二维码保存并返回流
      public static MemoryStream GetQRCode(string content, string Name, int moduleSize = 20)
      {
           var encoder = new QrEncoder(ErrorCorrectionLevel.M);
           QrCode qrCode = encoder.Encode(content);
           GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);
           //从文件流保存
           if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("/Upload/QrCode/")))
           {
                System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/Upload/QrCode/"));//不存在就创建目录 
           }
           string filename = HttpContext.Current.Server.MapPath("/Upload/QrCode/" + Name + ".png");
           using (FileStream stream = new FileStream(filename, FileMode.Create))
           {
                render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream);
           }
           //返回文件流
           MemoryStream memoryStream = new MemoryStream();
           render.WriteToStream(qrCode.Matrix, ImageFormat.Png, memoryStream);
 
           return memoryStream;
      }


       
      //生成带logo的二维码保存并返回流 
      public static MemoryStream GetQRCodeLogo(string content, string Name, int moduleSize = 14)
      {

           var encoder = new QrEncoder(ErrorCorrectionLevel.M);
           QrCode qrCode = encoder.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);
           Image img = Image.FromFile(HttpContext.Current.Server.MapPath(PageParam.LogoPath+PageNetInfo.NetLogo));
           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);
           MemoryStream ms = new MemoryStream();
           map.Save(ms, ImageFormat.Png);
           if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("/Upload/QrCode/")))
           {
                System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/Upload/QrCode/"));//不存在就创建目录 
           }
           map.Save(HttpContext.Current.Server.MapPath("/Upload/QrCode/" + Name + ".png"), ImageFormat.Png);
           return ms;
      }


 }
}

以上就是操作类,现在开始写调用

1.返回图片文件流代码:显示图片

控制器中的文件视图调用如下:

// 二维码输出
public FileContentResult GetQrCode()
{
     MemoryStream ms = QRCodeHelper.GetQRCodeLogo(string content, string name);
     return File(ms.ToArray(), "image/Png");
}

前台html展示图片调用

 <img src="/Home/GetQrCode"/>

2.返回图片文件:下载图片

 //图片下载
 public FilePathResult DownFileQrCode()
 {
     var path = Server.MapPath("/Upload/QrCode/001.png");
     var name = Path.GetFileName(path);
     return File(path, "application/octet-stream", name);
 }

这里只返回本地存在的图片,其他需求另说

前台调用:

 <a href="/Home/DownFileQrCode">下载</a>

基本用法就介绍完毕了,大神们如果有更好的方法显示和下载请联系我。


CarsonIT 微信扫码关注公众号 策略、创意、技术

留下您的脚步

 

最近评论

查看更多>>

站点统计

总文章数:275 总分类数:18 总评论数:88 总浏览数:124.78万

精选推荐

阅读排行

友情打赏

请打开您的微信,扫一扫