当前位置:网站首页>. 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
边栏推荐
- C#-Linq源码解析之DefaultIfEmpty
- spark-shuffle的读数据源码分析
- Uniapp wechat applet obtains page QR code (with parameters)
- On the closure function of Scala
- Docker之MySQL主从连接提示:Communications link failure
- 试用了多款报表工具,终于找到了基于.Net 6开发的一个了
- 启牛学堂给的中信建投证券账户是不是真的?开户安全吗
- 【游标的嵌套】mysql存储过程游标的嵌套
- JSP learning (2) -- JSP script elements and instructions
- Summary of spark common operators
猜你喜欢

Vhedt business development framework

Special research on Intelligent upgrading of heavy trucks in China in 2022
![[pop up box at the bottom of wechat applet package] I](/img/28/7cb36ef4fcf753764cc9363a323a20.png)
[pop up box at the bottom of wechat applet package] I

Qt筆記-QMap自定義鍵(key)

How to add a "security lock" to the mobile office of government and enterprises?

Linux system maintenance: mysql8.0.13 source code download and installation "fool" operation steps (Linux centos6.8) test available series

Figure operation flow of HAMA BSP Model
![[C language] deeply analyze the storage of integer and floating-point types in memory](/img/8b/12a4dc7a0c0e34e2add007592971dd.jpg)
[C language] deeply analyze the storage of integer and floating-point types in memory

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

High availability ResourceManager
随机推荐
JMeter use case
Interface (optimization type annotation)
.NetCore实现图片缩放与裁剪 - 基于ImageSharp
Interview knowledge points
On the closure function of Scala
同花顺容易开户么?网上开户安全么?
for.. of vs. for.. In statement
数据库mysql 主从方案
jsp学习之(一)---------jsp概述
Analysis of the read data source code of spark shuffle
JSP learning (2) -- JSP script elements and instructions
调用cmd 进程通信
Redis implements the correct posture of the delay queue
spark的NaiveBayes中文文本分类
Analysis of the writer source code of spark shuffle
启牛学堂给的中信建投证券账户是不是真的?开户安全吗
Spark Streaming-Receiver启动和数据接收
试用了多款报表工具,终于找到了基于.Net 6开发的一个了
代码扫描工具扫出的 Arrays.asList 使用BUG
win10桌面图标消失,工具栏变黑