当前位置:新注册送38元体验金 > 新注册送38元体验金编程 > MVC中使用QrCodeNet 生成二维码,mvcqrcodenet生成

MVC中使用QrCodeNet 生成二维码,mvcqrcodenet生成

文章作者:新注册送38元体验金编程 上传时间:2019-08-22

MVC中使用QrCodeNet 生成二维码,mvcqrcodenet生成

QrCodeNet下载地址:

 1 using System.Drawing;
 2 using System.Drawing.Imaging;
 3 using System.IO;
 4 using Gma.QrCodeNet.Encoding;
 5 using Gma.QrCodeNet.Encoding.Windows.Render;
 6 
 7 namespace QRCodeTest
 8 {
 9     public class QRCodeHelper
10     {
11         /// <summary>  
12         /// 生成二维码  
13         /// </summary>  
14         /// <param name="content">内容</param>
15         /// <param name="moduleSize">二维码的大小</param>
16         /// <returns>输出流</returns>  
17         public static MemoryStream GetQRCode(string content, int moduleSize = 9)
18         {
19             //ErrorCorrectionLevel 误差校正水平
20             //QuietZoneModules     空白区域
21 
22             var encoder = new QrEncoder(ErrorCorrectionLevel.M);
23             QrCode qrCode = encoder.Encode(content);
24             GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two) , Brushes.Black, Brushes.White);
25 
26             MemoryStream memoryStream = new MemoryStream();
27             render.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, memoryStream);
28             
29             return memoryStream;
30 
31             //生成图片的代码
32             //DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
33             //Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
34             //Graphics g = Graphics.FromImage(map);
35             //render.Draw(g, qrCode.Matrix);
36             //map.Save(fileName, ImageFormat.Jpeg);//fileName为存放的图片路径
37         }
38 
39         /// <summary>
40         /// 生成带Logo二维码  
41         /// </summary>
42         /// <param name="content">内容</param>
43         /// <param name="iconPath">logo路径</param>
44         /// <param name="moduleSize">二维码的大小</param>
45         /// <returns>输出流</returns>
46         public static MemoryStream GetQRCode(string content, string iconPath, int moduleSize = 9)
47         {
48             QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
49             QrCode qrCode = qrEncoder.Encode(content);
50             
51             GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White);
52 
53             DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
54             Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
55             Graphics g = Graphics.FromImage(map);
56             render.Draw(g, qrCode.Matrix);
57 
58             //追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例
59             //PS:追加的图片过大超过二维码的容错率会导致信息丢失,无法被识别
60             Image img = Image.FromFile(iconPath);
61 
62             Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2);
63             g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height);
64 
65             MemoryStream memoryStream = new MemoryStream();
66             map.Save(memoryStream, ImageFormat.Jpeg);
67 
68             return memoryStream;
69 
70             //生成图片的代码: map.Save(fileName, ImageFormat.Jpeg);//fileName为存放的图片路径
71         }
72     }
73 }

 

Controller:

 1 public ActionResult CreateQrCode()
 2  {
 3        string str = "http://cn.bing.com/search?q=C#";
 4        using (var memoryStream = QRCodeHelper.GetQRCode(str, Server.MapPath(@"~/images/logo.jpg")))
 5        {
 6              Response.ContentType = "image/jpeg";
 7              Response.OutputStream.Write(memoryStream.GetBuffer(), 0, (int)ms.Length);
 8              Response.End();
 9        }
10        return null;
11 }

View:

1 <img id="qrcode" src="@Url.Action("createqrcode")"  />

效果如下:

图片 1

生成二维码,mvcqrcodenet生成 QrCodeNet下载地址: 1 using System.Drawing; 2 using System.Drawing.Imaging; 3 using Syst...

本文由新注册送38元体验金发布于新注册送38元体验金编程,转载请注明出处:MVC中使用QrCodeNet 生成二维码,mvcqrcodenet生成

关键词: