/// <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 图片缩略图的更多相关文章

  1. .netcore 图片处理

    .netcore 图片处理 /// <summary> /// 根据文件类型和文件名返回新路径 /// </summary> /// <param name=" ...

  2. NodeJs + gm图片缩略图

    我的另一篇文章: Nginx/Apache图片缩略图技术 gm官网 1, 软件环境 nodejs npm GraphicsMagick or ImageMagick 貌似ImageMagick在处理大 ...

  3. Android 使用MediaStore.Images和 Cursor查询本地图片和图片缩略图

    先看一个实例: String[] projection = { MediaStore.Images.Thumbnails._ID ,MediaStore.Images.Thumbnails.DATA} ...

  4. Nginx/Apache图片缩略图技术

    1,目的 2,使用方式 3,Nginx + Linux 缩略图实现 3.1,原理 3.2,nginx配置实现 3.3,例子 4,Apache + Windows缩略图实现 4.1,环境 4.2,原理 ...

  5. GD库 图片缩略图 图片水印

    /** * GD库 图片缩略图 *//*$image = imagecreatefromjpeg("1.jpg");var_dump($image);exit;$width = i ...

  6. Nginx Image Module图片缩略图 水印处理模块

    Nginx Image Module图片缩略图 水印处理模块 下载Tengine tar -zxvf tengine-1.4.5.tar.gz cd tengine-1.4.5 下载Nginx tar ...

  7. [转帖]Nginx Image Module图片缩略图 水印处理模块

    Nginx Image Module图片缩略图 水印处理模块 https://www.cnblogs.com/jicki/p/5546972.html Nginx Image Module图片缩略图 ...

  8. nginx博客系统(内含nginx图片缩略图处理代码,不错)

    一直以来都在Qzone.CSDN等上面写博客,偶尔有些想法就在Paas平台上搭建服务,新浪和曾经的google上都用过其appengine.可是在别人的平台上写东西,总归有些不方便,有受制于人的感觉. ...

  9. 第二十八篇、自定义的UITableViewCell上有图片需要显示,要求网络网络状态为WiFi时,显示图片高清图;网络状态为蜂窝移动网络时,显示图片缩略图

    1)SDWebImage会自动帮助开发者缓存图片(包括内存缓存,沙盒缓存),所以我们需要设置用户在WiFi环境下下载的高清图,下次在蜂窝网络状态下打开应用也应显示高清图,而不是去下载缩略图. 2)许多 ...

  10. 最蛋疼的bug:读取图片缩略图(一定要在相冊查看下形成缓存)

    近期的一个连接服务端的应用.须要读取图片,一般供用户公布商品选择上传图片.初始的图片列表应该是缩略图.仅仅有确定了,才上传原图,OK不多说上代码 package edu.buaa.erhuo; imp ...

随机推荐

  1. vim 从嫌弃到依赖(5)——普通模式的一些操作

    通过前面几章内容的铺垫,基本已经介绍完了普通模式的大部分内容,按照进度下面会依次介绍插入模式.命令模式.选择模式的一些操作.根据不同模式提供功能的多少和使用频率,篇幅会有长有短.本来这篇文章应该介绍插 ...

  2. 一些提供办公效率的软件(clover、f.lux、幕布),老赞了!

    1.clover 链接:http://cn.ejie.me/ Clover是由异次元的读者ejie团队开发的一款电脑窗口标签化工具.Clover是电脑中资源管理器的一个扩展程序,可以为其增加多标签页的 ...

  3. 5.1 内存CRC32完整性检测

    CRC校验技术是用于检测数据传输或存储过程中是否出现了错误的一种方法,校验算法可以通过计算应用与数据的循环冗余校验(CRC)检验值来检测任何数据损坏.通过运用本校验技术我们可以实现对特定内存区域以及磁 ...

  4. playwright 一些方法解决cloudflare防护页的问题

    在尝试从一个使用Cloudflare Web应用程序防火墙(WAF)保护的网站获取数据时,我遇到了一些挑战.该网站的安全措施非常严格,以至于在正常浏览几个页面后,Cloudflare的检查页面就会出现 ...

  5. 关于debug一晚上的一些思考,做开发到底要养成什么习惯?

    总结:日志一定要写,日志一定要写,日志一定要写! 今天晚上是我学开发过程中很不一样的一晚,今晚学到了很多. 虽然我也只是一个开发的初学小白,今天的debug分享是我的一个小方法和一个小记录,如果大佬们 ...

  6. 打开PDB报错ORA-30013

    多租户架构,之前还在做运维的时期接触也不多.遇到多租户问题,第一反应是有些发虚的. 但实际很多问题很简单,也容易解决.本文就是一个例子. 问题:RAC节点2打开所有PDB时,报错ORA-30013. ...

  7. PostgreSQL-查询每个表的大小

    1.查询数据库中单个表的大小(不包含索引) select pg_size_pretty(pg_relation_size('表名')); 2.查询所有表的大小并排序(包含索引) SELECT tabl ...

  8. 开源.NetCore通用工具库Xmtool使用连载 - 散列算法篇

    [Github源码] <上一篇>详细介绍了Xmtool工具库中的加解密类库,今天我们继续为大家介绍其中的散列算法类库. 散列算法在某些特殊场景也可以当做加密方法使用:其特点是不可逆,同一内 ...

  9. 跨界协作:借助gRPC实现Python数据分析能力的共享

    gRPC是一个高性能.开源.通用的远程过程调用(RPC)框架,由Google推出.它基于HTTP/2协议标准设计开发,默认采用Protocol Buffers数据序列化协议,支持多种开发语言. 在gR ...

  10. axios.delete传参,400错误

    我在使用axios.delete进行传参的时候,发现会报400错误 后端代码(C#) 前端代码 这样的参数请求会报400错误 后端就一个参数,前端发一个id为什么接受不到呢? 在网上找了半天,终于明白 ...