Example #1
0
        public bool CreatePoster(out string imgUrl)
        {
            bool result = false;

            if (this.resultObj == null || this.resultObj["BgImg"] == null)
            {
                imgUrl = "掌柜名片模板未设置,无法生成名片!";
                return(result);
            }
            imgUrl = "生成失败";
            Bitmap bitmap = null;

            bitmap = ((!this.CodeUrl.Contains("weixin.qq.com")) ? ((Bitmap)System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(this.CodeUrl))) : ResourcesHelper.GetNetImg(this.CodeUrl));
            int num = int.Parse(this.resultObj["DefaultHead"].ToString());

            if (string.IsNullOrEmpty(this.UserHead) || (!this.UserHead.ToLower().StartsWith("http") && !this.UserHead.ToLower().StartsWith("https") && !File.Exists(Globals.MapPath(this.UserHead))))
            {
                this.UserHead = "/Utility/pics/imgnopic.jpg";
            }
            if (num == 2)
            {
                this.UserHead = "";
            }
            System.Drawing.Image image        = (!this.UserHead.ToLower().StartsWith("http") && !this.UserHead.ToLower().StartsWith("https")) ? ((string.IsNullOrEmpty(this.UserHead) || !File.Exists(Globals.MapPath(this.UserHead))) ? new Bitmap(100, 100) : System.Drawing.Image.FromFile(Globals.MapPath(this.UserHead))) : ResourcesHelper.GetNetImg(this.UserHead);
            GraphicsPath         graphicsPath = new GraphicsPath();

            graphicsPath.AddEllipse(new Rectangle(0, 0, image.Width, image.Width));
            Bitmap bitmap2 = new Bitmap(image.Width, image.Width);

            using (Graphics graphics = Graphics.FromImage(bitmap2))
            {
                graphics.SetClip(graphicsPath);
                graphics.DrawImage(image, 0, 0, image.Width, image.Width);
            }
            image.Dispose();
            bitmap = ResourcesHelper.CombinImage(bitmap, bitmap2, 80);
            Bitmap   bitmap3   = new Bitmap(480, 735);
            Graphics graphics2 = Graphics.FromImage(bitmap3);

            graphics2.SmoothingMode      = SmoothingMode.HighQuality;
            graphics2.CompositingQuality = CompositingQuality.HighQuality;
            graphics2.InterpolationMode  = InterpolationMode.High;
            graphics2.Clear(Color.White);
            Bitmap image2 = new Bitmap(100, 100);

            if (this.resultObj["BgImg"] != null && File.Exists(Globals.MapPath(this.resultObj["BgImg"].ToString())))
            {
                image2 = (Bitmap)System.Drawing.Image.FromFile(Globals.MapPath(this.resultObj["BgImg"].ToString()));
                image2 = ResourcesHelper.GetThumbnail(image2, 735, 480);
            }
            graphics2.DrawImage(image2, 0, 0, 480, 735);
            Font font  = new Font("微软雅黑", (float)(this.resultObj["myusernameSize"].ToInt(0) * 6 / 5));
            Font font2 = new Font("微软雅黑", (float)(this.resultObj["shopnameSize"].ToInt(0) * 6 / 5));

            graphics2.DrawImage(bitmap2, (int)((decimal)this.resultObj["posList"][0]["left"] * 480m), (int)this.resultObj["posList"][0]["top"] * 735 / 490, (int)((decimal)this.resultObj["posList"][0]["width"] * 480m), (int)((decimal)this.resultObj["posList"][0]["width"] * 480m));
            StringFormat format = new StringFormat(StringFormatFlags.DisplayFormatControl);
            string       text   = this.resultObj["myusername"].ToString().Replace("{{昵称}}", "$");
            string       text2  = this.resultObj["shopname"].ToString().Replace("{{商城名称}}", "$");

            string[] array  = text.Split('$');
            string[] array2 = text2.Split('$');
            graphics2.DrawString(array[0], font, new SolidBrush(ColorTranslator.FromHtml(this.resultObj["myusernameColor"].ToString())), (float)(int)((decimal)this.resultObj["posList"][1]["left"] * 480m), (float)((int)this.resultObj["posList"][1]["top"] * 735 / 490), format);
            if (array.Length > 1)
            {
                SizeF sizeF  = graphics2.MeasureString(" ", font);
                SizeF sizeF2 = graphics2.MeasureString(array[0], font);
                graphics2.DrawString(this.UserName, font, new SolidBrush(ColorTranslator.FromHtml(this.resultObj["nickNameColor"].ToString())), (float)(int)((decimal)this.resultObj["posList"][1]["left"] * 480m) + sizeF2.Width - sizeF.Width, (float)((int)this.resultObj["posList"][1]["top"] * 735 / 490), format);
                SizeF sizeF3 = graphics2.MeasureString(this.UserName, font);
                graphics2.DrawString(array[1], font, new SolidBrush(ColorTranslator.FromHtml(this.resultObj["myusernameColor"].ToString())), (float)(int)((decimal)this.resultObj["posList"][1]["left"] * 480m) + sizeF2.Width - sizeF.Width * 2f + sizeF3.Width, (float)((int)this.resultObj["posList"][1]["top"] * 735 / 490), format);
            }
            int   num2 = 660 - (int)((decimal)this.resultObj["posList"][2]["left"] * 480m);
            float num3 = 0f;
            int   num4 = 0;
            int   num5 = 0;

            for (int i = 0; i < array2[0].Length; i++)
            {
                if (i < array2[0].Length)
                {
                    string text3  = array2[0].Substring(i, 1);
                    SizeF  sizeF4 = graphics2.MeasureString(text3, font2);
                    num3 += sizeF4.Width;
                    if (num3 > (float)num2 - sizeF4.Width && num3 <= (float)num2)
                    {
                        graphics2.DrawString(array2[0].Substring(num4, i - num4), font2, new SolidBrush(ColorTranslator.FromHtml(this.resultObj["shopnameColor"].ToString())), (float)(int)((decimal)this.resultObj["posList"][2]["left"] * 480m), (float)((int)this.resultObj["posList"][2]["top"] * 735 / 490 + num5 * (int)sizeF4.Height));
                        num4 = i;
                        num5++;
                        num3 = 0f;
                    }
                }
            }
            if (num4 < array2[0].Length)
            {
                string text4  = array2[0].Substring(num4, 1);
                SizeF  sizeF5 = graphics2.MeasureString(text4, font2);
                graphics2.DrawString(array2[0].Substring(num4, array2[0].Length - num4), font2, new SolidBrush(ColorTranslator.FromHtml(this.resultObj["shopnameColor"].ToString())), (float)(int)((decimal)this.resultObj["posList"][2]["left"] * 480m), (float)((int)this.resultObj["posList"][2]["top"] * 735 / 490 + num5 * (int)sizeF5.Height));
            }
            if (array2.Length > 1)
            {
                SizeF sizeF6 = graphics2.MeasureString(" ", font2);
                SizeF sizeF7 = graphics2.MeasureString(array2[0], font2);
                graphics2.DrawString(this.StoreName, font2, new SolidBrush(ColorTranslator.FromHtml(this.resultObj["storeNameColor"].ToString())), (float)(int)((decimal)this.resultObj["posList"][2]["left"] * 480m) + sizeF7.Width - sizeF6.Width, (float)((int)this.resultObj["posList"][2]["top"] * 735 / 490), format);
                SizeF sizeF8 = graphics2.MeasureString(this.StoreName, font2);
                graphics2.DrawString(array2[1], font2, new SolidBrush(ColorTranslator.FromHtml(this.resultObj["shopnameColor"].ToString())), (float)(int)((decimal)this.resultObj["posList"][2]["left"] * 480m) + sizeF7.Width - sizeF6.Width * 2f + sizeF8.Width, (float)((int)this.resultObj["posList"][2]["top"] * 735 / 490), format);
            }
            graphics2.DrawImage(bitmap, (int)((decimal)this.resultObj["posList"][3]["left"] * 480m), (int)this.resultObj["posList"][3]["top"] * 735 / 490, (int)((decimal)this.resultObj["posList"][3]["width"] * 480m), (int)((decimal)this.resultObj["posList"][3]["width"] * 480m));
            bitmap.Dispose();
            if (!Directory.Exists(Globals.MapPath(Globals.GetStoragePath() + "/ReferralPoster")))
            {
                Directory.CreateDirectory(Globals.MapPath(Globals.GetStoragePath() + "/ReferralPoster"));
            }
            bitmap3.Save(Globals.MapPath(Globals.GetStoragePath() + "/ReferralPoster/Poster_" + ((base.ClientType == ClientType.VShop) ? "Wap_" : "Vshop_") + this.ReferralId + ".jpg"), ImageFormat.Jpeg);
            Random random = new Random();

            imgUrl = Globals.GetStoragePath() + "/ReferralPoster/Poster_" + ((base.ClientType == ClientType.VShop) ? "Wap_" : "Vshop_") + this.ReferralId + ".jpg?rnd=" + random.Next();
            bitmap3.Dispose();
            return(true);
        }
