Does anyone know how to stop ImageSharp from upscaling images when using ImageCropMode.Crop?
If I request, say, GetCropUrl(800, 600) and the underlying image is 1000x1000, I get the correct URL for a scaled and cropped image.
But if the underlying image was only 400x400, it upscales the image to 800x800 before cropping to 800x600.
Ideally I'd like it to pass a parameter to preserve the requested aspect ratio, but without upscaling (in my example, this would make an image 400x300).
Not sure if this is a limitation of ImageSharp, or I just can't find documentation for it anywhere.
In the end I wrote my own overload for GetCropUrl that recalculates the crop-box size, maintaining aspect ratio if the image is below the requested sizes.
Umbraco / ImageSharp upscaling
Does anyone know how to stop ImageSharp from upscaling images when using ImageCropMode.Crop?
If I request, say, GetCropUrl(800, 600) and the underlying image is 1000x1000, I get the correct URL for a scaled and cropped image.
But if the underlying image was only 400x400, it upscales the image to 800x800 before cropping to 800x600.
Ideally I'd like it to pass a parameter to preserve the requested aspect ratio, but without upscaling (in my example, this would make an image 400x300).
Not sure if this is a limitation of ImageSharp, or I just can't find documentation for it anywhere.
I think you need to set imageCropMode in your call to getcropurl
I am setting ImageCropMode, to ImageCropMode.Crop. I did mention that in my opening sentence, but I get how my psuedo-code is a bit misleading.
The issue is that this always scales to the max dimension before cropping (and ImageCropMode.Min/Max only scale, they dont crop).
sorry, missed that bit, seems BoxPad is the only one that doesn't mess with it, but that sticks it in a black box
Thought so.
In the end I wrote my own overload for GetCropUrl that recalculates the crop-box size, maintaining aspect ratio if the image is below the requested sizes.
Little clunky, but it does the job.
Yes, that was what I was about to suggest, you can check the crop dimensions against the actual image and adjust accordingly
You could also do something like this
That wouldn't preserve the aspect ratio, however.
If you wanted a 4:3 image (say 800x600), but the provided image was 600x1000 for example, you'd end up with 600x600.
is working on a reply...