Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Neil 40 posts 205 karma points
    Aug 08, 2023 @ 15:42
    Neil
    0

    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.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 08, 2023 @ 17:00
    Huw Reddick
    0

    I think you need to set imageCropMode in your call to getcropurl

  • Neil 40 posts 205 karma points
    Aug 09, 2023 @ 07:57
    Neil
    0

    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).

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 09, 2023 @ 13:38
    Huw Reddick
    0

    sorry, missed that bit, seems BoxPad is the only one that doesn't mess with it, but that sticks it in a black box

  • Neil 40 posts 205 karma points
    Aug 09, 2023 @ 13:39
    Neil
    0

    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.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 09, 2023 @ 13:55
    Huw Reddick
    0

    Yes, that was what I was about to suggest, you can check the crop dimensions against the actual image and adjust accordingly

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 09, 2023 @ 14:31
    Huw Reddick
    0

    You could also do something like this

    Image img = (Image)publishedimage;
    
    var imageUrl = publishedimage?.GetCropUrl(Math.Min(340,img.UmbracoWidth), Math.Min(255,img.UmbracoHeight)) ;
    
  • Neil 40 posts 205 karma points
    Aug 09, 2023 @ 14:37
    Neil
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft