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