我可以使用 SixLabors 图形处理库从原点裁剪图像 - 但无法弄清楚如何使用它从中心裁剪。
这是我到目前为止所拥有的:
public string ResizeToHeight(string filePath, float height, string targetDirectoryName)
{
string rootDirectoryPath = new DirectoryInfo(filePath).Parent.Parent.FullName;
var processingPathDirectory = Path.Combine(rootDirectoryPath, Constants.PROCESSING_FOLDER);
var processingPathFile = Path.Combine(processingPathDirectory, Path.GetFileName(filePath));
var side = Convert.ToInt32(height);
using (Image<Rgba32> image = Image.Load(filePath))
{
var fractionalChange = height/image.Height;
var newWidth = Convert.ToInt32(image.Width * fractionalChange);
var newHeight = Convert.ToInt32(image.Height * fractionalChange);
image.Mutate(x => x
.Resize(newWidth, newHeight));
image.Save(processingPathFile); // Automatic encoder selected based on extension.
}
using (Image<Rgba32> image = Image.Load(processingPathFile))
{
var difference = Math.Abs(image.Height-image.Width);
if(image.Height>image.Width)
{
var y = difference/2;
image.Mutate(x => x
.Crop(side, side));
//I'd like to do something like
//image.Mutate(x=>x
//.Crop(0,difference, side,side)
//so that the rectangular image is cropped from the center
image.Save(processingPathFile);
}
}
//etc
}
非常感谢任何帮助。
最佳答案
您可以使用以下代码从图像中心裁剪:
using (Image<Rgba32> image = Image.Load(outStream))
{
image.Mutate(x => x
.Crop(new Rectangle((image.Width - side) / 2, (image.Height -side) / 2, side ,side ))
);
//...
}
关于asp.net-core - 使用 SixLabors 从中心裁剪和方形图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57446629/