c# - 如何使用 ImageSharp 调整中心大小并裁剪图像

标签 c# imagesharp

我需要转换一些基于 System.Drawing 的代码才能使用此 .NET Core 兼容库:

https://github.com/SixLabors/ImageSharp

下面基于 System.Drawing 的代码调整图像大小并裁剪边缘,返回要保存的内存流。这可以通过 ImageSharp 库实现吗?

private static Stream Resize(Stream inStream, int newWidth, int newHeight)
{
    var img = Image.Load(inStream);
    if (newWidth != img.Width || newHeight != img.Height)
    {
        var ratioX = (double)newWidth / img.Width;
        var ratioY = (double)newHeight / img.Height;
        var ratio = Math.Max(ratioX, ratioY);
        var width = (int)(img.Width * ratio);
        var height = (int)(img.Height * ratio);

        var newImage = new Bitmap(width, height);
        Graphics.FromImage(newImage).DrawImage(img, 0, 0, width, height);
        img = newImage;

        if (img.Width != newWidth || img.Height != newHeight)
        {
            var startX = (Math.Max(img.Width, newWidth) - Math.Min(img.Width, newWidth)) / 2;
            var startY = (Math.Max(img.Height, newHeight) - Math.Min(img.Height, newHeight)) / 2;
            img = Crop(img, newWidth, newHeight, startX, startY);
        }
    }

    var ms = new MemoryStream();
    img.Save(ms, ImageFormat.Jpeg);
    ms.Position = 0;
    return ms;
}

private static Image Crop(Image image, int newWidth, int newHeight, int startX = 0, int startY = 0)
{
    if (image.Height < newHeight)
        newHeight = image.Height;

    if (image.Width < newWidth)
        newWidth = image.Width;

    using (var bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb))
    {
        bmp.SetResolution(72, 72);
        using (var g = Graphics.FromImage(bmp))
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight), startX, startY, newWidth, newHeight, GraphicsUnit.Pixel);

            var ms = new MemoryStream();
            bmp.Save(ms, ImageFormat.Jpeg);
            image.Dispose();
            var outimage = Image.FromStream(ms);
            return outimage;
        }
    }
}

最佳答案

是的, super 简单。

using (var inStream = ...)
using (var outStream = new MemoryStream())
using (var image = Image.Load(inStream, out IImageFormat format))
{
    image.Mutate(
        i => i.Resize(width, height)
              .Crop(new Rectangle(x, y, cropWidth, cropHeight)));

    image.Save(outStream, format);
}

编辑 如果您想保持原始图像不变,可以使用Clone方法。

using (var inStream = ...)
using (var outStream = new MemoryStream())
using (var image = Image.Load(inStream, out IImageFormat format))
{
    var clone = image.Clone(
                    i => i.Resize(width, height)
                          .Crop(new Rectangle(x, y, cropWidth, cropHeight)));

    clone.Save(outStream, format);
}

您甚至可以将其优化为对 Resize 的单个方法调用通过接受带有“ResizeMode.Crop”的 ResizeOptions 实例的重载。这将允许您调整大小到一个比例,然后剪掉该比例之外的任何多余内容。

关于c# - 如何使用 ImageSharp 调整中心大小并裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55298428/

相关文章:

c# - C# 中的基本数据结构

polygon - 将高斯模糊或像素化应用于多边形

c# - 如何合并两张图片?

c# - 将 ImageUniqueID 插入 JPEG 的 EXIF 数据而不使用 .NET 修改图像

c# - ImageSharp.Web 与 AzureBlobStorageImageProvider 给出 404

c# - 不是那个懒惰的正则表达式匹配吗?

c# - 为什么我的编码显示两次?

c# - 使用 SendMessage 模拟鼠标点击

应用程序池的 C# .NET 单例生活

asp.net-core-mvc - 有没有办法使用 ImageSharp 验证图像的透明度?