准备工作
引入如下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))
{

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

被折叠的 条评论
为什么被折叠?



