【问题标题】:WPF some images are rotated when loadedWPF 某些图像在加载时会旋转
【发布时间】:2013-12-30 22:49:27
【问题描述】:

我是 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

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

【问题讨论】:

  • 我看不出上面的代码有什么问题。
  • 我无法用代码重现它。你能展示在 WPF 图像控件中查看原始图像时的不同之处吗?
  • 是否任何比例变换或任何其他变换应用于其父级?
  • 请张贴原图文件
  • @user2957509,我怀疑图像在其元数据中指定了非默认方向,但事实并非如此。我尝试在 WPF 应用程序中显示它,并且显示正确(无旋转)。

标签: c# wpf xaml


【解决方案1】:

由于位图元数据, 这是我的代码, 它可以正常工作

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多