首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#运用GmaQrCode生成二维码

C#运用GmaQrCode生成二维码

作者头像
彭泽0902
发布2018-01-04 15:29:17
7650
发布2018-01-04 15:29:17
举报
文章被收录于专栏:C#C#

    项目中需要生成二维码,方法比较多,可以采用JS插件,也可以采用第三方插件后台生成二维码,在后台方法中可以采用QRCode或者GmaQrCode,现在介绍一种C#在后台生成二维码的方法:

        /// <summary>
        /// 获取二维码
        /// </summary>
        /// <param name="codeString">编码字符</param>
        /// <returns>二维码地址</returns>
        public static string ReturnGmaQrCode(string codeString)
        {
            try
            {
                using (var ms = new MemoryStream())
                {
                    //const string stringtest = "中国inghttp://www.baidu.com/mvc.test?&";
                    if (string.IsNullOrEmpty(codeString))
                    {
                        throw new ArgumentNullException(codeString);
                    }
                    var stringtest = codeString;
                    CreateGmaQrCodeHelper(stringtest, ms);
                    var img = Image.FromStream(ms);
                    var filename = DateTime.Now.ToString("yyyymmddhhmmss");
                    var path = HttpContext.Current.Server.MapPath("~/image/") + filename + ".png";
                    img.Save(path);
                    return path;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }

        /// <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 CreateGmaQrCodeHelper(string strContent, MemoryStream ms)
        {
            try
            {
                //误差校正水平
                const ErrorCorrectionLevel ecl = ErrorCorrectionLevel.M;
                //待编码内容
                var content = strContent;
                //空白区域
                const QuietZoneModules quietZones = QuietZoneModules.Two;
                //大小
                const 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;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return true;
        }

    在asp.net的项目中,将生成的二维码地址返回至当前方法。可以采用ashx文件加载图片文件,在页面中获取地址,可以在页面中显示二维码资质。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-10-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档