.net服务端生成二维码

mvc4   net4.0

1、引用附件的DLL文件

2、两个函数即可

#region 生成二维码
        public ActionResult getQrCode()
        {
            using (var ms = new MemoryStream())
            {
                string stringtest = "这里是二维码内容";
                GetQRCode1(stringtest, ms);
                Response.ContentType = "image/Png";
                Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length);
                Response.End();
            }

            return View();
        }

        public bool GetQRCode1(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;
        }
        #endregion

 点击下载附件

posted @ 2017-11-10 16:36  大鹅先生  阅读(484)  评论(0编辑  收藏  举报