Example #2
0
        public bool CreatePoster(out string imgUrl)
        {
            bool result = false;

            if (resultObj == null || resultObj["BgImg"] == null)
            {
                imgUrl = "掌柜名片模板未设置,无法生成名片!";
                return(result);
            }

            imgUrl = "生成失败";



            //生成二维码图片
            Bitmap Qrimage = null;

            if (CodeUrl.Contains("weixin.qq.com"))
            {
                Qrimage = ResourcesHelper.GetNetImg(CodeUrl);
            }
            else
            {
                Qrimage = (Bitmap)System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(CodeUrl));
            }
            int DefaultHead = int.Parse(resultObj["DefaultHead"].ToString());

            if (string.IsNullOrEmpty(UserHead) || (!UserHead.ToLower().StartsWith("http") && !UserHead.ToLower().StartsWith("https") && !File.Exists(Globals.MapPath(UserHead))))
            {
                UserHead = "/Utility/pics/imgnopic.jpg";
            }


            if (DefaultHead == 2)
            {
                UserHead = "";
            }


            System.Drawing.Image logoimg;
            if (UserHead.ToLower().StartsWith("http") || UserHead.ToLower().StartsWith("https"))
            {
                logoimg = ResourcesHelper.GetNetImg(UserHead); //获取网络图片 ;// new Bitmap(UserHead);
            }
            else
            {
                if (!string.IsNullOrEmpty(UserHead) && File.Exists(Globals.MapPath(UserHead)))
                {
                    logoimg = System.Drawing.Image.FromFile(Globals.MapPath(UserHead));
                }
                else
                {
                    logoimg = new Bitmap(100, 100);
                };
            }


            //转换成圆形图片
            GraphicsPath gp = new GraphicsPath();

            gp.AddEllipse(new Rectangle(0, 0, logoimg.Width, logoimg.Width));
            Bitmap Tlogoimg = new Bitmap(logoimg.Width, logoimg.Width);

            using (Graphics gl = Graphics.FromImage(Tlogoimg))
            { //假设bm就是你要绘制的正方形位图,已创建好
                gl.SetClip(gp);
                gl.DrawImage(logoimg, 0, 0, logoimg.Width, logoimg.Width);
            }
            logoimg.Dispose();

            //合成二维码图像
            //Qrimage = CombinImage(Qrimage, Tlogoimg, 80); //二维码图片

            Bitmap Cardbmp = new Bitmap(480, 735);

            Graphics g = Graphics.FromImage(Cardbmp);

            g.SmoothingMode      = SmoothingMode.HighQuality;; //抗锯齿
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode  = InterpolationMode.High;
            g.Clear(System.Drawing.Color.White); //白色填充

            Bitmap Bgimg = new Bitmap(100, 100);

            if (resultObj["BgImg"] != null && File.Exists(Globals.MapPath(resultObj["BgImg"].ToString())))
            {
                //如果背景图片存在,
                Bgimg = (Bitmap)System.Drawing.Image.FromFile(Globals.MapPath(resultObj["BgImg"].ToString())); //如果存在,读取背景图片
                Bgimg = ResourcesHelper.GetThumbnail(Bgimg, 735, 480);                                         //处理成对应尺寸图片
            }

            //绘制背景图
            g.DrawImage(Bgimg, 0, 0, 480, 735);


            Font usernameFont = new Font("微软雅黑", (int)(resultObj["myusernameSize"].ToInt() * 6 / 5));

            Font shopnameFont = new Font("微软雅黑", (int)(resultObj["shopnameSize"].ToInt() * 6 / 5));


            //加入用户头像
            g.DrawImage(Tlogoimg, (int)(((decimal)resultObj["posList"][0]["left"]) * 480),
                        (int)resultObj["posList"][0]["top"] * 735 / 490,
                        (int)(((decimal)resultObj["posList"][0]["width"]) * 480),
                        (int)(((decimal)resultObj["posList"][0]["width"]) * 480)
                        );

            StringFormat StringFormat = new StringFormat(StringFormatFlags.DisplayFormatControl);



            string myusername = resultObj["myusername"].ToString().Replace(@"{{昵称}}", "$");
            string shopname   = resultObj["shopname"].ToString().Replace(@"{{商城名称}}", "$");

            string[] myusernameArray = myusername.Split('$');
            string[] shopnameArray   = shopname.Split('$');

            //写昵称
            g.DrawString(myusernameArray[0], usernameFont, new SolidBrush(System.Drawing.ColorTranslator.FromHtml(resultObj["myusernameColor"].ToString())),
                         (int)(((decimal)resultObj["posList"][1]["left"]) * 480),
                         (int)resultObj["posList"][1]["top"] * 735 / 490,
                         StringFormat);

            if (myusernameArray.Length > 1)
            {
                var spcSize1       = g.MeasureString(" ", usernameFont);
                var myusernameSize = g.MeasureString(myusernameArray[0], usernameFont);
                g.DrawString(UserName, usernameFont, new SolidBrush(System.Drawing.ColorTranslator.FromHtml(resultObj["nickNameColor"].ToString())),
                             (int)(((decimal)resultObj["posList"][1]["left"]) * 480) + myusernameSize.Width - spcSize1.Width,
                             (int)resultObj["posList"][1]["top"] * 735 / 490,
                             StringFormat);

                var usernameSize = g.MeasureString(UserName, usernameFont);
                g.DrawString(myusernameArray[1], usernameFont, new SolidBrush(System.Drawing.ColorTranslator.FromHtml(resultObj["myusernameColor"].ToString())),
                             (int)(((decimal)resultObj["posList"][1]["left"]) * 480) + myusernameSize.Width - spcSize1.Width * 2 + usernameSize.Width,
                             (int)resultObj["posList"][1]["top"] * 735 / 490,
                             StringFormat);
            }


            //写店铺名
            var   lineWidth = 660 - (int)(((decimal)resultObj["posList"][2]["left"]) * 480);
            float fontWidth = 0;
            int   index     = 0;
            int   lineIndex = 0;

            for (int i = 0; i < shopnameArray[0].Length; i++)
            {
                if (i < shopnameArray[0].Length)
                {
                    string str      = shopnameArray[0].Substring(i, 1);
                    var    spcSize2 = g.MeasureString(str, shopnameFont);
                    fontWidth += spcSize2.Width;
                    if (fontWidth > (lineWidth - spcSize2.Width) && fontWidth <= lineWidth)
                    {
                        g.DrawString(shopnameArray[0].Substring(index, i - index), shopnameFont, new SolidBrush(System.Drawing.ColorTranslator.FromHtml(resultObj["shopnameColor"].ToString())),
                                     (int)(((decimal)resultObj["posList"][2]["left"]) * 480),
                                     (int)resultObj["posList"][2]["top"] * 735 / 490 + lineIndex * (int)spcSize2.Height);
                        index = i;
                        lineIndex++;
                        fontWidth = 0;
                    }
                }
            }

            if (index < shopnameArray[0].Length)
            {
                string str      = shopnameArray[0].Substring(index, 1);
                var    spcSize2 = g.MeasureString(str, shopnameFont);
                g.DrawString(shopnameArray[0].Substring(index, shopnameArray[0].Length - index), shopnameFont, new SolidBrush(System.Drawing.ColorTranslator.FromHtml(resultObj["shopnameColor"].ToString())),
                             (int)(((decimal)resultObj["posList"][2]["left"]) * 480),
                             (int)resultObj["posList"][2]["top"] * 735 / 490 + lineIndex * (int)spcSize2.Height);
            }

            if (shopnameArray.Length > 1)
            {
                var spcSize = g.MeasureString(" ", shopnameFont);

                var shopnameSize = g.MeasureString(shopnameArray[0], shopnameFont);
                g.DrawString(StoreName, shopnameFont, new SolidBrush(System.Drawing.ColorTranslator.FromHtml(resultObj["storeNameColor"].ToString())),
                             (int)(((decimal)resultObj["posList"][2]["left"]) * 480) + shopnameSize.Width - spcSize.Width,
                             (int)resultObj["posList"][2]["top"] * 735 / 490,
                             StringFormat);

                var StorenameSize = g.MeasureString(StoreName, shopnameFont);
                g.DrawString(shopnameArray[1], shopnameFont, new SolidBrush(System.Drawing.ColorTranslator.FromHtml(resultObj["shopnameColor"].ToString())),
                             (int)(((decimal)resultObj["posList"][2]["left"]) * 480) + shopnameSize.Width - spcSize.Width * 2 + StorenameSize.Width,
                             (int)resultObj["posList"][2]["top"] * 735 / 490,
                             StringFormat);
            }


            //加入二维码
            //g.DrawImage(Qrimage,
            //    (int)(((decimal)resultObj["posList"][3]["left"]) * 480),
            //    ((int)resultObj["posList"][3]["top"] * 735 / 490),
            //    (int)(((decimal)resultObj["posList"][3]["width"]) * 480),
            //    (int)(((decimal)resultObj["posList"][3]["width"]) * 480)
            //    );
            //Qrimage.Dispose();

            if (!Directory.Exists(Globals.MapPath(Globals.GetStoragePath() + @"/ReferralPoster")))
            {
                Directory.CreateDirectory(Globals.MapPath(Globals.GetStoragePath() + @"/ReferralPoster"));
            }

            Cardbmp.Save(Globals.MapPath(Globals.GetStoragePath() + @"/ReferralPoster/Poster_Applet_" + ReferralId + ".jpg"), ImageFormat.Jpeg);
            Random rd = new Random();

            imgUrl = Globals.GetStoragePath() + @"/ReferralPoster/Poster_Applet_" + ReferralId + ".jpg" + "?rnd=" + rd.Next();

            Cardbmp.Dispose();
            result = true;

            return(result);
        }