Resizing and cropping images using ImageResizer
Asked Answered
C

1

6

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?

Cailly answered 5/11, 2014 at 14:49 Comment(2)
When you say it produces nothing, do you mean that resultStream.Length == 0? You'll naturally need to re-seek the memory stream to 0 after ImageResizer has written to it.Mathura
@ComputerLinguist: I assumed that readers of the stream initially set position to start to prevent something like this to happen. Let me try it right away and see what happens.Cailly
M
11

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.

Mathura answered 6/11, 2014 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.