MVC中使用QrCodeNet 生成二维码

QrCodeNet下载地址:http://qrcodenet.codeplex.com/

 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 QRCodeDemo
 8 {
 9     public class QRCodeHelper
10     {
11         ///   
12         /// 生成二维码  
13         ///   
14         /// 内容
15         /// 二维码的大小
16         /// 输出流  
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         /// 
40         /// 生成带Logo二维码  
41         /// 
42         /// 内容
43         /// logo路径
44         /// 二维码的大小
45         /// 输出流
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%23";
 4        using (var memoryStream = QRCodeHelper.GetQRCode(str))
 5        {
 6              Response.ContentType = "image/jpeg";
 7              Response.OutputStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
 8              Response.End();
 9        }
10        return null;
11 }

View:

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

效果如下:

MVC中使用QrCodeNet 生成二维码_第1张图片

转载于:https://www.cnblogs.com/zlen/p/5765063.html

你可能感兴趣的:(MVC中使用QrCodeNet 生成二维码)