C# 生成二维码


1. 安装nuget package QRCoder:
<package id="QRCoder" version="1.3.1" targetFramework="net45" />




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);






智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告