.NetCore生成海报+二维码(logo)
生活随笔
收集整理的這篇文章主要介紹了
.NetCore生成海报+二维码(logo)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
準備工作
引入如下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;}效果圖
注:圖中二維碼僅供參考
總結
var bitmap = qrCode.GetGraphic(pixel, Color.Black, Color.White,(Bitmap)Image.FromFile(logoPath), 15, 8);
總結
以上是生活随笔為你收集整理的.NetCore生成海报+二维码(logo)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: liunx+python+django框
- 下一篇: JavaScript:实现GnomeSo