1. 程式人生 > >.net服務端生成二維碼

.net服務端生成二維碼

int get tps tty pan level matrix span odi

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

點擊下載附件

.net服務端生成二維碼