二维码打包下载

using QRCoder;

using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.IO;

namespace SIP.Common

{

    ///

    /// 二维码

    ///

    public static class QrCodeQrCodeHelper

    {

        ///

        /// 生成二维码

        ///

        ///

        ///

        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;

        }

        ///

        ///

        ///

        ///

        ///

        ///

        ///

        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;

        }

        ///

        /// 获取二维码图片

        ///

        /// 数据

        /// 二维码名称

        ///

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

你可能感兴趣的:(二维码打包下载)