Пример #1
0
        /// <summary>
        /// 生成海报方法
        /// </summary>
        /// <param name="model">海报内容model</param>
        /// <returns>返回海报图片地址(本地)</returns>
        public static string MergePosterPersonalImage(DrawingPictureProsonal model)
        {
            int resize = 3;

            // 初始化背景图片的宽高
            Bitmap bitMap = new Bitmap(303 * resize, 452 * resize);
            // 初始化画板
            Graphics g1 = Graphics.FromImage(bitMap);

            ////设置画布背景颜色为白色
            g1.FillRectangle(Brushes.White, new Rectangle(0, 0, bitMap.Width, bitMap.Height));

            //设置背景图
            Image imgBg, imgAvatar, imgQrCode;

            try
            {
                imgBg = DrawingPictures.DownloadImg(model.BgImage);
            }
            catch (Exception ex)
            {
                Comm.WriteLog("MergePosterPersonalImage", "背景图下载失败", DebugLogLevel.Warning, ex: ex);
                throw new Exception("背景图下载失败");
            }
            try
            {
                imgAvatar = DrawingPictures.CutEllipse(DrawingPictures.DownloadImg(model.Avatar));
            }
            catch (Exception ex)
            {
                Comm.WriteLog("MergePosterPersonalImage", "头像图下载失败", DebugLogLevel.Warning, ex: ex);
                throw new Exception("头像图下载失败");
            }
            try
            {
                imgQrCode = DrawingPictures.CutEllipse(DrawingPictures.DownloadImg(model.QrCode));
            }
            catch (Exception ex)
            {
                Comm.WriteLog("MergePosterPersonalImage", "头像图下载失败", DebugLogLevel.Warning, ex: ex);
                throw new Exception("二维码图下载失败");
            }

            g1.DrawImage(imgBg, new Rectangle(0, 0, bitMap.Width, 380 * resize));
            if (!string.IsNullOrWhiteSpace(model.Avatar))
            {
                //拼接头像图片
                int size = 48 * resize;
                imgAvatar        = DrawingPictures.CutEllipse(DrawingPictures.DownloadImg(model.Avatar));
                g1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g1.DrawImage(imgAvatar, 8 * resize, 390 * resize, 48 * resize, 48 * resize);
            }

            if (!string.IsNullOrWhiteSpace(model.QrCode))
            {
                //拼接二维码之前画一个白色的圆底
                var       padding    = 3 * resize;
                var       qrCodeSize = 56 * resize;
                int       xQrCode    = 243 * resize;
                int       yQrCode    = 388 * resize;
                Rectangle rect       = new Rectangle(xQrCode - padding, yQrCode - padding, qrCodeSize + (padding * 2), qrCodeSize + (padding * 2));
                Brush     brush      = new SolidBrush(Color.White);
                g1.FillEllipse(brush, rect);

                //拼接二维码图片
                imgQrCode = DrawingPictures.DownloadImg(model.QrCode);

                g1.DrawImage(imgQrCode, new Rectangle(xQrCode, yQrCode, qrCodeSize, qrCodeSize));
            }

            //姓名
            int   fontSize = 12 * resize;
            Font  fDefault = new Font("微软雅黑", fontSize);
            Font  fEmoji = new Font("Segoe UI Emoji", fontSize);
            Color fc = Color.FromArgb(255, 44, 54, 76);
            Brush fb = new SolidBrush(fc);
            int   fx = 64 * resize, fy = 405 * resize;
            var   emojis = System.Text.RegularExpressions.Regex.Matches(model.Name, Reg.EMOJI);

            for (int i = 0; i < model.Name.Length;)
            {
                bool isFind = false;
                foreach (System.Text.RegularExpressions.Match item in emojis)
                {
                    if (item.Index == i)
                    {
                        g1.DrawString(item.Value, fEmoji, fb, fx, 405 * resize);
                        fx    += (int)(Math.Ceiling(g1.MeasureString(item.Value, fEmoji).Width)) - 4 * resize;
                        i     += item.Length;
                        isFind = true;
                        break;
                    }
                }
                if (i == model.Name.Length || isFind)
                {
                    continue;
                }
                string txt = model.Name[i].ToString();
                g1.DrawString(txt, fDefault, fb, fx, 405 * resize);
                fx += (int)(Math.Ceiling(g1.MeasureString(txt, fDefault).Width)) - 4 * resize;
                i++;
            }

            // 保存输出到本地
            var    path     = $"~/Upload/{model.FileName}.png";
            string savePath = System.Web.HttpContext.Current.Server.MapPath(path);

            bitMap = new Bitmap(DrawingPictures.RoundCorners((Image)bitMap, 4 * resize, Color.Transparent));
            bitMap.MakeTransparent();
            bitMap.Save(savePath);
            ////微信小程序的限制,图片放到七牛上无法缓存,然后无法把海报保存到相册
            //QinQiuApi qniu = new QinQiuApi();
            //string resultpath = qniu.UploadFile(savePath, true);
            g1.Dispose();
            bitMap.Dispose();
            return(path);
        }
