Azure webjob image resize issue

137 Views Asked by At

I am using azure web job to save thumb of an image and this is code for doing this

ImageProcessor.Imaging.Formats.FormatBase f;
f = new ImageProcessor.Imaging.Formats.JpegFormat();


Size size = new Size(200, 200); 
using (WebClient client = new WebClient())
            {
                MemoryStream stream = new MemoryStream(client.DownloadData(input));
                MemoryStream stream2 = new MemoryStream();
                int quality = 110;


                do
                {
                    quality = quality - 10;
                    using (ImageFactory factory = new ImageFactory(false))
                    {
                        factory.Load(stream)
                            .Format(f)
                            .Resize(size)                          
                            //.BackgroundColor(Color.White)
                            .Quality(quality)
                            .Save(stream2);
                    }
                } while (stream2.Length > stream.Length || stream2.Length ==100000);

When i add message to queue with this image for example, job should give me thumb image 200 * 200 but there is trailing black area in top and bottom of resulted image like this which shouldn't be done Why this is done ???

1

There are 1 best solutions below

1
On BEST ANSWER

You're trying to fit a rectangle into a square.

From http://imageprocessor.org/imageprocessor/imagefactory/resize/:

Resize

Resizes the current image to the given dimensions. If EXIF metadata is to be preserved the information contained within will also be updated to match.

public ImageFactory Resize(Size size)

Parameters

size:

The System.Drawing.Size containing the width and height to set the image to.
Passing a 0 for either dimension will omit that dimension.

So, pass in 0 as height to maintain aspect ratio,

Size size = new Size(200, 0);

or use one of the other resize modes: Crop/Min/Max/Stretch