二维码q

 public class QRCodeHelper
    {
        /// <summary>
        /// 生成二维码图片
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public Bitmap CreateQRimg(string str)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(str, QRCodeGenerator.ECCLevel.Q);
            QRCoder.QRCode qrCode = new QRCoder.QRCode(qrCodeData);
            Bitmap bt = qrCode.GetGraphic(20);

            return bt;
        }
        public Bitmap CreateQRimg(string str, Color darkColor, Color lightColor)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(str, QRCodeGenerator.ECCLevel.Q);
            QRCoder.QRCode qrCode = new QRCoder.QRCode(qrCodeData);
            Bitmap bt = qrCode.GetGraphic(3, darkColor, lightColor,null);
            return bt;
        }
        /// <summary>
        /// 生成二维码图片流(二维码上含文字)
        /// </summary>
        /// <param name="str1">二维码中要传递的数据(地址)</param>
        /// <param name="str2">二维码上显示的文字说明</param>
        public byte[] GenerateQRCode(string str1, string str2)
        {
            using (Image codeImage = CreateQRimg(str1), strImage = ConvertStringToImage(str2))
            {
                Image img = CombineImage(600, 600, codeImage, 60, 50, strImage, 0, 530);
                using (var stream = new MemoryStream())
                {
                    img.Save(stream, ImageFormat.Jpeg);
                    //输出图片流
                    return stream.ToArray();
                }
            }
        }

        /// <summary>
        /// 生成二维码图片流(不含文字)
        /// </summary>
        /// <param name="str">二维码中要传递的数据(地址)</param>
        /// <returns></returns>
        public byte[] GenerateQRCode(string str)
        {
            using (Image codeImage = CreateQRimg(str))
            {
                using (var stream = new MemoryStream())
                {
                    codeImage.Save(stream, ImageFormat.Jpeg);

                    return stream.ToArray();
                }
            }
        }

        /// <summary>
        /// 生成文字图片
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public Image ConvertStringToImage(string str)
        {
            Bitmap image = new Bitmap(600, 40, PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(image);

            try
            {
                Font font = new Font("SimHei", 14, FontStyle.Regular);

                g.Clear(Color.White);

                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                Rectangle rectangle = new Rectangle(0, 0, 600, 40);

                g.DrawString(str, font, new SolidBrush(Color.Black), rectangle, format);

                return image;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                GC.Collect();
            }
        }

        /// <summary>
        /// 在画板中合并二维码图片和文字图片
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="imgLeft"></param>
        /// <param name="imgLeft_left"></param>
        /// <param name="imgLeft_top"></param>
        /// <param name="imgRight"></param>
        /// <param name="imgRight_left"></param>
        /// <param name="imgRight_top"></param>
        /// <returns></returns>
        public Image CombineImage(int width, int height, Image imgLeft, int imgLeft_left, int imgLeft_top, Image imgRight, int imgRight_left, int imgRight_top)
        {
            Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(image);

            try
            {
                g.Clear(Color.White);
                g.DrawImage(imgLeft, imgLeft_left, imgLeft_top, 500, 500);
                g.DrawImage(imgRight, imgRight_left, imgRight_top, imgRight.Width, imgRight.Height);

                return image;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                g.Dispose();
            }
        }
    }
var qrCode = new QRCodeHelper();

                var image = qrCode.GenerateQRCode(appSections["ClassEvaluate:Default"] + "?gId=" + m.d.Id + "&cId=" + m.c.Id + "&tId=" + input.Id);
                string filename = "\\" + m.d.Name + " " + m.c.ClassName + "【" + input.TaskName + "(" + input.BeginDate.ToString("yyyyMMdd") + " - " + input.EndDate.ToString("yyyyMMdd") + ")】.jpg";
                string path = Path.GetDirectoryName(_iHostEnvironment.WebRootPath + "\\任务\\"
                             + input.TaskName + "(" + input.BeginDate.ToString("yyyyMMdd") + "-" + input.EndDate.ToString("yyyyMMdd") + ")_任务二维码\\"
                             + input.TaskName + "(" + input.BeginDate.ToString("yyyyMMdd") + "-" + input.EndDate.ToString("yyyyMMdd") + ")\\"
                             + m.d.Name + "\\");

                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);
                if (!File.Exists(path + filename))
                {
                    using (FileStream fs = System.IO.File.Create(path + filename, image.Length))
                    {
                        fs.Write(image, 0, image.Length);
                        fs.Flush();
                    }
                }
posted @ 2021-08-16 08:39  cv玲玲  阅读(50)  评论(0编辑  收藏  举报