Пример #2
0
        /// <summary>
        /// 生成海报方法
        /// </summary>
        /// <param name="strBg">背景图地址</param>
        /// <param name="model">海报内容model</param>
        /// <returns>返回海报图片地址</returns>
        public static string MergePosterImage(DrawingPictureModel model)
        {
            List <TagModel> taglist = model.TagList;
            string          bgPath  = System.Web.HttpContext.Current.Request.MapPath("~\\Content\\Images\\bg.png");
            string          fname   = "微软雅黑";
            Font            f       = new Font(fname, 18);
            Font            f12     = new Font(fname, 12);
            Font            font22  = new Font(fname, 22, FontStyle.Bold);
            Font            font26  = new Font(fname, 26, FontStyle.Bold);
            Font            font20  = new Font(fname, 20, FontStyle.Bold);

            // 初始化背景图片的宽高
            Bitmap bitMap = new Bitmap(933, 1500);
            // 初始化画板
            Graphics g1 = Graphics.FromImage(bitMap);
            ////设置画布背景颜色为白色
            //g1.FillRectangle(Brushes.White, new Rectangle(0, 0, 933, 1500));
            //g1.Clear(Color.White);

            //设置背景图
            FileStream bgfs    = new FileStream(bgPath, FileMode.Open, FileAccess.Read);
            Image      bgimage = Image.FromStream(bgfs);

            bgfs.Close();
            g1.DrawImage(bgimage, new Rectangle(0, 0, 933, 1500));

            //用setpixel方法绘制图片
            //Bitmap bitMap = new Bitmap(933, 1500);//初始化用来拼图的背景图并设置大小
            //Graphics g1 = Graphics.FromImage(bitMap);//初始化用来拼图的画板
            //Bitmap map = new Bitmap(model.AvatarPath);//读取要画到画板上的图片
            //g1.FillRectangle(Brushes.White, new Rectangle(0, 0, 834, 834));//设置要画的图片大小
            //循环设置好的要画的图片的宽和高来绘画图片
            //for (int i = 0; i < map.Width; i++)
            //{
            //    for (int j = 0; j < map.Height; j++)
            //    {
            //        var temp = map.GetPixel(i, j);//取每个像素和颜色
            //        bitMap.SetPixel(48 + i, 60 + j, temp);//将读取到的图片像素和颜色画到画板上
            //    }
            //}
            //map.Dispose();

            if (!string.IsNullOrWhiteSpace(model.AvatarPath))
            {
                //拼接头像图片
                FileStream avfs    = new FileStream(model.AvatarPath, FileMode.Open, FileAccess.Read);
                Image      avimage = Image.FromStream(avfs);
                avfs.Close();
                avfs.Dispose();
                g1.DrawImage(avimage, new Rectangle(48, 60, 834, 834));
            }

            if (!string.IsNullOrWhiteSpace(model.QrPath))
            {
                //拼接二维码之前画一个白色的圆底
                Rectangle rect  = new Rectangle(670, 1230, 240, 240);
                Brush     brush = new SolidBrush(Color.White);
                g1.FillEllipse(brush, rect);
                //描边
                Pen pen = Pens.Black;
                g1.DrawEllipse(pen, rect);

                //拼接二维码图片
                FileStream fs    = new FileStream(model.QrPath, FileMode.Open, FileAccess.Read);
                Image      image = Image.FromStream(fs);
                fs.Close();
                fs.Dispose();
                g1.DrawImage(image, new Rectangle(670, 1230, 240, 240));
            }

            if (!string.IsNullOrWhiteSpace(model.LogoPath))
            {
                //拼接公司logo图片
                FileStream logofs    = new FileStream(model.LogoPath, FileMode.Open, FileAccess.Read);
                Image      logoimage = Image.FromStream(logofs);
                logofs.Close();
                logofs.Dispose();
                g1.DrawImage(logoimage, new Rectangle(50, 1230, 96, 96));
            }

            if (taglist.Count > 0)
            {
                for (int i = 0; i < taglist.Count; i++)
                {
                    TagModel tempm     = taglist[i];
                    string   tempname  = tempm.TagName;
                    int      taglength = tempname.Length;
                    int      tagwidth  = taglength <= 5 ? 90 : taglength <= 7 ? 120 : taglength <= 10 ? 160 : taglength <= 13 ? 190 : 220;

                    Color bgc = new Color();
                    Color boc = new Color();
                    switch (tempm.TagStyle)
                    {
                    case CardTabStyle.Orange:
                    {
                        bgc = Color.FromArgb(255, 250, 249);
                        boc = Color.FromArgb(255, 223, 214);
                    }
                    break;

                    case CardTabStyle.Green:
                    {
                        bgc = Color.FromArgb(249, 255, 252);
                        boc = Color.FromArgb(190, 233, 215);
                    }
                    break;

                    case CardTabStyle.Blue:
                    {
                        bgc = Color.FromArgb(249, 255, 255);
                        boc = Color.FromArgb(214, 226, 255);
                    }
                    break;

                    case CardTabStyle.Purple:
                    {
                        bgc = Color.FromArgb(252, 249, 255);
                        boc = Color.FromArgb(234, 214, 255);
                    }
                    break;

                    default:
                        break;
                    }

                    if (i == 0)
                    {
                        DrawingPictures.SetBox(bitMap, g1, tagwidth, 50, Color.FromArgb(255, 223, 214), Color.FromArgb(255, 250, 249), 50, 1130, 2);
                        DrawingPictures.DrawStringWrap(g1, f12, tempname, new Rectangle(50, 1130, tagwidth, 50), 1145, 55, 18);
                    }
                    else
                    {
                        TagModel tempm2     = taglist[i - 1];
                        string   tempname2  = tempm.TagName;
                        int      taglength2 = tempname2.Length;
                        int      tagwidth2  = taglength2 <= 5 ? 90 : taglength2 <= 7 ? 120 : taglength2 <= 10 ? 160 : taglength2 <= 13 ? 190 : 220;
                        DrawingPictures.SetBox(bitMap, g1, tagwidth, 50, Color.FromArgb(190, 233, 215), Color.FromArgb(249, 255, 252), 70 + tagwidth2, 1130, 2);
                        DrawingPictures.DrawStringWrap(g1, f12, tempname, new Rectangle(70 + tagwidth2, 1130, tagwidth, 50), 1145, 75 + tagwidth2, 18);
                    }
                }
                //计算标签内容的字数长度
                //int taglength1 = model.tag1.Length;
                //int taglength2 = model.tag2.Length;
                //int t1width = taglength1 <= 5 ? 90 : taglength1 <= 7 ? 120 : taglength1 <= 10 ? 160 : taglength1 <= 13 ? 190 : 220;
                //int t2width = taglength2 <= 5 ? 90 : taglength2 <= 7 ? 120 : taglength2 <= 10 ? 160 : taglength2 <= 13 ? 190 : 220;

                ////画第一个标签框
                //DrawingPictures.SetBox(bitMap, g1, t1width, 50, Color.FromArgb(255, 223, 214), Color.FromArgb(255, 250, 249), 50, 1130, 2);
                ////画第二个标签框
                //DrawingPictures.SetBox(bitMap, g1, t2width, 50, Color.FromArgb(190, 233, 215), Color.FromArgb(249, 255, 252), 70 + t1width, 1130, 2);
                ////第一个标签框内容
                //DrawingPictures.DrawStringWrap(g1, f12, model.tag1, new Rectangle(50, 1130, t1width, 50), 1145, 55, 18);
                ////第二个标签框内容
                //DrawingPictures.DrawStringWrap(g1, f12, model.tag2, new Rectangle(70 + t1width, 1130, t2width, 50), 1145, 75 + t1width, 18);
            }

            //姓名
            DrawingPictures.DrawStringWrap(g1, font26, model.UserName == null ? "" : model.UserName, new Rectangle(50, 1000, 400, 40), 950, 50, 15);
            //职位
            DrawingPictures.DrawStringWrap(g1, font22, model.Position == null ? "" : model.Position, new Rectangle(50, 1000, 400, 40), 995, 50, 15);
            //签名
            DrawingPictures.DrawStringWrap(g1, f, model.Remark == null ? "" : model.Remark, new Rectangle(50, 1000, 800, 60), 1050, 50, 20);
            //公司名称
            DrawingPictures.DrawStringWrap(g1, font20, model.CompanyName == null ? "" : model.CompanyName, new Rectangle(50, 1250, 400, 160), 1350, 50, 12);

            // 保存输出到本地
            var    path     = $"~/Upload/{model.PosterImageName}.jpg";
            string savePath = System.Web.HttpContext.Current.Server.MapPath(path);

            bitMap.Save(savePath);
            //微信小程序的限制,图片放到七牛上无法缓存,然后无法把海报保存到相册
            //QinQiuApi qniu = new QinQiuApi();
            //string resultpath = qniu.UploadFile(savePath, true, true);
            g1.Dispose();
            bitMap.Dispose();
            //生成完成后删除本地缓存文件
            if (File.Exists(model.LogoPath))
            {
                File.Delete(model.LogoPath);
            }
            if (File.Exists(model.AvatarPath))
            {
                File.Delete(model.AvatarPath);
            }
            if (File.Exists(model.QrPath))
            {
                File.Delete(model.QrPath);
            }
            return(path);
        }