mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-11-07 18:04:45 +08:00
sync with OpenAuth.Core
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user