当前位置:网站首页>. NETCORE enables image scaling and cropping - based on imagesharp
. NETCORE enables image scaling and cropping - based on imagesharp
2022-06-22 17:04:00 【Dotnet cross platform】
Preface
( I suddenly found that it has been more than a while
When I was blogging recently , Need to implement a similar Lorempixel、LoremPicsum Such random picture function , The picture has , You also need a function to get pictures according to the input width and height , Because before processing pictures, we used ImageSharp library , So this time I immediately thought of using it .
Analyze requirements
The pictures in the picture library are basically Wallpapers I collected before , The sizes are uneven , There are horizontal and vertical screens
Then, you need to input the width and height of the interface , You can randomly select a picture and zoom or crop it
My idea is :
Horizontal screen picture , Adjust the height to match the input height , The width is adjusted proportionally
Vertical screen picture , Adjust the width to match the input height , Adjust the height proportionally
Then select the middle part to intercept
Of course, there are special circumstances , This is when the input width and height exceed the original height and width of the picture , This can only destroy the original scale of the picture , Force stretching ~
PS: I wanted to draw a picture to express my thoughts , But I haven't found any drawing tools (Draw.io: Don't look at me )
Realization
First read the picture
It's simple , Just pass in the picture path
Of course, it can also be read by stream
using var image = Image.Load("imagePath");Then, zoom and crop the image according to its size
The code used by many blogs found on the Internet is similar image.Resize and image.Crop And so on. , But this is an old version ImageSharp Code
The new version is all in image.Mutate in , What operation do you want to import again lambda expression
( It's kind of like ORM The operation of )
For example, scaling is like this
image.Mutate(a => a.Resize(newWidth, newHeight));This is the way of cutting
image.Mutate(a => a.Crop(new Rectangle(x, y, width, height)));Complete code
The function is very simple , The complete code is here
void GetImage(string imagePath, int width, int height) {
using var image = Image.Load(imagePath);
Rectangle cropRect;
int newWidth;
int newHeight;
// Horizontal screen picture
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);
}
// Vertical screen picture
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");
}Follow up in my StarBlog Develop notes series in , Next, we'll update ~
Reference material
Random picture website :https://picsum.photos/
.NetCore How to use ImageSharp Generate pictures :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
边栏推荐
- 调用cmd 进程通信
- hydra安装及使用
- 你管这破玩意儿叫高可用?
- mysql 字符串字段转浮点型字段
- What are the characteristics of the interactive whiteboard? Function introduction of electronic whiteboard
- 快速掌握 ASP.NET 身份认证框架 Identity - 登录与登出
- Hydra installation and use
- QT notes qmap user defined key
- Problems and recovery of spark streaming checkpoint
- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
猜你喜欢

Team management | how to improve the thinking skills of technical leaders?

In the era of video explosion, who is supporting the high-speed operation of video ecological network?

Partage de l'architecture du système de paiement du Groupe letv pour traiter 100 000 commandes simultanées élevées par seconde

JSP learning (2) -- JSP script elements and instructions

Docker之MySQL主从连接提示:Communications link failure

Vhedt business development framework

What is restful and what rules should be followed when designing rest APIs?

Summary of safari compatibility issues

jsp学习之(一)---------jsp概述

Blazor University (31)表单 —— 验证
随机推荐
Spark Streaming checkpoint的问题与恢复
Figure operation flow of HAMA BSP Model
Call CMD process communication
scala-for推导:能够在for表达式中的最初部分定义值,并在(外面)后面的表达式中使用该值
Spark's NaiveBayes Chinese text classification
Gridhome, a must-have static site generator for beginners
每秒处理10万高并发订单的乐视集团支付系统架构分享
使用IDM让百度云加速的方法
Hello Playwright:(7)模拟键盘和鼠标
图计算Hama-BSP模型的运行流程
Qt筆記-QMap自定義鍵(key)
jsp学习之(一)---------jsp概述
jsp学习之(三)--------- jsp隐式对象
.NET 发布和支持计划介绍
同花顺是什么?在线开户安全么?
Test for API
代码扫描工具扫出的 Arrays.asList 使用BUG
ABP Framework 5.3.0 版本新增功能和变更说明
Windows8.1 64 installed by mysql5.7.27
[pop up box at the bottom of wechat applet package] I