2

I am using the SixLabors.ImageSharp to programatically crop an image in C# .NET Core 3.1. Below you can find a working code snippet.

public static void ResizeImage(Image<Rgba32> input, Size dimensions)
{
    var options = new ResizeOptions
    {
      Size = dimensions,
      Mode = ResizeMode.Crop
    };

    input.Mutate(x => x.Resize(options));
}

It works really well, but I would like to allow the user to crop the image based on a pair of given coordinates. Meaning that, the cropping would start from those coordinates, and not from the origin (0, 0). Is it possible to do so with this tool?

So far I could only crop starting from an image corner. I want to be able to crop starting from any position. For example, for the following image:

John Doe

A user wants to crop the central part of the picture, by shifting the cropping in the x and y axis. Final result would be:

enter image description here

Notice that I have cut the corners of the image, in the given example. Is it possible to do so with Imagesharp?

ccoutinho
  • 3,308
  • 5
  • 39
  • 47

2 Answers2

6

Use Rectangle.FromLTRB

using (var inputStream = File.OpenRead(Path.Combine(inPath, "john-doe.png")))
using (var image = Image.Load<Rgba32>(inputStream))
{
    // Generate some rough coordinates from the source.
    // We'll take 25% off each edge.
    var size = image.Size();
    var l = size.Width / 4;
    var t = size.Height / 4;
    var r = 3 * (size.Width / 4);
    var b = 3 * (size.Height / 4);

    image.Mutate(x => x.Crop(Rectangle.FromLTRB(l, t, r, b)));

    image.Save(Path.Combine(outPath, "john-doe-cropped.png"));
}
James South
  • 10,147
  • 4
  • 59
  • 115
-1

Even though James' answer pointed me in the right direction, and also before in our brief conversation in the Imagesharp's gitter discussion, what solved the problem for me was the following code:

private static void ResizeImage(Image<Rgba32> input, int width, int height, int x, int y)
    {
        input.Mutate(img => img.Crop(Rectangle.FromLTRB(x, y, width+x, height+y)));
    }

In this code, I am shifting the original image in the x and y axis, and cropping the image by the given width and height.

ccoutinho
  • 3,308
  • 5
  • 39
  • 47
  • 2
    You've posted the same critical piece of code as my answer and marked yourself as the answer. Poor form. – James South Jun 04 '20 at 10:22
  • @JamesSouth Does your answer crop an image starting from a given coordinate? – ccoutinho Jun 04 '20 at 10:52
  • Yes. The critical line is this one. `image.Mutate(x => x.Crop(Rectangle.FromLTRB(l, t, r, b)));` – James South Jun 04 '20 at 11:17
  • yes, but don't you reckon that the correlation between `l` and `r`, and between `t` and `b` should be highlighted in an approved answer? I don't know the API, and finding that out wasn't obvious. And I actually don't understand the transformations you're making in your answer – ccoutinho Jun 04 '20 at 11:34
  • 1
    The supplied code was a complete sample. The parameters `l`,`t`,`r`,`b` are correlated, they match the parameter names of the method. The inline variable are simply demonstrating how to calculate those variables as a percentage. I do know the API because I wrote the API. – James South Jun 05 '20 at 15:07