利用QrCode.Net生成二维码 asp.net mvc c#

自己参考QrCode.Net开源实例封装了一个生成二维码的静态方法如下:

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /// <summary>  
  2.     /// 含有QR码的描述类和包装编码和渲染  
  3.     /// </summary>  
  4.     public class QRCodeHelper  
  5.     {  
  6.         /// <summary>  
  7.         /// 获取二维码  
  8.         /// </summary>  
  9.         /// <param name="strContent">待编码的字符</param>  
  10.         /// <param name="ms">输出流</param>  
  11.         ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>  
  12.         public static bool GetQRCode(string strContent, MemoryStream ms)  
  13.         {  
  14.             ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平   
  15.             string Content = strContent;//待编码内容  
  16.             QuietZoneModules QuietZones = QuietZoneModules.Two;  //空白区域   
  17.             int ModuleSize = 12;//大小  
  18.             var encoder = new QrEncoder(Ecl);  
  19.             QrCode qr;  
  20.             if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵  
  21.             {  
  22.                 var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));  
  23.                 render.WriteToStream(qr.Matrix, ImageFormat.Png, ms);  
  24.             }  
  25.             else  
  26.             {  
  27.                 return false;  
  28.             }   
  29.             return true;  
  30.         }   
  31.   
  32.     }  


vs2010  framework3.5  引用的组件是QrCode.Net 0.4 Pre-Release\Gma.QrCodeNet.Encoding.Net35 

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public ActionResult Index()  
  2.       {  
  3.              
  4.           // Render the QR code as an image  
  5.           using (var ms = new MemoryStream())  
  6.           {  
  7.               string stringtest="中国inghttp://www.baidu.com/mvc.test?&";  
  8.               QRCodeHelper.GetQRCode(stringtest, ms);  
  9.               Response.ContentType = "image/Png";  
  10.               Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length);   
  11.               Response.End();  
  12.           }  
  13.              
  14.           return View();  
  15.       }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值