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 ...
随机推荐
- 策略模式学习,使用go实现策略模式
策略模式 定义 优点 缺点 使用场景 代码实现 策略模式和工厂模式的区别 参考 策略模式 定义 策略模式定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化,不会影响到客户端的使用 ...
- 使用CAShapeLayer,UIBezierPath,CAGradientLayer构建边框颜色会旋转的六边形
主要思路是: 1.使用UIBezierPath绘制一个六边形路径 2.创建一个CAShapeLayer图层,将这个六边形path设置到CAShapeLayer属性上.然后设置fillColor为透明, ...
- 14代i5-14600K现身:多核性能提升多达11%
14代酷睿桌面端还未发售,就陆续在跑分平台上露出. 平台规格为Z790主板.32GB DDR5-5200内存,酷睿i5-14600K的单核成绩为2819,多核成绩为16666,对比酷睿i5-13600 ...
- 盘点下4个Winform UI开源控件库
今天一起来盘点下4个Winform UI开源控件库,有.Net Framework,也有.Net Core. 1.支持.Net 7的开源UI组件框架 项目简介 这是一个基于.Net Framework ...
- 双端队列(deque)--python
Python中的双端队列(deque)是一种特殊的数据结构,它允许在队列的两端进行插入和删除操作12.双端队列可以看成栈和队列的结合3.在Python中,我们可以使用collections模块中的de ...
- C# 重绘图片.图片加字,加矩形,加圆,加线,根据XY坐标修改RGB
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using System.Ne ...
- npm 为 指定组织下的包 配置数据源 .npmrc配置
之前公司搭了一个 npm 服务器用于发布自己的包,本地可以使用 nrm 切换数据源并安装成功,但是到了 jenkins 部署的时候就下载失败了,解决办法如下: 1.在根目录下创建 npm 配置文件,文 ...
- IDEA 2024.1:Spring支持增强、GitHub Action支持增强、更新HTTP Client等
有段时间没有更新IDEA了,早上看到 IntelliJ IDEA 2024.1 EAP 5发布的邮件提示,瞄了一眼,发现真的是越来越强了,其中不少功能对我来说还是非常有用的.也许这些能力对关注DD的小 ...
- NC24755 [USACO 2010 Dec S]Apple Delivery
题目链接 题目 题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course ...
- gif 制作
gif 制作 博文中使用 gif 有时比纯粹的图片更明了.比如展示"墨刀"中的动画效果: 录制视频 首先利用录制视频,例如使用在线录制工具 vizard. Tip:需要花费2分钟手 ...