我是WPF的新手,无法找到一些解决方法。我有一个在XAML中定义的基本图像控件。我正在动态地将位图图像加载到此控件。问题是一些位图在Image控件中被翻转,当它被加载时我想用它的默认方向加载所有图像。
这是我的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#code:
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
解决 谢谢大家。这是元数据问题。我有很多带有错误元数据的照片。当我试图从其他相机加载图像时,没关系。在某些编辑器中保存损坏的图像后,照片正确加载。
答案 0 :(得分:1)
由于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;
}