Imagesharp: Wrong result file resolution

Created on 30 May 2017  路  4Comments  路  Source: SixLabors/ImageSharp

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am running the latest version of ImageSharp
  • [x] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [x] I have searched open and closed issues to ensure it has not already been reported

Description

I have an image with resolution 2304x1528 (ratio: 1.50785...). I want to save it with width 300px preserving original aspect ratio. However passing height = 0 to ResizeOptions.Size, and setting mode Min/Maxmakes result image with width 299px.
To workaround the issue I calc height manualy like that (using Ceiling method before conversion to int) and pass it to Size:

Convert.ToInt32(Math.Ceiling(resizeOptions.Width / image.PixelRatio))

https://github.com/JimBobSquarePants/ImageSharp/blob/master/src/ImageSharp/Processing/Transforms/Options/ResizeHelper.cs#L410

Steps to Reproduce

using (Image<Rgba32> image = Image.Load("foo.jpg"))
{
    image
        .Resize(new ResizeOptions {
            Size = new Size(300, 0),
            Mode = ResizeMode.Max
        })
        .Save("bar.jpg");
}

System Configuration

  • ImageSharp version: 1.0.0-alpha9-00054 - 1.0.0-alpha9-00089
  • Other ImageSharp packages and versions:
  • Environment (Operating system, version and so on): Windows 10, MacOS
  • .NET Framework version: .net core
  • Additional information:

Most helpful comment

my guess is you have an image that's been rotated using metadata, I bet that if you inspected the image in both Explorer and ImageSharp before the resize it will also have the dimensions inverted.

I recon is you change your code to
c# using (Image<Rgba32> image = Image.Load("foo.jpg")) { image .AutoOrient() // <- fixes the pixel data orientation if the metadata differs to it. .Resize(new ResizeOptions { Size = new Size(300, 0), Mode = ResizeMode.Max }) .Save("bar.jpg"); }

All 4 comments

Hey @vad3x thanks for the raising the issue and providing a great code sample for reproduction.

I've pushed a fix for the issue just now so you should now get the correct values.

Cheers

James

Hi @JimBobSquarePants, thanks for the fast response!
The case works well now, but now when I resizing image 1528x2304, I'm getting wierd result.
The image var seems well to me (take a look at width, height):
screen
However, after saving:
screen2

Could you please check it, and correct me if I'm wrong?

my guess is you have an image that's been rotated using metadata, I bet that if you inspected the image in both Explorer and ImageSharp before the resize it will also have the dimensions inverted.

I recon is you change your code to
c# using (Image<Rgba32> image = Image.Load("foo.jpg")) { image .AutoOrient() // <- fixes the pixel data orientation if the metadata differs to it. .Resize(new ResizeOptions { Size = new Size(300, 0), Mode = ResizeMode.Max }) .Save("bar.jpg"); }

@tocsoft you are right, I've rotated the image using MS Photos app, I did not know that the app changes only metadata.

Thanks Scott.

Was this page helpful?
0 / 5 - 0 ratings