当前位置:网站首页>.NetCore实现图片缩放与裁剪 - 基于ImageSharp
.NetCore实现图片缩放与裁剪 - 基于ImageSharp
2022-06-22 15:36:00 【dotNET跨平台】
前言
(突然发现断更有段时间了
最近在做博客的时候,需要实现一个类似Lorempixel、LoremPicsum这样的随机图片功能,图片有了,还需要一个根据输入的宽度高度获取图片的功能,由于之前处理图片时使用到了ImageSharp库,所以这次我立刻就想到用它。
分析需求
图片库中的图片基本都是我之前收集的壁纸什么的,尺寸参差不齐,有横屏的也有竖屏
然后包装成接口只需要输入宽度和高度,就能随机选一张图片然后进行缩放或者裁剪
我的思路是:
横屏图片,将高度调整到与输入高度一致,宽度按比例调整
竖屏图片,将宽度调整到与输入高度一致,高度按比例调整
然后再选取中间部分进行截取
当然还有特殊情况,就是输入的宽度和高度超过图片原来高度宽度的情况,这个只能破坏图片原有的比例,强行进行拉伸~
PS:本来想画个图表达一下思路,不过没找到趁手的画图工具(Draw.io:不要看我)
实现
首先读取图片
很简单,传入图片路径即可
当然也可以用流的方式读取
using var image = Image.Load("imagePath");然后就是根据图片的大小各种情况来进行缩放和裁剪
在网上查到的很多博客用的代码都是类似image.Resize和image.Crop之类的,但这是旧版的ImageSharp代码
新版全都放在image.Mutate里,要什么操作再传入lambda表达式
(有点像ORM的操作)
比如缩放就是这样
image.Mutate(a => a.Resize(newWidth, newHeight));裁剪就是这样
image.Mutate(a => a.Crop(new Rectangle(x, y, width, height)));完整代码
功能很简单,完整代码在此
void GetImage(string imagePath, int width, int height) {
using var image = Image.Load(imagePath);
Rectangle cropRect;
int newWidth;
int newHeight;
// 横屏图片
if (image.Width > image.Height) {
if (width > image.Width) {
newWidth = width;
newHeight = height;
}
else {
newHeight = height;
newWidth = image.Width / image.Height * newHeight;
}
cropRect = new Rectangle((newWidth - width) / 2, 0, width, height);
}
// 竖屏图片
else {
if (height > image.Height) {
newWidth = width;
newHeight = height;
}
else {
newWidth = width;
newHeight = newWidth * image.Height / image.Width;
}
cropRect = new Rectangle(0, (newHeight - height) / 2, width, height);
}
image.Mutate(a => a.Resize(newWidth, newHeight));
image.Mutate(a => a.Crop(cropRect));
image.SaveAsPng("output.png");
}后续在我的StarBlog开发笔记系列里,接下来会更新~
参考资料
随机图片网站:https://picsum.photos/
.NetCore如何使用ImageSharp进行图片的生成:https://www.cnblogs.com/niwan/p/11126239.html
https://stackoverflow.com/questions/63639644/how-load-an-image-from-disk-and-save-to-a-stream-using-imagesharp-while-preservi
边栏推荐
- LETV group payment system architecture sharing for processing 100000 high concurrent orders per second
- jsp学习之(三)--------- jsp隐式对象
- Figure operation flow of HAMA BSP Model
- Qt Notes - qmap Custom key
- How to use IDM to accelerate Baidu cloud
- Purchase guide - how to purchase a high-quality conference tablet, these aspects must be compared
- Oracle database and table
- Test for API
- Examples of MySQL account addition, deletion, modification, data import and export commands
- 从Application提交角度审视Executor
猜你喜欢
随机推荐
多线程里面不能注入Service或者Mapper
spark常用 算子小总结
5 modes of IO model
Problems and recovery of spark streaming checkpoint
JMeter use case
Short video source code development, high-quality short video source code need to do what?
Interface (optimization type annotation)
Why buy increased life insurance? Is increased life insurance safe and reliable?
Learning about ABAP program tuning (IV) loop where key
Spark and mysql:did not find registered driver with class com mysql. jdbc. Driver
[C language] use of library function qsort
[wechat applet custom bottom tabbar]
jsp學習之(二)---------jsp脚本元素和指令
IDEA安装总结
Test for API
Purchase guide - how to purchase a high-quality conference tablet, these aspects must be compared
jsp学习之(二)---------jsp脚本元素和指令
Summary of safari compatibility issues
团队管理|如何提高技术 Leader 的思考技巧?
有同学问PHP要学什么框架?









