.NetCore生成海报+二维码(logo)

本文介绍了如何利用C#生成包含商品信息、二维码和美观设计的海报。通过安装QRCoder、ImageSharp等Nuget包,结合图片处理库,绘制包括顶部图片、商品图片、卡券背景、文字内容、二维码等多个元素,最终生成可分享的海报图片。同时,文章提及了前端生成海报的建议,以提高性能和调试便利性。

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

准备工作

引入如下Nuget包依赖

Install-Package QRCoder 1.4.1
Install-Package SixLabors.ImageSharp 1.0.3
Install-Package SixLabors.ImageSharp.Drawing 1.0.0-beta13
或者编辑项目文件,新增如下节点
<PackageReference Include="QRCoder" Version="1.4.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta13" />

依赖图片

项目根目录新建字体文件夹(fonts)
Alibaba-PuHuiTi-Regular.ttf
SIMHEI.TTF
新建图片文件夹(images)
bottom.png
mask.png
star.png
top.png

代码如下

        /// <summary>
        /// 生成海报
        /// </summary>
        /// <param name="response">商品海报返回数据</param>
        /// <returns></returns>
        protected async Task<Stream> Generate(ProductResponse response)
        {
            //画布宽度
            int canvasWidth = 327 * 2;
            //画布高度
            int canvasHeight = 485 * 2;

            //卡券图片背景高度
            int CouponImgBgHeight = 197 * 2;

            //卡券图片宽度
            int CouponImgWidth = 260 * 2;

            //卡券图片高度
            int CouponImgHeight = 155 * 2;

            //卡券图片居上间距
            int CouponImgTopPadding = 35 * 2;

            //卡券图片居左间距
            int CouponImgLeftPadding = 33 * 2;

            //卡券图片背景颜色
            Color CouponImgBgColor = Color.ParseHex("#F7F7F7");

            //卡券内容背景高度
            int CouponContentBgHeight = 232 * 2;

            //卡券内容背景颜色
            Color CouponContentBgColor = Color.White;

            //图片的高度
            int imageHeight = 28 * 2;

            //二维码宽度
            int qrCodeWidth = 80 * 2;

            //二维码高度
            int qrCodeHeight = 80 * 2;

            FontCollection fonts = new FontCollection();
            FontFamily numFamily = fonts.Install("fonts/Alibaba-PuHuiTi-Regular.ttf");
            FontFamily textFamily = fonts.Install("fonts/SIMHEI.TTF");

 			// 绘制画布
            using (Image<Rgba32> image = new Image<Rgba32>(canvasWidth, canvasHeight, Color.Transparent))
            {

                //绘制顶部图片
                var topImage = Image.Load("images/top.png");
                image.Mutate(t => t.DrawImage(topImage, new Point(0, 0), 1f));

                //绘制卡券图片背景区域(绘制矩形,四点确定矩形)
                image.Mutate(t => t.FillPolygon(CouponImgBgColor,
                           new Vector2(0, imageHeight),
                           new Vector2(canvasWidth, imageHeight),
                           new Vector2(canvasWidth, CouponImgBgHeight + imageHeight),
                           new Vector2(0, CouponImgBgHeight + imageHeight)));

                //绘制商品图片
                var couponRequest = (HttpWebRequest)WebRequest.Create(response.ImgHttpUrl);
                HttpWebResponse couponResponse = (HttpWebResponse)couponRequest.GetResponse();
                using (Stream responseStream = couponResponse.GetResponseStream())
                {
                    var productImage = Image.Load(responseStream);
                    productImage.Mutate(t => t.Resize(CouponImgWidth, CouponImgHeight));
                    image.Mutate(t => t.DrawImage(productImage, new Point(CouponImgLeftPadding, CouponImgTopPadding), 1f));

                    //绘制圆角蒙版
                    var maskImage = Image.Load("images/mask.png");
                    maskImage.Mutate(t => t.Resize(CouponImgWidth, CouponImgHeight));
                    image.Mutate(t => t.DrawImage(maskImage, new Point(CouponImgLeftPadding, CouponImgTopPadding), 1f));
                }

                //绘制卡券内容背景区域(绘制矩形)
                image.Mutate(t => t.FillPolygon(CouponContentBgColor,
                            new Vector2(0, imageHeight + CouponImgBgHeight),
                            new Vector2(canvasWidth, imageHeight + CouponImgBgHeight),
                            new Vector2(canvasWidth, imageHeight + CouponImgBgHeight + CouponContentBgHeight),
                            new Vector2(0, imageHeight + CouponImgBgHeight + CouponContentBgHeight)));
				// 文字多行显示处理,最多支持两行
                var productName = response.ProductName;
                var offset = 0; // 如果超过一行,则文字下方所有内容偏移的高度
                if (productName.Length > 18)
                {
                    productName = productName.Insert(18, "\r\n");
                    offset += 24;
                }
                if (productName.Length > 38)
                {
                    var length = productName.Length - 37;
                    productName = productName.Remove(37, length) + "...";
                }
                image.Mutate(t => t.DrawText(productName, new Font(textFamily, 16 * 2, FontStyle.Bold), Color.ParseHex("#333333"), new PointF(16 * 2, 242 * 2)));

                image.Mutate(t => t.DrawText("¥", new Font(numFamily, 14 * 2, FontStyle.Bold), Color.ParseHex("#FF0100"), new PointF(16 * 2, 279 * 2 + offset)));
                image.Mutate(t => t.DrawText(response.Amount.ToString(), new Font(numFamily, 24 * 2, FontStyle.Bold), Color.ParseHex("#FF0100"), new PointF(30 * 2, 270 * 2 + offset)));
                // 金额长度过程,金额后面的内容偏移量
                var amountOffset = response.Amount.ToString().Length * 28;
                image.Mutate(t => t.DrawText("优惠券金额", new Font(textFamily, 14 * 2, FontStyle.Bold), Color.ParseHex("#FF0100"), new PointF(30 * 2 + amountOffset, 281 * 2 + offset)));

                //绘制商品类型背景(绘制矩形)
                image.Mutate(t => t.FillPolygon(Color.ParseHex("#FAEFED"),
                            new Vector2(496, 552 + offset),
                            new Vector2(496 + 126, 552 + offset),
                            new Vector2(496 + 126, 552 + 35 + offset),
                            new Vector2(496, 552 + 35 + offset)));

                image.Mutate(t => t.DrawText("类型", new Font(textFamily, 11 * 2, FontStyle.Regular), Color.ParseHex("#FF0100"), new PointF(504 + 20, 558 + offset)));
                //image.Mutate(t => t.DrawText("RMB" + response.ProductFaceValue, new Font(numFamily, 14, FontStyle.Regular), Color.ParseHex("#B0B1B3"), new PointF(160, 278)));
                //image.Mutate(t => t.DrawLines(new Pen(Color.ParseHex("#B0B1B3"),1f), new PointF(155, 288), new PointF(212, 288)));

                var dateText = $"{response.StartDate.Value.ToString("yyyy.MM.dd")}-{response.EndDate.Value.ToString("yyyy.MM.dd")}";
                image.Mutate(t => t.DrawText($"有效期:", new Font(textFamily, 12 * 2, FontStyle.Regular), Color.ParseHex("#666666"), new PointF(16 * 2, 310 * 2 + 5 + offset)));
                image.Mutate(t => t.DrawText(dateText, new Font(numFamily, 12* 2, FontStyle.Regular), Color.ParseHex("#666666"), new PointF(16 * 2 + 90, 310 * 2 + offset)));

                //绘制分割线(绘制直线,两点确定直线)
                image.Mutate(t => t.DrawLines(Pens.Dash(Color.ParseHex("#F3DEDE"), 1f), new PointF(15 * 2, 340 * 2 + offset), new PointF(311 * 2, 340 * 2 + offset)));

                image.Mutate(t => t.DrawText("① 保存图片至相册", new Font(textFamily, 12 * 2, FontStyle.Regular), Color.ParseHex("#B0B1B3"), new PointF(32 * 2, 380 * 2 + offset)));
                image.Mutate(t => t.DrawText("② 长按扫描二维码浏览商品", new Font(textFamily, 12 * 2, FontStyle.Regular), Color.ParseHex("#B0B1B3"), new PointF(32 * 2, 402 * 2 + offset)));

                //绘制二维码,根据产品推广URL生成二维码
                var bitmap = GetQRCode(response.PopularizeUrl, 2);
                using (MemoryStream qrCodeStream = new MemoryStream())
                {
                    bitmap.Save(qrCodeStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] data = new byte[qrCodeStream.Length];
                    qrCodeStream.Seek(0, SeekOrigin.Begin);
                    qrCodeStream.Read(data, 0, Convert.ToInt32(qrCodeStream.Length));
                    var qrCodeImage = Image.Load(data);
                    qrCodeImage.Mutate(t => t.Resize(qrCodeWidth, qrCodeHeight));
                    image.Mutate(t => t.DrawImage(qrCodeImage, new Point(215 * 2, 357 * 2 + offset), 1f));
                }

                //绘制底部图片
                var bottomImage = Image.Load("images/bottom.png");
                image.Mutate(t => t.DrawImage(bottomImage, new Point(0, canvasHeight - imageHeight), 1f));
			    // 图片转为文件流
                using (var stream = new MemoryStream())
                {
                    await image.SaveAsync(stream, new PngEncoder());
                    stream.Position = 0; 
                    return stream;
                }
            }

            return null;
        }

        /// <summary>
        /// 获取二维码图片
        /// </summary>
        /// <param name="url">存储内容</param>
        /// <param name="pixel">像素大小</param>
        /// <returns></returns>
        public System.Drawing.Bitmap GetQRCode(string url, int pixel)
        {
            QRCodeGenerator generator = new QRCodeGenerator();
            QRCodeData codeData = generator.CreateQrCode(url, QRCodeGenerator.ECCLevel.Q);
            QRCoder.QRCode qrcode = new QRCoder.QRCode(codeData);
            System.Drawing.Bitmap qrImage = qrcode.GetGraphic(pixel);

            return qrImage;
        }

