Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 957
Robert Koritnik Avatar asked Nov 05 '14 14:11

Robert Koritnik


People also ask

Which tool is used for resizing an image?

BeFunky is a great free image resize tool that offers a lot of options. You can resize your image by width or height or by percentage scale.


1 Answers

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.

like image 135
Lilith River Avatar answered Oct 18 '22 13:10

Lilith River