简体   繁体   中英

C# WPF when image is loaded the width and height are switched

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM