## 0.0.2(2021-12-30)

使用SixLabors.ImageSharp替换System.Drawing,全面拥抱跨平台,减少安装libgdiplus的过程。组件功能更加丰富。
升级uview至2.0.19版本。进一步减小包大小。
取消全部WebRequest,替换为HttpClient。
nuget更新大批组件升级到6.0版本
This commit is contained in:
JianWeie
2021-12-30 02:24:46 +08:00
parent 60344f6381
commit 03a93a50a4
66 changed files with 661 additions and 544 deletions

View File

@@ -10,11 +10,9 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using CoreCms.Net.Caching.AccressToken;
@@ -31,6 +29,12 @@ using CoreCms.Net.WeChat.Service.Options;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
@@ -854,7 +858,6 @@ namespace CoreCms.Net.Services
//文件硬地址
var qrCodeDir = _webHostEnvironment.WebRootPath + "/static/qrCode/weChat/" + otherData;
System.Drawing.Image qrCodeImage = System.Drawing.Image.FromFile(qrCodeDir);
//获取数据来源
var dataObj = JObject.FromObject(data);
@@ -881,58 +884,67 @@ namespace CoreCms.Net.Services
if (images.Any())
{
var image = images[0];
//创建画布
//创建 带二维码的图片 大小的 位图
Bitmap tmpImage = new Bitmap(400, 600);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(tmpImage);
//下面这个设成High
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
//清除整个绘图面并以背景色填充
//g.Clear(Color.Transparent);// 透明(看到的可能是黑色)
g.Clear(Color.White); //绘制白色
//创建一个背景宽度为400X600的底图
using var imageTemple = new SixLabors.ImageSharp.Image<Rgba32>(400, 600);
//设置底图的背景色为白色
imageTemple.Mutate(x => x.BackgroundColor(SixLabors.ImageSharp.Color.White));
//绘制商品图片(网络下载图片)
System.Net.WebRequest request = System.Net.WebRequest.Create(image);
System.Net.WebResponse response = request.GetResponse();
Stream reader = response.GetResponseStream();
if (reader != null)
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(image);
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync();
//载入下载的图片流
var imageThumbnail = await SixLabors.ImageSharp.Image.LoadAsync(stream);
//将下载的图片压缩至400X400
imageThumbnail.Mutate(x =>
{
System.Drawing.Image imgHeadPhoto = System.Drawing.Image.FromStream(reader);
g.DrawImage(imgHeadPhoto, 0, 0, 400, 400);
imgHeadPhoto.Dispose();
}
reader.Close();
reader.Dispose();
//绘制分享二维码
g.DrawImage(qrCodeImage, 275, 420, 120, 120);
//绘制商品名称
Font titleFont = new Font("微软雅黑", 14);
RectangleF descRect = new RectangleF();
descRect.Location = new Point(10, 460);
descRect.Size = new Size(260, ((int)g.MeasureString(goodModel.name, titleFont, 260, StringFormat.GenericTypographic).Height));
g.DrawString(goodModel.name, titleFont, Brushes.Black, descRect);
//g.DrawString(goodModel.name, titleFont, new SolidBrush(Color.Black), new PointF(10, 430));
x.Resize(400, 400);
});
//将商品大图合并到背景图上
imageTemple.Mutate(x => x.DrawImage(imageThumbnail, new SixLabors.ImageSharp.Point(0, 0), 1));
//将用户的分享二维码合并大背景图上
var imageQrcode = await SixLabors.ImageSharp.Image.LoadAsync(qrCodeDir);
//将二维码缩略至120X120
imageQrcode.Mutate(x =>
{
x.Resize(120, 120);
});
//将二维码图片合并到背景图上
imageTemple.Mutate(x => x.DrawImage(imageQrcode, new SixLabors.ImageSharp.Point(275, 420), 1));
//构建字体//装载字体(ttf)(而且字体一定要支持简体中文的)
var fonts = new FontCollection();
SixLabors.Fonts.FontFamily fontFamily = fonts.Install(_webHostEnvironment.WebRootPath + "/fonts/SourceHanSansK-Normal.ttf");
//商品名称可能较长,设置为多行文本输出
SixLabors.Fonts.Font titleFont = new SixLabors.Fonts.Font(fontFamily, 20, SixLabors.Fonts.FontStyle.Regular);
//多行文本输出
var textOptions = new TextOptions()
{
ApplyKerning = true,
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
WrapTextWidth = 230
};
var graphicsOptions = new GraphicsOptions()
{
Antialias = true
};
//沿着行尾的绕行路径绘制文本
var options = new SixLabors.ImageSharp.Drawing.Processing.DrawingOptions
{
GraphicsOptions = graphicsOptions,
TextOptions = textOptions
};
//开始绘制商品名称
imageTemple.Mutate(ctx => ctx.DrawText(options, goodModel.name, titleFont, SixLabors.ImageSharp.Color.Red, new SixLabors.ImageSharp.PointF(10, 450)));
//绘制商品金额
Font moneyFont = new Font("微软雅黑", 18);
g.DrawString("¥" + goodModel.price, moneyFont, new SolidBrush(Color.Crimson), new PointF(10, 420));
SixLabors.Fonts.Font moneyFont = new SixLabors.Fonts.Font(fontFamily, 18);
//获取该文件绘制所需的大小
imageTemple.Mutate(ctx => ctx.DrawText("¥" + goodModel.price, moneyFont, SixLabors.ImageSharp.Color.Crimson, new SixLabors.ImageSharp.PointF(10, 410)));
//绘制提示语
Font tipsFont = new Font("微软雅黑", 8);
g.DrawString("扫描或长按识别二维码", tipsFont, new SolidBrush(Color.Black), new PointF(278, 555));
//释放资源 并保存要返回 位图
qrCodeImage.Dispose();
//图片压缩
SaveImage2File(_webHostEnvironment.WebRootPath + fileName, tmpImage, 90);
g.Dispose();
tmpImage.Dispose();
SixLabors.Fonts.Font tipsFont = new SixLabors.Fonts.Font(fontFamily, 10);
imageTemple.Mutate(ctx => ctx.DrawText("扫描或长按识别二维码", tipsFont, SixLabors.ImageSharp.Color.Black, new SixLabors.ImageSharp.PointF(283, 555)));
//载入流存储在到文件
await imageTemple.SaveAsync(_webHostEnvironment.WebRootPath + fileName);
return true;
}
@@ -943,32 +955,6 @@ namespace CoreCms.Net.Services
return false;
}
/// <summary>
/// 将Image实例保存到文件,注意此方法不执行 img.Dispose()
/// 图片保存时本可以直接使用destImage.Save(path, ImageFormat.Jpeg),但是这种方法无法进行进一步控制图片质量
/// </summary>
/// <param name="path"></param>
/// <param name="img"></param>
/// <param name="quality">1~100整数,无效值则取默认值95</param>
/// <param name="mimeType"></param>
private void SaveImage2File(string path, Image destImage, int quality, string mimeType = "image/jpeg")
{
if (quality <= 0 || quality > 100) quality = 95;
//创建保存的文件夹
FileInfo fileInfo = new FileInfo(path);
if (!Directory.Exists(fileInfo.DirectoryName))
{
Directory.CreateDirectory(fileInfo.DirectoryName);
}
//设置保存参数,保存参数里进一步控制质量
EncoderParameters encoderParams = new EncoderParameters();
long[] qua = new long[] { quality };
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获取指定mimeType的mimeType的ImageCodecInfo
var codecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(ici => ici.MimeType == mimeType);
destImage.Save(path, codecInfo, encoderParams);
}
}
}