0

In WPF c# loading an image from code in runtime, some images are loading when the width and height are switched so the width=height and height=width so the image is turned in 90 degrees. In the file system properties tab, the Width and Height are correct and the image is shown correctly. If I open this image in any imageViewer the image is shown correctly but if I open it in powerpoint the same issue is found and the image is turned. I downloaded some other codes in WPF and all are showing the image turned. most images are shown correctly.

For example This Image : https://www.dropbox.com/s/5fki0ew3gt78myi/TestImage_Widht4000_Height6000.JPG?dl=0

The Widht=4000and Height=6000 but if I get the bitmap.PixelWidth=6000 and bitmap.PixelHeight=400

Can anyone please help! Thanks

I have tried everything :(

           var ImagefilePath = SelectedImagePath + "\\" + ((object[])(e.AddedItems))[0].ToString();

            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(ImagefilePath.ToString(), UriKind.Absolute);

            bitmap.EndInit();

            // Set Image.Source  
            imgPhoto.Source = bitmap;

            ImageInfoText.Content=" W=" + bitmap.PixelWidth + " H=" + bitmap.PixelHeight;
shay golan
  • 21
  • 4
  • Can you show the XAML? – bit Aug 29 '19 at 10:16
  • I found that if I download the file from the link dropbox will fix the image so please try using the image from this link : https://www.dropbox.com/s/axcv5ym9cyxp0l1/TestImage_Widht4000_Height6000.zip?dl=0 – shay golan Aug 29 '19 at 10:20
  • 1
    please insert the images directly to you post. The dropbox link might vanish in future. The picture in the question will remain. – Mat Aug 29 '19 at 10:23
  • As a note, you can simplify your code to `var bitmap = new BitmapImage(new Uri(ImagefilePath, UriKind.Absolute));`. No need for Begin/EndInit when you only set the source Uri. – Clemens Aug 29 '19 at 11:22
  • I can't add the image because when I upload the image it solve the issue and if you download the image the problem is fixed (something is changing in the coding of the image) – shay golan Aug 29 '19 at 14:20

1 Answers1

0

It is most probably the metadata within the image itself. You need to get rid of the metadata and that should fix your problem.

Here is a similar issue : WPF some images are rotated when loaded

Here is how you can remove metadata: Easy way to clean metadata from an image?

The preferable way would be to use the System.Windows.Media.Imaging.BitmapEncoder class's Metadata collection property.

bit
  • 4,407
  • 1
  • 28
  • 50
  • Thanks for your answer but still didn't manage to use the codes in the links you attached in your answer and the similar issue was not solved (he used outside app to convert the image to solve the issue and it will not work for me because I need to load images at runtime like viewer) – shay golan Aug 29 '19 at 14:14