根据模板生成图片二维码和打包zip

根据模板生成图片二维码和打包zip

dll下载链接

        public ActionResult Index()
        {
            List<string> OBJ = new List<string>();
            for (int i = 0; i < 10; i++)
            {
                string Qrpath = SendQrCode("/Images/QrCode1.png", "http://www.baidu.com", DateTime.Now.ToString("HHmmssfff") + ".png", 690, 690, 202, 507, "门店名称:", "商户编号", "联系电话");
                OBJ.Add(Qrpath);
            }
            string address = Zip1(OBJ, "/Upload/" + 123 + ".zip", string.Empty);
            return View();
        }
        #region 根据模板生成二维码
        public string SendQrCode(string BackGroundImagePath, string QrCodeStr, string ImageFileName, int imgWidth, int imgHeight, int paddLeft, int paddTop, string MerchantName, string MerchantNoString, string MerchantLink)
        {
            string savepath = "/Upload/QrCode/";//图片保存路径

            if (!System.IO.Directory.Exists(Server.MapPath(savepath)))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(savepath));
            }
            Image NowImage;
            if (string.IsNullOrEmpty(BackGroundImagePath))
            {
                NowImage = GCode(QrCodeStr);
            }
            else
            {
                NowImage = CombinImage3(Server.MapPath(BackGroundImagePath), GCode(QrCodeStr), imgWidth, imgHeight, paddLeft, paddTop, MerchantName, MerchantNoString, MerchantLink); ;//参数说明:原图保存路径,二维码地址,生成图片的宽度,生成图片的高度,居左宽度,居顶高度  
            }
            NowImage.Save(Server.MapPath(savepath + ImageFileName));
            return "/Upload/QrCode/" + ImageFileName;
        }
        #endregion

        #region 生成二维码
        /// <summary>
        /// 生成二维码.
        /// </summary>
        /// <param name="data">需要添加进去的文本</param>
        /// <returns></returns>
        public static System.Drawing.Image GCode(String data)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;

            qrCodeEncoder.QRCodeScale = 5;
            qrCodeEncoder.QRCodeVersion = 7;

            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
            var pbImg = qrCodeEncoder.Encode(data, System.Text.Encoding.UTF8);
            var width = pbImg.Width / 10;
            var dwidth = width * 2;
            Bitmap bmp = new Bitmap(pbImg.Width + dwidth, pbImg.Height + dwidth);
            Graphics g = Graphics.FromImage(bmp);
            var c = System.Drawing.Color.White;
            g.FillRectangle(new SolidBrush(c), 0, 0, pbImg.Width + dwidth, pbImg.Height + dwidth);
            g.DrawImage(pbImg, width, width);
            g.Dispose();
            return bmp;
        }


        /// <summary>
        /// 调用此函数后使此两种图片合并,类似相册,有个
        /// 背景图,中间贴自己的目标图片
        /// </summary>
        /// <param name="sourceImg">粘贴的源图片</param>
        /// <param name="destImg">粘贴的目标图片</param>
        /// string destImgPath = "/Areas/Admin/Content/images/QrCodeBackground.png";
        public static System.Drawing.Image CombinImage3(string imgBack, System.Drawing.Image destImg, int newW, int newH, int paddLeft, int paddTop, string MerchantName, string MerchantNoString, string MerchantLink)
        {

            System.Drawing.Image img = destImg;      //照片图片  
            if (img.Height != 100 || img.Width != 100)
            {
                //设置图片尺寸
                img = KiResizeImage(img, newW, newH, 0);
            }

            if (imgBack == null)
            {
                return null;
            }
            System.Drawing.Image nowImgbakc = System.Drawing.Image.FromFile(imgBack);

            if (IsPixelFormatIndexed(nowImgbakc.PixelFormat))
            {
                Bitmap bmp = new Bitmap(nowImgbakc.Width, nowImgbakc.Height, PixelFormat.Format32bppArgb);
                Graphics gr = Graphics.FromImage(bmp);
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gr.DrawImage(nowImgbakc, 0, 0, nowImgbakc.Width, nowImgbakc.Height);      //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高); 
                //g.DrawImage(imgBack, 距离左边的宽度, 距离上边的高度, 相框宽, 相框高);
                gr.DrawImage(img, paddLeft, paddTop, img.Width, img.Height);

                gr.Flush();
                GC.Collect();
                return bmp;
            }
            else
            {
                Bitmap bmp = new Bitmap(nowImgbakc.Width, nowImgbakc.Height, PixelFormat.Format32bppArgb);

                Graphics gr = Graphics.FromImage(bmp);


                gr.DrawImage(nowImgbakc, 0, 0, nowImgbakc.Width, nowImgbakc.Height);

                覆盖图片
                gr.DrawImage(img, paddLeft, paddTop, img.Width, img.Height);//二维码

                //插入文字
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                int size = 0;
                int soure = 0;

                if (nowImgbakc.Width > 1000)
                {
                    soure = 25;
                    size = 20;
                }
                else
                {
                    soure = 25;
                    size = 18;
                }


                int ContentWidht = MerchantName.Length * 40;//内容宽度
                int FontBackgroundWidth = ContentWidht;//

                int MerchantNoContentWidht = MerchantNoString.Length * soure;//内容宽度
                int MerchantNoFontBackgroundWidth = MerchantNoContentWidht;//

                int MerchantLinkContentWidht = MerchantLink.Length * soure;//内容宽度
                int MerchantLinkFontBackgroundWidth = MerchantLinkContentWidht;//

                Color color = System.Drawing.ColorTranslator.FromHtml("#0080ff");
                Brush brush = new SolidBrush(color);

                gr.DrawString(MerchantName, new Font("微软雅黑", 30, FontStyle.Regular),
                    brush, new Rectangle(nowImgbakc.Width / 2 - (FontBackgroundWidth / 2), newH - 230, nowImgbakc.Width, 40), sf);
                gr.DrawString(MerchantNoString, new Font("微软雅黑", 20, FontStyle.Regular),
                   Brushes.Black, new Rectangle(100, newH - 190, nowImgbakc.Width, 1543), sf);
                gr.DrawString(MerchantLink, new Font("微软雅黑", 20, FontStyle.Regular),
                   Brushes.Black, new Rectangle(650, newH - 190, nowImgbakc.Width, 1543), sf);
                gr.Flush();
                GC.Collect();
                return bmp;
            }
        }

        /// <summary>
        /// Resize图片
        /// </summary>
        /// <param name="bmp">原始Bitmap</param>
        /// <param name="newW">新的宽度</param>
        /// <param name="newH">新的高度</param>
        /// <param name="Mode">保留着,暂时未用</param>
        /// <returns>处理以后的图片</returns>
        public static System.Drawing.Image KiResizeImage(System.Drawing.Image bmp, int newW, int newH, int Mode)
        {
            try
            {
                System.Drawing.Image b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);

                // 插值算法的质量
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();


                return b;
            }
            catch
            {
                return null;
            }

        }

        /// <summary>
        /// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
        /// </summary>
        /// <param name="imgPixelFormat">原图片的PixelFormat</param>
        /// <returns></returns>
        private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
        {
            foreach (PixelFormat pf in indexedPixelFormats)
            {
                if (pf.Equals(imgPixelFormat)) return true;
            }

            return false;
        }

        /// <summary>
        /// 会产生graphics异常的PixelFormat
        /// </summary>
        private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
        PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
        PixelFormat.Format8bppIndexed
            };
        #endregion

        #region 压缩多个文件
        /// <summary>
        ///  压缩多个文件
        /// </summary>
        /// <param name="files">文件名</param>
        /// <param name="ZipedFileName">压缩包文件名</param>
        /// <param name="Password">解压码</param>
        /// <returns></returns>
        public string Zip1(List<string> files, string ZipedFileName, string Password)
        {
            if (files.Count == 0) throw new FileNotFoundException("未找到指定打包的文件");
            ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(Server.MapPath(ZipedFileName)));
            s.SetLevel(6);
            if (!string.IsNullOrEmpty(Password.Trim())) s.Password = Password.Trim();
            ZipFileDictory(files, s);
            s.Finish();
            s.Close();
            return ZipedFileName;
        }

        private void ZipFileDictory(List<string> files, ZipOutputStream s)
        {
            ZipEntry entry = null;
            FileStream fs = null;
            Crc32 crc = new Crc32();
            try
            {
                //创建当前文件夹
                entry = new ZipEntry("/");  //加上 “/” 才会当成是文件夹创建
                s.PutNextEntry(entry);
                s.Flush();
                foreach (string file in files)
                {
                    //打开压缩文件
                    fs = System.IO.File.OpenRead(Server.MapPath(file));

                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    entry = new ZipEntry("/" + Path.GetFileName(file));
                    entry.DateTime = DateTime.Now;
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
                    s.Write(buffer, 0, buffer.Length);
                }
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs = null;
                }
                if (entry != null)
                    entry = null;
                GC.Collect();
            }
        }
        #endregion
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伴之则安博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值