asp.net-core - 使用 SixLabors 从中心裁剪和方形图像

标签 asp.net-core

我可以使用 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/

相关文章:

c# - ASP.NET 核心 3.1 |如何使用服务集合进行单元测试并使用 NullLogger 作为 ILogger 实现

c# - 正则表达式验证属性无法正常工作

c# - 未映射强类型配置设置

asp.net-core - ServiceStack - 即使使用 JsConfig,年、月和日参数也描述了无法表示的日期时间

asp.net - 未处理的拒绝(错误): Could not load settings for 'WebPortal' - ASP. NET Core React

c# - 在 C# 的 StackExchange.Redis 中将什么用作 asyncState 对象(或从哪里获取它)

asp.net-core - MVC网络核心3.1 : Tool to create and download PDF file

c# - 请求匹配多个操作,导致 ASP.NET 5/MVC 6 中具有不同参数的操作不明确

visual-studio - 在ASP.NET Core的stacktrace中包括文件名和行号

c# - ASP.NET Core - 如何检测 IHostedService 是否在运行时崩溃?我可以重新启动它吗?