WPF 某些图像在加载时会旋转

use*_*509 4 c# wpf xaml

我是 WPF 新手,找不到解决方法。我有一个在 XAML 中定义的基本图像控件。我正在将位图图像动态加载到此控件。问题是,当加载图像控件时,某些位图在图像控件中翻转,并且我想以其默认方向加载所有图像。

这是我的 XAML:

<StackPanel Width="Auto" DockPanel.Dock="Left" Margin="1,1,0,1" Orientation="Vertical" Background="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" HorizontalAlignment="Left" VerticalAlignment="Stretch" Height="Auto" >
   <Image x:Name="Photo"  Stretch="Uniform"  Height="149" Width="134" Margin="21,25,0,20" HorizontalAlignment="Left" VerticalAlignment="Top" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

代码:

BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(child.profilPoto.path, UriKind.Absolute);
img.EndInit();
Photo.Source = img;
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑:

这是图像的链接:img左侧有一个如何在 WPF 中显示图像的信息。右侧是原始图像。

编辑2: 原始图像

已解决, 谢谢大家。这是元数据问题。我有很多照片的元数据很糟糕。当我尝试从其他相机加载图像时,一切正常。在某些编辑器中保存损坏的图像后,照片可以正确加载。

小智 8

因为 BitmapMetadata,这是我的代码,它可以正常工作

private static string _orientationQuery = "System.Photo.Orientation";
public static BitmapSource LoadImageFile(String path)
{
  Rotation rotation = Rotation.Rotate0;
  using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
  {
    BitmapFrame bitmapFrame = BitmapFrame.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
    BitmapMetadata bitmapMetadata = bitmapFrame.Metadata as BitmapMetadata;

    if ((bitmapMetadata != null) && (bitmapMetadata.ContainsQuery(_orientationQuery)))
    {
      object o = bitmapMetadata.GetQuery(_orientationQuery);

      if (o != null)
      {
        switch ((ushort)o)
        {
          case 6:
            {
              rotation = Rotation.Rotate90;
            }
            break;
          case 3:
            {
              rotation = Rotation.Rotate180;
            }
            break;
          case 8:
            {
              rotation = Rotation.Rotate270;
            }
            break;
        }
      }
    }
  }

  BitmapImage _image = new BitmapImage();
  _image.BeginInit();
  _image.UriSource = new Uri(path);
  _image.Rotation = rotation;
  _image.EndInit();
  _image.Freeze();

  return _image;
}
Run Code Online (Sandbox Code Playgroud)