c# - WPF 某些图像在加载时旋转

标签 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>

C#代码:

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

谢谢

编辑:

这是图片链接:img左侧是图像在 WPF 中的显示方式。右边是原图。

编辑 2: Original image

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

最佳答案

因为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;
}

关于c# - WPF 某些图像在加载时旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20848861/

相关文章:

c# - UWP - 来自不同样式资源字典的引用 StaticResource : Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'

c# - 在 DataGrid WPF 中查找文本框

c# - 使用连接选择多个表中的多个项目

c# - 使用 C# HttpListener 处理多个请求

带接口(interface)的 C# 类型联合

c# - 如何获取TextBox的真实高度?

c# - 加载到 richtextbox wpf c# 后,rtf 布局发生了变化

c# - 在生成的 EF 类中添加 INotifyPropertyChange

c# - 如果其中一个 subview 的值在 Windows 8.1 应用程序中更新,如何更新父 ViewModel 中的值

c# - WPF 组合框 : Different template in textbox and drop-downlist