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:
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:
Notice that I have cut the corners of the image, in the given example. Is it possible to do so with Imagesharp?
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
.