## 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,7 +10,6 @@
using System;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
@@ -31,6 +30,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.FileProviders;
using Qiniu.Storage;
using Qiniu.Util;
using SixLabors.ImageSharp;
using ToolGood.Words;
namespace CoreCms.Net.Services
@@ -302,8 +302,11 @@ namespace CoreCms.Net.Services
var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + ".jpg";
var today = DateTime.Now.ToString("yyyyMMdd");
Image mImage = Image.FromStream(memStream);
Bitmap bp = new Bitmap(mImage);
byte[] data = new byte[memStream.Length];
memStream.Seek(0, SeekOrigin.Begin);
memStream.Read(data, 0, Convert.ToInt32(memStream.Length));
SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(new MemoryStream(data));
var saveUrl = options.Path + today + "/";
var dirPath = _webHostEnvironment.WebRootPath + saveUrl;
@@ -322,7 +325,8 @@ namespace CoreCms.Net.Services
var filePath = dirPath + newFileName;
var fileUrl = saveUrl + newFileName;
bp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);//注意保存路径
//保存到图片
image.SaveAsync(filePath);
return bucketBindDomain + fileUrl;
}

View File

@@ -6,13 +6,15 @@
<ItemGroup>
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.13.0" />
<PackageReference Include="Essensoft.Paylink.WeChatPay" Version="4.0.7" />
<PackageReference Include="Essensoft.Paylink.WeChatPay" Version="4.0.8" />
<PackageReference Include="Flurl.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<PackageReference Include="Qc.YilianyunSdk" Version="1.0.7" />
<PackageReference Include="Qiniu" Version="8.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.4" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta13" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="Tencent.QCloud.Cos.Sdk" Version="5.4.25" />
<PackageReference Include="ToolGood.Words" Version="3.0.2.8" />

View File

@@ -20,6 +20,7 @@ using CoreCms.Net.Model.ViewModels.Sms;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using Flurl.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
@@ -154,12 +155,19 @@ namespace CoreCms.Net.Services
/// <param name="mobile"></param>
/// <param name="contentBody"></param>
/// <param name="smsOptions">配置文件</param>
public string SendSms(string mobile, string contentBody, SMSOptions smsOptions)
public async Task<string> SendSms(string mobile, string contentBody, SMSOptions smsOptions)
{
if (smsOptions.Enabled)
{
string param = $@"action=send&userid={smsOptions.UserId}&account={smsOptions.Account}&password={smsOptions.Password}&content={"" + smsOptions.Signature + "" + contentBody}&mobile={mobile}";
var str = HttpHelper.PostSend(smsOptions.ApiUrl, param);
var str = await smsOptions.ApiUrl.PostUrlEncodedAsync(new
{
action = "send",
userid = smsOptions.UserId,
account = smsOptions.Account,
password = smsOptions.Password,
mobile = mobile,
content = "【" + smsOptions.Signature + "】" + contentBody,
}).ReceiveString();
return str;
}
else

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);
}
}
}

View File

@@ -20,6 +20,7 @@ using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using Flurl.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
@@ -248,12 +249,12 @@ namespace CoreCms.Net.Services
/// </summary>
/// <param name="cardCode"></param>
/// <returns></returns>
public WebApiCallBack BankCardsOrganization(string cardCode)
public async Task<WebApiCallBack> BankCardsOrganization(string cardCode)
{
var jm = new WebApiCallBack();
var url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=" + cardCode + "&cardBinCheck=true";
var res = HttpHelper.PostSend(url, "");
var res = await url.GetStringAsync();
var resObj = JObject.Parse(res);
if (!resObj.ContainsKey("validated"))
{