2

I downloaded the ImageProcessor library using nuget for c#. I am using it to upload and resize image for a website. The upload process works fine except, when I try to view the uploaded image it appears backward rotated 90 from the original image. Here is the code that I am using:

        ISupportedImageFormat format = new JpegFormat { Quality = 70 };

        using (MemoryStream inStream = new MemoryStream(_img))
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                {
                    // Load, resize, set the format and quality and save an image.
                    imageFactory.Load(inStream)
                        .Resize(new ResizeLayer(new Size(width, height), resizeMode: resizeMode))
                                .Format(format)
                                .Save(outStream);
                }

                return outStream.ToArray();
            }
        }
James South
  • 10,147
  • 4
  • 59
  • 115
user1790300
  • 2,143
  • 10
  • 54
  • 123
  • Depending on what software you are using to view the image, some software uses the image's meta-data to rotate the image - and the ImageProcessor might not. Or visa versa. – Ulric Sep 30 '15 at 15:53
  • It is being viewed from a browser. I downloaded the resized image and it was 90 degrees off. – user1790300 Sep 30 '15 at 15:57
  • Try setting the `preserveExifData` to `true` and see if it makes a difference. Other than that, I am out of suggestions. Sorry. – Ulric Sep 30 '15 at 16:05
  • Now I do notice that the picture is actually long ways, don't know if that makes a difference. I twas taken from a phone. – user1790300 Sep 30 '15 at 16:09
  • 1
    Photos normally have Exif meta data. One of those Exif properties is Orientation. This can affect how images are displayed. Try my above suggestion to see if it helps. – Ulric Sep 30 '15 at 16:14
  • Here's a link to a blog complaining about Orientation. It might prove enlightening. :) http://keyj.emphy.de/exif-orientation-rant/ – Ulric Sep 30 '15 at 16:15
  • If the ExIf meta data is the problem, how can I get around it? – user1790300 Sep 30 '15 at 16:31
  • If all the images are meant to be Landscape oriented then you could check if the image's height is greater than its width and if so, rotate the image. – Ulric Sep 30 '15 at 19:30
  • Did you find a solution to this .... when i download a image from web then resize is proper and the image is not rotating but if i upload from my mobile it is rotating to 90 or 180 degree depending on the phone... – XXX Jul 12 '16 at 11:24

1 Answers1

6

If you are not preserving EXIF metadata the ImageFactory class has a method AutoRotate that will alter the image to compensate for the original orientation.

http://imageprocessor.org/imageprocessor/imagefactory/autorotate/

Your new code would be as follows.

ISupportedImageFormat format = new JpegFormat { Quality = 70 };

using (MemoryStream inStream = new MemoryStream(_img))
{
    using (MemoryStream outStream = new MemoryStream())
    {
        // Initialize the ImageFactory using the overload to preserve EXIF metadata.
        using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
        {
            // Load, resize, set the format and quality and save an image.
            imageFactory.Load(inStream)
                        .AutoRotate()
                        .Resize(new ResizeLayer(new Size(width, height), resizeMode: resizeMode))
                        .Format(format)
                        .Save(outStream);
        }

        return outStream.ToArray();
    }
}
James South
  • 10,147
  • 4
  • 59
  • 115