程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> MVC中使用QrCodeNet 生成二維碼,mvcqrcodenet生成

MVC中使用QrCodeNet 生成二維碼,mvcqrcodenet生成

編輯:C#入門知識

MVC中使用QrCodeNet 生成二維碼,mvcqrcodenet生成


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 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%23";
 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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved