zoukankan      html  css  js  c++  java
  • asp.net mvc 生成二维码

    生成二维码,帮助类:

    using Gma.QrCodeNet.Encoding;
    using Gma.QrCodeNet.Encoding.Windows.Render;
    using System;
    using System.Collections.Generic;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Piano.Utility.Common
    {
       public  class QRCodeHelper
        {
            /// <summary>
            /// 获取二维码
            /// </summary>
            /// <param name="strContent">待编码的字符</param>
            /// <param name="ms">输出流</param>
            ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>
            public static bool GetQRCode(string strContent, MemoryStream ms)
            {
                ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 
                string Content = strContent;//待编码内容
                QuietZoneModules QuietZones = QuietZoneModules.Two;  //空白区域 
                int ModuleSize = 12;//大小
                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;
            } 
        }
    }

    使用方法(此处是MVC5.2.0模式下):

     //二维码生成
            public ActionResult getQrCode()
            {
    
                // Render the QR code as an image
                using (var ms = new MemoryStream())
                {
                    string url = string.Format(ConfigurationManager.AppSettings["WxgzhUrl"], UserValidator.GetInstituteId());
                    string stringtest = url;
                    QRCodeHelper.GetQRCode(stringtest, ms);
                    Response.ContentType = "image/Png";
                    Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length);
                    Response.End();
                }
    
                return View();
            }

    前端调用:

     <img src="/My/getQrCode" alt="" style="160px;height:160px;" /><span class="remindmsg">扫一扫查看</span>
  • 相关阅读:
    leetcode-----16. 最接近的三数之和
    leetcode-----15. 三数之和
    leetcode-----14. 最长公共前缀
    leetcode-----13. 罗马数字转整数
    leetcode-----12. 整数转罗马数字
    leetcode-----11. 盛最多水的容器
    leetcode-----10. 正则表达式匹配
    leetcode-----9. 回文数
    leetcode-----8. 字符串转换整数 (atoi)
    leetcode-----7. 整数反转
  • 原文地址:https://www.cnblogs.com/MissQing/p/6477742.html
Copyright © 2011-2022 走看看