Why do images rotate when resized?

Viewed 41

I am uploading images (jpg, edited in my WPF application), but first I want to make sure the images are being resized to a max width (in order to save storage). My current code does downsize big images, but rotates all images to landscape; some images, originaly already in landscape, end up being rotated 180 degrees...

This is my current code:

    public void PrepareImageToUpload(string fileLocation)
        {

            BitmapImage myBitmapImage = new BitmapImage();
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(fileLocation);
            myBitmapImage.CacheOption = BitmapCacheOption.None;
            myBitmapImage.DecodePixelWidth = 2000;
            myBitmapImage.EndInit();

            byte[] data;
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(myBitmapImage));
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
                data = ms.ToArray();
            }


            string filename = Path.GetFileName(fileLocation);
            string body = Convert.ToBase64String(data);
            string mimeType = "image/jpeg";


            MyFileUploader(fileName, body, mimeType);
        }

What should I change in order to resize the image but keeping the orientation?

UPDATE: It seems I was using the wrong method to resize the images. I ended up using ImageSharp to resize, which keeps the orientation correctly...

0 Answers
Related