C# 生成二维码

1. 安装nuget package QRCoder:




2. c#
  
public JsonResult RenderQrCode(string content)
        {
            try
            {
                RenderQrCodeResponse response = new RenderQrCodeResponse();


                QRCodeGenerator.ECCLevel eccLevel = QRCodeGenerator.ECCLevel.H;
                using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
                {
                    using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, eccLevel))
                    {
                        var folder = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "QRCodes");
                        if (!Directory.Exists(folder))
                        {
                            Directory.CreateDirectory(folder);
                        }


                        QRCode qrCode = new QRCode(qrCodeData);
                        Image image = qrCode.GetGraphic(10);
                        string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
                        string path = new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase) + "QRCodes\\" + fileName;
                        FileStream fs = new FileStream(path, FileMode.Create);


                        image.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
                        fs.Close();


                        response.imageUrl = "QRCodes/" + fileName;
                    }
                }


                return Json(response, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
            
        }




可调整这行代码的参数来改变qrcode大小
Image image = qrCode.GetGraphic(10);






你可能感兴趣的:(c#,编程,ASP.NET,MVC)