netcore 图片缩略图
/// <summary>
/// 取小写文件名后缀
/// </summary>
/// <param name="name">文件名</param>
/// <returns>返回小写后缀,不带“.”</returns>
public static string GetFileExt(string name)
{
return name.Split(".").Last().ToLower();
} /// <summary>
/// 是否为图片文件
/// </summary>
/// <param name="fileExt">文件扩展名,不含“.”</param>
public static bool IsImage(string fileExt)
{
ArrayList al = new ArrayList { "bmp", "jpeg", "jpg", "gif", "png", "ico" };
return al.Contains(fileExt);
} /// <summary>
/// 检查是否允许文件
/// </summary>
/// <param name="fileExt">文件后缀</param>
/// <param name="allowExt">允许文件数组</param>
public static bool CheckFileExt(string fileExt, string[] allowExt)
{
return allowExt.Any(t => t == fileExt);
} /// <summary>
/// 制作缩略图
/// </summary>
/// <param name="original">图片对象</param>
/// <param name="newFileName">新图路径</param>
/// <param name="maxWidth">最大宽度</param>
/// <param name="maxHeight">最大高度</param>
public static void ThumbImg(Image original, string newFileName, int maxWidth, int maxHeight)
{
Size newSize = ResizeImage(original.Width, original.Height, maxWidth, maxHeight);
using (Image displayImage = new Bitmap(original, newSize))
{
try
{
displayImage.Save(newFileName, original.RawFormat);
}
finally
{
original.Dispose();
}
}
} /// <summary>
/// 制作缩略图
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="newFileName">新图路径</param>
/// <param name="maxWidth">最大宽度</param>
/// <param name="maxHeight">最大高度</param>
public static void ThumbImg(string fileName, string newFileName, int maxWidth, int maxHeight)
{
byte[] imageBytes = File.ReadAllBytes(fileName);
Image img = Image.FromStream(new MemoryStream(imageBytes));
ThumbImg(img, newFileName, maxWidth, maxHeight);
} /// <summary>
/// 计算新尺寸
/// </summary>
/// <param name="width">原始宽度</param>
/// <param name="height">原始高度</param>
/// <param name="maxWidth">最大新宽度</param>
/// <param name="maxHeight">最大新高度</param>
/// <returns></returns>
private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
{
if (maxWidth <= 0)
maxWidth = width;
if (maxHeight <= 0)
maxHeight = height;
decimal MAX_WIDTH = maxWidth;
decimal MAX_HEIGHT = maxHeight;
decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT; int newWidth, newHeight;
decimal originalWidth = width;
decimal originalHeight = height; if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
{
decimal factor;
if (originalWidth / originalHeight > ASPECT_RATIO)
{
factor = originalWidth / MAX_WIDTH;
newWidth = Convert.ToInt32(originalWidth / factor);
newHeight = Convert.ToInt32(originalHeight / factor);
}
else
{
factor = originalHeight / MAX_HEIGHT;
newWidth = Convert.ToInt32(originalWidth / factor);
newHeight = Convert.ToInt32(originalHeight / factor);
}
}
else
{
newWidth = width;
newHeight = height;
}
return new Size(newWidth, newHeight);
} /// <summary>
/// 得到图片格式
/// </summary>
/// <param name="name">文件名称</param>
/// <returns></returns>
public static ImageFormat GetFormat(string name)
{
string ext = GetFileExt(name);
switch (ext)
{
case "ico":
return ImageFormat.Icon;
case "bmp":
return ImageFormat.Bmp;
case "png":
return ImageFormat.Png;
case "gif":
return ImageFormat.Gif;
default:
return ImageFormat.Jpeg;
}
}
报错
2019-05-09 10:27:01,330 线程ID:[80] 日志级别:ERROR 出错类:WebApp.HttpGlobalExceptionFilter property:[(null)] - 错误描述:System.TypeInitializationException: The type initializer for 'System.DrawingCore.GDIPlus' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'gdiplus' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libgdiplus: cannot open shared object file: No such file or directory
at System.DrawingCore.GDIPlus.GdiplusStartup(UInt64& token, GdiplusStartupInput& input, GdiplusStartupOutput& output)
at System.DrawingCore.GDIPlus..cctor()
--- End of inner exception stack trace ---
at System.DrawingCore.GDIPlus.GdipGetGenericFontFamilySansSerif(IntPtr& fontFamily)
at System.DrawingCore.FontFamily..ctor(GenericFontFamilies genericFamily)
at System.DrawingCore.FontFamily.get_GenericSansSerif()
at System.DrawingCore.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical)
at Common.VerifyCodeHelper.CreateByteByImgVerifyCode(String verifyCode, Int32 width, Int32 height) in F:\src\WebApp\Common\VerifyCodeHelper.cs:line 206
at WebApp.Controllers.VerifyCodeController.NumberVerifyCode() in F:\src\WebApp\Controllers\VerifyCodeController.cs:line 27
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()
解决方法
Centos 7
yum install libgdiplus-devel
netcore 图片缩略图的更多相关文章
- .netcore 图片处理
.netcore 图片处理 /// <summary> /// 根据文件类型和文件名返回新路径 /// </summary> /// <param name=" ...
- NodeJs + gm图片缩略图
我的另一篇文章: Nginx/Apache图片缩略图技术 gm官网 1, 软件环境 nodejs npm GraphicsMagick or ImageMagick 貌似ImageMagick在处理大 ...
- Android 使用MediaStore.Images和 Cursor查询本地图片和图片缩略图
先看一个实例: String[] projection = { MediaStore.Images.Thumbnails._ID ,MediaStore.Images.Thumbnails.DATA} ...
- Nginx/Apache图片缩略图技术
1,目的 2,使用方式 3,Nginx + Linux 缩略图实现 3.1,原理 3.2,nginx配置实现 3.3,例子 4,Apache + Windows缩略图实现 4.1,环境 4.2,原理 ...
- GD库 图片缩略图 图片水印
/** * GD库 图片缩略图 *//*$image = imagecreatefromjpeg("1.jpg");var_dump($image);exit;$width = i ...
- Nginx Image Module图片缩略图 水印处理模块
Nginx Image Module图片缩略图 水印处理模块 下载Tengine tar -zxvf tengine-1.4.5.tar.gz cd tengine-1.4.5 下载Nginx tar ...
- [转帖]Nginx Image Module图片缩略图 水印处理模块
Nginx Image Module图片缩略图 水印处理模块 https://www.cnblogs.com/jicki/p/5546972.html Nginx Image Module图片缩略图 ...
- nginx博客系统(内含nginx图片缩略图处理代码,不错)
一直以来都在Qzone.CSDN等上面写博客,偶尔有些想法就在Paas平台上搭建服务,新浪和曾经的google上都用过其appengine.可是在别人的平台上写东西,总归有些不方便,有受制于人的感觉. ...
- 第二十八篇、自定义的UITableViewCell上有图片需要显示,要求网络网络状态为WiFi时,显示图片高清图;网络状态为蜂窝移动网络时,显示图片缩略图
1)SDWebImage会自动帮助开发者缓存图片(包括内存缓存,沙盒缓存),所以我们需要设置用户在WiFi环境下下载的高清图,下次在蜂窝网络状态下打开应用也应显示高清图,而不是去下载缩略图. 2)许多 ...
- 最蛋疼的bug:读取图片缩略图(一定要在相冊查看下形成缓存)
近期的一个连接服务端的应用.须要读取图片,一般供用户公布商品选择上传图片.初始的图片列表应该是缩略图.仅仅有确定了,才上传原图,OK不多说上代码 package edu.buaa.erhuo; imp ...
随机推荐
- 微信小程序-双线程渲染模型
微信小程序双线程渲染模型 小程序的运行环境分成渲染层和逻辑层: WXML 模板和 WXSS 样式工作在渲染层,通过 WebView 进行渲染 小程序会为每一个界面都创建一个 WebView 来渲染这个 ...
- QQ音乐转 mp3 格式的链接,不需要下载任何软件
一.打开QQ音乐的官网 QQ音乐-千万正版音乐海量无损曲库新歌热歌天天畅听的高品质音乐平台! 二.随便找一首歌播放 三.打开后,就可以使用了
- 制作包含最新更新的Windows 10 LTSC 2021 ISO
介绍 在制作桌面云windows 模板的时候,一般需要安装最新的更新.更新安装过程非常耗时,并且安装更新会导致桌面模板的磁盘空间膨胀.制作出的模板会占用很大的磁盘空间.如果不安装更新,模板大小约5G. ...
- 从CPU100%高危故障到稳定在10%:一个月的优化之旅,成功上线!
引言 经过三个月的开发,项目通过了所有测试并上线,然而,我们发现项目的首页几乎无法打开,后台一直发生超时错误,导致CPU过度负荷.在这次项目开发过程中,我制定了一份详细的技术优化方案.考虑到客户无法提 ...
- 使用XAG配置GoldenGate在RAC集群环境中的高可用
背景:本文是根据实际客户测试需求整理,因为客户OGG所在环境只有GI集群,数据库部署在其他位置,所以会有一些差异,但核心思路一致,已完全测试通过,整理出来供大家参考. 1.前期准备 2.创建ACFS文 ...
- Delphi中的注释,仅此一篇
在Pascal中,注释括在大括号中或带星号的圆括号中.Delphi 也认可C++ 风格的注释,即把注释放在双斜线后.例如: {this is a comment} (* this is another ...
- ASP.NET Core分布式项目实战(Consent视图制作)--学习笔记
任务19:Consent视图制作 按照上一节 Consent 的思路 在 mvcCookieAuthSample 项目的 Controllers 文件夹下新建一个 ConsentController ...
- docker离线安装及设置默认存储目录
一.离线安装Docker 在内网环境下,一般不能联网在线部署,这时候就需要以离线的方式安装docker.本文介绍在CentOS 7.6环境中离线安装docker的步骤. 1. 下载docker安装包 ...
- 2023 NOI春季测试游记
前言: 知周所众,在 2023.3.4 ,圈钱因部分地区 NOIP 趋势了而组织举办了春测,这个蒟蒻本来没有 NOIP 的资格,但却去上了春测. 3.2 晚上jijidawang来发表,结果少无数份( ...
- STM32F401的PWM输出
PWM的说明 PWM有三个关键指标: PWM频率, 占空比, 区分度 对于同一个时钟频率下工作的单片机, 区分度是和PWM工作频率相关的, 因为总频率是固定的, PWM工作频率越高, 留下给区分度的部 ...