sync with OpenAuth.Core

This commit is contained in:
yubaolee
2020-12-20 23:14:09 +08:00
parent 7217e7a924
commit 7540baa322
25 changed files with 347 additions and 169 deletions

View File

@@ -2,76 +2,49 @@
// Copyright (c) 2019 openauth.me. All rights reserved.
// </copyright>
// <author>www.cnblogs.com/yubaolee</author>
// <date>2019-03-07</date>
// <summary>生成缩略图</summary>
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
namespace Infrastructure.Helpers
{
public class ImgHelper
{
//MakeThumbnail(path, tpath, 120, 90, "H");
public static void MakeThumbnail(string originalImagePath,
/// <summary>
/// 根据已有图片生成缩略图
/// <para>用法MakeThumbnail(path, tpath, 120, 90, "H");</para>
/// </summary>
/// <param name="originalImagePath">源图片路径</param>
/// <param name="thumbnailPath">缩略图保存路径</param>
/// <param name="width">缩略图的宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">缩略模式H:指定高度宽度按比例处理W指定宽度高度按比例处理HW按参数指定的高度和宽度</param>
public static void MakeThumbnail(string originalImagePath,
string thumbnailPath,
int width=120, int height=90, string mode="H")
int width = 120, int height = 90, string mode = "H")
{
//Image originalImage = Image.FromFile(originalImagePath);
//int towidth = width;
//int toheight = height;
//int x = 0;
//int y = 0;
//int ow = originalImage.Width;
//int oh = originalImage.Height;
//switch (mode)
//{
// case "HW"://指定高宽缩放(可能变形)
// break;
// case "W"://指定宽,高按比例
// toheight = originalImage.Height * width / originalImage.Width;
// break;
// case "H"://指定高,宽按比例
// towidth = originalImage.Width * height / originalImage.Height;
// break;
// case "Cut"://指定高宽裁减(不变形)
// if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
// {
// oh = originalImage.Height;
// ow = originalImage.Height * towidth / toheight;
// y = 0;
// x = (originalImage.Width - ow) / 2;
// }
// else
// {
// ow = originalImage.Width;
// oh = originalImage.Width * height / towidth;
// x = 0;
// y = (originalImage.Height - oh) / 2;
// }
// break;
// default:
// break;
//}
//MediaTypeNames.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//g.Clear(Color.Transparent);
//g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
// new Rectangle(x, y, ow, oh),
// GraphicsUnit.Pixel);
//try
//{
// bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
//}
//catch (System.Exception e)
//{
// throw e;
//}
//finally
//{
// originalImage.Dispose();
// bitmap.Dispose();
// g.Dispose();
//}
using (var originalImage = Image.Load(originalImagePath))
{
int towidth = width; //缩略图宽度
int toheight = height; //缩略图高度
switch (mode)
{
case "HW": //指定高宽缩放(可能变形)
break;
case "W": //指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
default:
break;
}
originalImage.Mutate(x => x.Resize(towidth, toheight));
originalImage.Save(thumbnailPath);
}
}
}
}
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -18,6 +18,7 @@
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.2" />
</ItemGroup>
<ItemGroup>