效果图

注:图中二维码仅供参考
在这里插入图片描述

总结

  1. 后端生成图片海报,可能存在性能因素,而且绘制不方便调试,建议前端生成海报
  2. 二维码生成官方文档,生成中间带logo的二维码,使用方法
    var bitmap = qrCode.GetGraphic(pixel, Color.Black, Color.White,(Bitmap)Image.FromFile(logoPath), 15, 8);
好的,我可以为您提供一个基本的代码示例来实现这个要求。不过需要注意的是,由于我是一个AI语言模型,无法直接访问您的文件系统,因此我无法将生成二维码打包成压缩文件。您需要根据您的需求自行编写将二维码打包成压缩文件的代码。 首先,您需要安装 SixLabors 包。可以使用以下命令在 .NET Core 项目中安装 SixLabors 包: ``` dotnet add package SixLabors.ImageSharp ``` 然后,在您的代码中导入 SixLabors 命名空间: ```csharp using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using SixLabors.Fonts; using SixLabors.ImageSharp.Formats.Png; using System.IO; ``` 接下来,您可以编写一个函数来生成二维码并保存为 PNG 格式的图片: ```csharp public static void GenerateQRCode(string text, int size, string filePath) { var qrCode = new QrCode(); qrCode.Version = QrCodeVersion.AutoVersion; qrCode.ErrorCorrectionLevel = QrCodeErrorCorrectionLevel.Medium; var encoder = new QrEncoder(); encoder.Encode(text, qrCode); var renderer = new GraphicsRenderer(new FixedModuleSize(size, QuietZoneModules.Two)); using (var stream = new FileStream(filePath, FileMode.Create)) { renderer.WriteToStream(qrCode.Matrix, ImageFormats.Png, stream); } } ``` 在这个函数中,我们首先创建了一个 `QrCode` 对象,设置了它的版本和纠错等级。然后,我们使用 `QrEncoder` 对象将文本编码成二维码,并将结果存储在 `QrCode` 对象的 `Matrix` 属性中。接着,我们创建了一个 `GraphicsRenderer` 对象,设置了二维码的尺寸和边缘宽度,并使用 `WriteToStream` 方法将二维码写入 PNG 格式的图片文件中。 最后,您可以在您的代码中调用这个函数来生成二维码并保存: ```csharp var text = "https://www.example.com"; var size = 400; var filePath = "qrcode.png"; GenerateQRCode(text, size, filePath); ``` 需要注意的是,这个函数只能生成单个二维码图片。如果您需要生成多个二维码并打包成压缩文件,您需要编写另外的代码来实现这个功能。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值