C#生成推广码(二维码+背景图片)

 public static string SCZFM(string id, string name)
        {
            string pathewm = System.Web.HttpContext.Current.Server.MapPath("EWM");
            string pp = pathewm + "\\" + id + ".png";
            if (File.Exists(@pp))
            {
                return id;
            }

            string des = "http://xxxx" + id;//二维码解析后的地址
            int size = 200;

            string path = System.Web.HttpContext.Current.Server.MapPath("EWM2");
            string file = path + "\\" + id + ".png";
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            
            //qrCodeEncoder.QRCodeVersion = 15;
            qrCodeEncoder.QRCodeScale = 4;
            System.Drawing.Image image = qrCodeEncoder.Encode(des);
            if (size > 0)
            {
                //当设定目标图片尺寸大于生成的尺寸时,逐步增大方格尺寸
                while (image.Width < size)
                {
                    qrCodeEncoder.QRCodeScale++;
                    System.Drawing.Image imageNew = qrCodeEncoder.Encode(des);
                    if (imageNew.Width < size)
                    {
                        image = new System.Drawing.Bitmap(imageNew);
                        imageNew.Dispose();
                        imageNew = null;
                    }
                    else
                    {
                        qrCodeEncoder.QRCodeScale--; //新尺寸未采用,恢复最终使用的尺寸
                        imageNew.Dispose();
                        imageNew = null;
                        break;
                    }
                }


                //当设定目标图片尺寸小于生成的尺寸时,逐步减小方格尺寸

                while (image.Width > size && qrCodeEncoder.QRCodeScale > 1)
                {
                    qrCodeEncoder.QRCodeScale--;
                    System.Drawing.Image imageNew = qrCodeEncoder.Encode(des);
                    image = new System.Drawing.Bitmap(imageNew);
                    imageNew.Dispose();
                    imageNew = null;
                    if (image.Width < size)
                    {
                        break;

                    }
                }

                //生成图像
                //Bitmap image = qrCodeEncoder.Encode(des, Encoding.Default);
                //将image保存到filename
                image.Save(file);

                int W = 750;
                int H = 1334;
                string filePath = HttpContext.Current.Server.MapPath("/wemmain.jpg");
                Bitmap map = new Bitmap(filePath);
                Bitmap bmp = new Bitmap(map, W, H);

                Graphics g = Graphics.FromImage(bmp);
                //g.DrawImage(map, 0, 0, 800, 1134);

                string txfile = HttpContext.Current.Server.MapPath("/EWM2/" + id + ".png");
                Bitmap txmap = new Bitmap(txfile);
                g.DrawImage(txmap, 291, 634, 167, 167);


                System.Drawing.Font f2 = new Font("黑体", 20);
                System.Drawing.Brush b2 = new SolidBrush(Color.White);
                string tels = id.Substring(0, 3) + "****" + id.Substring(7, 4);
                g.DrawString("文字:" + tels, f2, b2, 263, 1183);
                g.DrawString("---  文字:" + name + "  ---", f2, b2, 187, 1229);

                string Pic_Path = HttpContext.Current.Server.MapPath("/EWM/" + id + ".png");
                bmp.Save(Pic_Path, System.Drawing.Imaging.ImageFormat.Jpeg);
                g.Dispose();
                map.Dispose();
                bmp.Dispose();

            }
            return id;
        }
c#
73 views
Comments
登录后评论
Sign In