使用Image Sharp 、QRCoder对二维码图片中间加入logo图片

方式一:Image Sharp

1.在NuGet包源加入Image Sharp包源

2.在需要用到Image Sharp的项目中安装以下的插件

3.调用图片上绘制图片的方法

 方式二:QRCoder

1.引入QRCoder包

 代码:

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="qrCodeContent">二维码内容</param>
        /// <param name="filePath">二维码保存地址</param>
        /// <param name="fileName">二维码文件名</param>
        public string GenerateQrCode(string qrCodeContent, string filePath, string fileName)
        {
            fileName = fileName.Replace("/", "_");
            fileName = fileName.Replace(":", "_");
            fileName = fileName.Replace("\\", "_");
            fileName = fileName.Replace("*", "_");
            fileName = fileName.Replace('"', '_');
            fileName = fileName.Replace(">", "_");
            fileName = fileName.Replace("<", "_");
            fileName = fileName.Replace("|", "_");
            fileName = fileName.Replace("?", "_");

            string webRootPath = _hostingEnvironment.WebRootPath;
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            //QRCodeGenerator.ECCLevel:纠错能力,Q级:约可纠错25 % 的数据码字
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrCodeContent, QRCodeGenerator.ECCLevel.Q);
            var aa = qrCodeData.Version;
            QRCode qrCode = new QRCode(qrCodeData);
            System.Drawing.Bitmap icon = new System.Drawing.Bitmap(webRootPath + "/logo/logo.jpg");
            System.Drawing.Bitmap qrCodeImage = qrCode.GetGraphic(15, System.Drawing.Color.Black, System.Drawing.Color.White, icon);

            if (!Directory.Exists(webRootPath + filePath))
            {
                Directory.CreateDirectory(webRootPath + filePath);
            }
            var imgePath = webRootPath + filePath + fileName + ".jpg";
            qrCodeImage.Save(imgePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            //var imgPath = path.Substring(webRootPath.Length);
            return filePath + fileName + ".jpg";
}

 

posted @ 2018-07-09 11:04  流年sugar  阅读(427)  评论(0编辑  收藏  举报