Search code examples
c#imageresizerresize-crop

Resizing and cropping images using ImageResizer


I'm trying to resize and then square-crop incoming images. I have my image in a ReadOnlyStream and would like to output to MemoryStream.

I'm using ImageResizer library to do this.

I'd like my images to first reduce in size and then center-square-crop them. I'm using this code, but it doesn't produce what I require. It produces nothing...

var resultStream = new MemoryStream();
ImageJob job = new ImageJob(imageStream, resultStream, new Instructions {
    Width = 100,
    Height = 100,
    Mode = FitMode.Crop
});
job.Build();

This code should downsample large images and crop them based on library defaults (center cropping).

I didn't provide any specific configuration in web.config because as I understand things it's not required.

What am I doing wrong?


Solution

  • ImageResizer does not reset the output stream position to 0 after writing to it, as this would break non-seekable write streams like HttpResponseStream.

    You need to call resultStream.Seek(0, SeekOrigin.Begin); before reading from it.