4

我需要在 .NET Core 中裁剪图像。我使用了 ImageSharp、CoreCompat 和 Microsoft.Windows.Compatibility,并扫描了我能找到的所有方法。但是,我仍然无法找到裁剪图像的方法。调整大小在那里,但没有裁剪。

如何根据左上角的像素位置、宽度和高度在 .NET Core 中裁剪图像?

4

4 回答 4

2

我建议使用ImageSharpCoreCompat.System.Drawing 如果您使用的是 ImageSharp,我认为这是裁剪图像的正确方法;通过 Mutate() 方法使用 ImageProcessors:

int width = 0;
int height = 0;
image.Mutate(ctx => ctx.Crop(width, height));

您需要以下命名空间来访问它:

using SixLabors.ImageSharp.Processing;
于 2018-09-04T16:02:23.147 回答
1

只需安装以下软件包:

System.Drawing.Common package

并使用以下代码:

using SD = System.Drawing;

static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
    try
    {
        using (SD.Image OriginalImage = SD.Image.FromFile(Img))
        {
            using (SD.Bitmap bmp = new SD.Bitmap(Width, Height))
            {
                bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);

                using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
                {
                    Graphic.SmoothingMode = SmoothingMode.AntiAlias;

                    Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

                    Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;

                    Graphic.DrawImage(OriginalImage, new SD.Rectangle(0, 0, Width, Height), X, Y, Width, Height, SD.GraphicsUnit.Pixel);

                    MemoryStream ms = new MemoryStream();

                    bmp.Save(ms, OriginalImage.RawFormat);

                    return ms.GetBuffer();

                }
            }
        }
    }
    catch (Exception Ex)
    {
        throw (Ex);
    }
}

这个答案基于这里的一篇很棒的文章。但是,只需安装上述软件包,即可在 .net Core 2.x 及更高版本下工作。顺便说一句,其他著名的包也使用相同的核心库。

如您所见,与其他答案中提到的著名库相比,您在裁剪方面具有更大的灵活性。

于 2019-04-30T17:52:55.647 回答
1

您可以使用ImageSharp 库。它具有基于左上角裁剪图像的方法。看下面的例子:

private async static Task CropImage()
{
    using (var httpClient = new HttpClient())
    {
        var response = await httpClient.GetAsync("https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png")
            .ConfigureAwait(false);

        using (var inputStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
        {
            using (var image = Image.Load(inputStream))
            {
                var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "foo.png");

                image.Clone(
                    ctx => ctx.Crop(560, 300)).Save(path);
            }
        }
    }
}
于 2018-08-23T13:23:57.057 回答
0

首先安装包“System.Drawing.Common”。

using System.Drawing;
using System.Drawing.Imaging;

...

public byte[] Crop(string path, int width, int height)
{
    Image img = Image.FromFile(path);
    Bitmap bmp = new Bitmap(width, height);
    bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(img, 0, 0);
    using (MemoryStream ms = new MemoryStream())
    {
        bmp.Save(ms, ImageFormat.Jpeg);
        return ms.ToArray();
    }
}
于 2021-08-31T07:51:03.000 回答