二维码打包下载

using QRCoder;

using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.IO;

namespace SIP.Common

{

    /// <summary>

    /// 二维码

    /// </summary>

    public static class QrCodeQrCodeHelper

    {

        /// <summary>

        /// 生成二维码

        /// </summary>

        /// <param name="data"></param>

        /// <returns></returns>

        public static Image CreteQr(string data)

        {

            QRCodeGenerator qrGenerator = new QRCodeGenerator();

            QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q);

            QRCode qrCode = new QRCode(qrCodeData);

            Bitmap qrCodeImage = qrCode.GetGraphic(20);

            return qrCodeImage;

        }

        /// <summary>

        ///

        /// </summary>

        /// <param name="qr"></param>

        /// <param name="front"></param>

        /// <param name="botton"></param>

        /// <returns></returns>

        public static Image AddName(Image qr, string front, string botton)

        {

            Graphics g = Graphics.FromImage(qr);

            if (!string.IsNullOrWhiteSpace(front))

            {

                Font fontf = new Font("黑体", 40);

                SolidBrush sbrushf = new SolidBrush(Color.Black);

                g.DrawString(front, fontf, sbrushf, new Point(335, 25));

                MemoryStream msf = new MemoryStream();

                qr.Save(msf, ImageFormat.Bmp);

            }

            Font font = new Font("黑体", 20);

            SolidBrush sbrush = new SolidBrush(Color.Black);

            int height = qr.Height - 80 + 10;

            int index = 1;

            int count = 15;

            while (true)

            {

                int length = botton.Length < index * count ? botton.Length - (index - 1) * count : count;

                string str = botton.Substring((index - 1) * count, length);

                g.DrawString(str, font, sbrush, new Point(150, height + ((index - 1) * 25)));

                MemoryStream ms = new MemoryStream();

                qr.Save(ms, ImageFormat.Bmp);

                if (botton.Length < index * count)

                    break;

                index++;

            }

            return qr;

        }

        /// <summary>

        /// 获取二维码图片

        /// </summary>

        /// <param name="data">数据</param>

        /// <param name="name">二维码名称</param>

        /// <returns></returns>

        public static Image GetQr(string data, string front, string bottom)

        {

            Image qr = CreteQr(data);

            qr = AddName(qr, front, bottom);

            return qr;

        }

    }

}


//调用的地方

string filename = DateTime.Now.ToString("yyyyMMddHHMssffff");

                string filepath = FileHelper.GetAbsolutePath("/temp/qrcode/" + filename + "/");

                if (Directory.Exists(filepath))

                {

                    Directory.Delete(filepath);

                }

                else

                {

                    Directory.CreateDirectory(filepath);

                }

                var list = _iEquipmentService.GetPageAdmin(view, out total).ToList();

                for (int i = 0; i < list.Count; i++)

                {

                    var p = list[i];

                    var img = QrCodeQrCodeHelper.GetQr(p.Code, p.AssetCode, p.Name);

                    img.Save(filepath + p.AssetCode + ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);

                }

                string fn = filepath + filename + ".zip";

                DotNetZipHelper.ZipFolder(filepath, fn, "设备二维码", "设备二维码");

                //下载文件           

                FileDown d = new FileDown();

                d.DownLoad(fn);

                //删除文件

                System.IO.FileInfo file = new System.IO.FileInfo(fn);

                if (file.Exists)

                    file.Delete();

                return Json(new { bl = true });

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容