Search code examples
c#imageresizer

Order of changes/edits when altering an image in ImageResizer


I am using the Nuget package of ImageResizer in a web project. Users can edit their profile image by panning, rotating and zooming the original image within a constrained window size.

In our old ASP classic image editing library we could load a version of the original file in memory and then perform the transformation in memory and save out the required sizes etc. The order of these transformation is important as any rotation will affect the co-ordinates for the pan/crop etc. There doesnt seem to be anyway to control what happens in what order with ImageResizer so I am doing something like this.

WebRequest requestPic = WebRequest.Create("URL-of-original-image");
WebResponse responsePic = requestPic.GetResponse();

var settings = new Instructions();
settings.Rotate = submittedAngleforNewImage;

var imageRotated = new ImageJob(responsePic.GetResponseStream(), "location-to-save-temp-file", settings)
{AddFileExtension = true};
imageRotated.Build();

settings = new Instructions();
settings.CropRectangle = cropCoordsScaledArray;

var imageCropped = new ImageJob(imageRotated.FinalPath, "location-to-save-temp-file", settings)
{AddFileExtension = true};
imageCropped.Build();

Then I build new images, by loading the imageCropped image, in the required sizes (three in this case). So there is a lot of repetition here creating the build job, loading the file from disk etc.

If I try to do the transformations in one go the order is wrong...it seems to crop before doing anything else but I Cant confirm what the order is.

Anyone have any ideas for how to reduce the amount of code and disk reads here or any suggestions as to how I can perform in memory transformations in a set order before saving..?

Thanks


Solution

  • The ImageResizer order of operations is trim whitespace -> srotate -> sflip -> crop -> scale -> filter -> pad -> rotate -> flip. If you save intermediate files, make sure they are in .png format so you don't create extra compression artifacts.

    That said, you might consider Imageflow.NET instead, as it offers control over the order of operations and is much faster. It doesn't yet support rotation degrees that aren't a multiple of 90, however. Do you use partial degree rotations?