APS.NET(C#)分销推荐海报程序

本文介绍了一个生成个性化推广海报的方法,包括调整图片尺寸、绘制圆形头像及二维码等步骤,并展示了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近有个项目需要进行生成海报的程序,类似推荐注册的页面,这个在分销系统里面很常见,我们做个简要讲解,具体图片和代码如下。

调用的方法:

public string Draw(string myorganizecode,string myorganizename, string tuiorganizename,string level,string headimgurl,string nickname)
{

    //头像 
    //背景图片
    string path = Server.MapPath("/MallApi/static/home/template.jpg");

    System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(path);

    string qrcode = Server.MapPath("/UploadImg/Agent/"+ myorganizecode + "/"+ level + "/"+ myorganizecode + ".jpg");
    //处理二维码图片大小 240*240px
    System.Drawing.Image qrCodeImage = ApiUtils.ReduceImage(qrcode, 360, 360);

    //处理头像图片大小 100*100px
    headimgurl = "https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLvDC4niccQ2ywph3bTJoLxicMmzaJeK3tMzD8F4Cq521Go0iaQFtcviade4bohbfuDNo1kxCqCDDOmqA/132";
    System.Drawing.Image titleImage = ApiUtils.CircleImage(headimgurl, 100, 100);

    using (Graphics g = Graphics.FromImage(imgSrc))
    {
        //画专属推广二维码
        g.DrawImage(qrCodeImage, new Rectangle(imgSrc.Width - qrCodeImage.Width - 195,
        imgSrc.Height - qrCodeImage.Height - 480,
        qrCodeImage.Width,
        qrCodeImage.Height),
        0, 0, qrCodeImage.Width, qrCodeImage.Height, GraphicsUnit.Pixel);
        //画头像
        g.DrawImage(titleImage, 330, 90, titleImage.Width, titleImage.Height);
        //画昵称
        Font font = new Font("Microsoft YaHei", 22, FontStyle.Regular);
        g.DrawString(nickname, font, new SolidBrush(Color.White), 350, 210);
        //画自己的经销商等级
        Font fontLevel= new Font("Microsoft YaHei", 22, FontStyle.Regular);
        g.DrawString(myorganizename, fontLevel, new SolidBrush(System.Drawing.ColorTranslator.FromHtml("#24208b")), 290, 270);
        //画邀请的话
        Font fontY = new Font("Microsoft YaHei", 26, FontStyle.Regular); 
        g.DrawString("邀请您成为", fontY, new SolidBrush(System.Drawing.ColorTranslator.FromHtml("#24208b")), 275, 370);
        Font fontName = new Font("Microsoft YaHei", 26, FontStyle.Regular);
        g.DrawString(tuiorganizename, fontName, new SolidBrush(System.Drawing.ColorTranslator.FromHtml("#24208b")), 290, 430);

    }
    string newpath = Server.MapPath(@"/UploadImg/Agent/newtg_" + Guid.NewGuid().ToString() + ".jpg");
    imgSrc.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
    return newpath;
}

引用方法:

  /// <summary>
        /// 缩小/放大图片
        /// </summary>
        /// <param name="url">图片网络地址</param>
        /// <param name="toWidth">缩小/放大宽度</param>
        /// <param name="toHeight">缩小/放大高度</param>
        /// <returns></returns>
        public static Image ReduceImage(string url, int toWidth, int toHeight)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream responseStream = response.GetResponseStream();

            Image originalImage = Image.FromStream(responseStream);
            if (toWidth <= 0 && toHeight <= 0)
            {
                return originalImage;
            }
            else if (toWidth > 0 && toHeight > 0)
            {
                if (originalImage.Width < toWidth && originalImage.Height < toHeight)
                    return originalImage;
            }
            else if (toWidth <= 0 && toHeight > 0)
            {
                if (originalImage.Height < toHeight)
                    return originalImage;
                toWidth = originalImage.Width * toHeight / originalImage.Height;
            }
            else if (toHeight <= 0 && toWidth > 0)
            {
                if (originalImage.Width < toWidth)
                    return originalImage;
                toHeight = originalImage.Height * toWidth / originalImage.Width;
            }
            Image toBitmap = new Bitmap(toWidth, toHeight);
            using (Graphics g = Graphics.FromImage(toBitmap))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.Clear(Color.Transparent);
                g.DrawImage(originalImage,
                            new Rectangle(0, 0, toWidth, toHeight),
                            new Rectangle(0, 0, originalImage.Width, originalImage.Height),
                            GraphicsUnit.Pixel);
                originalImage.Dispose();
                return toBitmap;
            }

         
        }

        /// <summary>
        /// 生成圆形图片
        /// </summary>
        /// <param name="url"></param>
        /// <param name="toWidth"></param>
        /// <param name="toHeight"></param>
        /// <returns></returns>
        public static Image CircleImage(string url, int toWidth, int toHeight)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream responseStream = response.GetResponseStream();

            Image originalImage = Image.FromStream(responseStream);
         
            Image toBitmap = new Bitmap(originalImage.Width, originalImage.Height);
            using (Graphics g = Graphics.FromImage(toBitmap))
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                using (System.Drawing.Drawing2D.GraphicsPath p = new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Alternate))
                {
                    p.AddEllipse(0, 0, toWidth, toHeight);
                    g.FillPath(new TextureBrush(originalImage), p);

                    originalImage.Dispose();
                }
                return toBitmap;
            }
        }

最终效果图:
分享图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

星海设计

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

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

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

打赏作者

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

抵扣说明:

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

余额充值