0

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...

intrixius
  • 1,096
  • 2
  • 11
  • 25
  • 1
    You are talking about **resize**, and you share code about `PrepareImageToUpload`. Can you explain the correlation? – Luuk Sep 23 '22 at 07:05
  • There's no resize operation in this code. Setting `DecodePixelWidth` is *not* a resize operation. `BitmapImage` is *not* meant for image operations, it's meant for displaying images in WPF applications – Panagiotis Kanavos Sep 23 '22 at 07:06
  • Adding very dirty: myBitmapImage.Rotation = Rotation.Rotate180; // ;) – SndrSchnklshk Sep 23 '22 at 07:10
  • @PanagiotisKanavos , thanks for the info. These images are indeed being edited in a WPF application before being uploaded; I guess I should dive into how to resize images correctly. I thought the DecodePixelWidth does resize images... – intrixius Sep 23 '22 at 07:30
  • As the property's docs say, it specifies the size of image to load (decode). Some formats, eg PNG, can store multiple resolutions. The docs say that if the format doesn't support it, the value is ignored – Panagiotis Kanavos Sep 23 '22 at 07:32
  • @PanagiotisKanavos, then I am probably completely wrong on how to resize my (jpg) images... – intrixius Sep 23 '22 at 07:34
  • 1
    For *editing* images it's probably better to use a library made for this, like ImageSharp. BitmapImage is built for *displaying* on a WPF form and the transforms applied to it are the same transforms (and animations) you apply through XAML. Check [this resizing example](https://docs.sixlabors.com/articles/imagesharp/resize.html) using ImageSharp. Image processing libraries are far easier to use and yet far more powerful – Panagiotis Kanavos Sep 23 '22 at 07:37
  • @Luuk, before I upload the image to the online service (using `MyFileUploader`) I try to downsize the images, hence the "decodepixelwidth", but that seems not doing what I though it would do – intrixius Sep 23 '22 at 07:37
  • 1
    Using ImageSharp the code you need is just `image.Mutate(x => x.Resize(width, height)); image.Save(outPath);`. You can also specify different samplers, play with options like cropping and more – Panagiotis Kanavos Sep 23 '22 at 07:39

0 Answers0