当前位置:网站首页>. 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
边栏推荐
- UI自动化定位利器-xpath实战
- Examples of MySQL account addition, deletion, modification, data import and export commands
- Idea installation summary
- 快速掌握 ASP.NET 身份认证框架 Identity - 登录与登出
- Spark Streaming-Receiver启动和数据接收
- mysql5.7.27安装之windows8.1 64
- Spark on data skew
- jsp学习之(一)---------jsp概述
- scala-for推导:能够在for表达式中的最初部分定义值,并在(外面)后面的表达式中使用该值
- 新手必会的静态站点生成器——Gridsome
猜你喜欢

Qt Notes - qmap Custom key

IDEA安装总结

STM32通过DMA进行ADC采集(HAL库)

jsp學習之(二)---------jsp脚本元素和指令

Apache ShardingSphere 一文读懂

试用了多款报表工具,终于找到了基于.Net 6开发的一个了

【进阶自动化测试第一步】1分钟带你了解自动化测试

Short video source code development, high-quality short video source code need to do what?
![[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)
随机推荐
Test for API
Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
0基础如何入门软件测试,能转行成功吗?
The win10 desktop icon disappears and the toolbar turns black
STM32 ADC acquisition via DMA (HAL Library)
[C language] deeply analyze the relationship between pointer and array
scala-for推导:能够在for表达式中的最初部分定义值,并在(外面)后面的表达式中使用该值
Vs2017 solution to not displaying qstring value in debugging status
同花顺是什么?在线开户安全么?
NLog自定义Target之MQTT
【阿里云服务器-安装mysql的5.6版本安装,重装】
What are the characteristics of the interactive whiteboard? Function introduction of electronic whiteboard
Scala for derivation: the ability to define a value in the first part of a for expression and use it in subsequent (outer) expressions
Web technology sharing | [Gaode map] to realize customized track playback
scala-for的基本应用
MySQL string field to floating point field
UI自动化定位利器-xpath实战
win10桌面图标消失,工具栏变黑
spark关于数据倾斜问题
招行23型号UKey在win7上无法识别