为什么我的图像显示得更大
本文关键字:显示 我的 图像 为什么 | 更新日期: 2023-09-27 18:36:16
我正在尝试在初始屏幕上显示图像,并且在显示时被拉伸。 我尝试显示的图像是一个简单的 bmp 文件。 知道为什么吗?
在 SplashWindow.xaml 中:
<Window ... SizeToContent="WidthAndHeight">
<Grid>
...
<Image Grid.Row="0" Source="{Binding SplashImage}"></Image>
</Grid>
</Window>
在 SplashView 模型中.cs
public ImageSource SplashImage
{
get
{
return ImageUtilities.GetImageSource(_splashImageFilenameString);
}
}
来自ImageUtilities.cs
public static ImageSource GetImageSource(string imageFilename)
{
BitmapFrame bitmapFrame = null;
if(!string.IsNullOrEmpty(imageFilename))
{
if(File.Exists(imageFilename))
{
bitmapFrame = BitmapFrame.Create(new Uri(imageFilename));
}
else
{
Debug.Assert(false, "File " + imageFilename + " does not exist.");
}
}
return bitmapFrame;
}
在 XAML 中,将"拉伸"属性设置为"无"(我相信它默认为"填充"):
<Image Grid.Row="0" Source="{Binding SplashImage}" Stretch="None"></Image>
如果需要,还可以显式设置"宽度"和"高度"属性。
通常您需要:
<Image Source="{Binding ImagePath}" Stretch="Uniform" />
此值将尽可能放大图像,同时仍完全适合父控件。 它不会扭曲它,它会保持源的纵横比。 如果您使用
Stretch="None"
它将以原始大小显示图像(或图像的合适程度,它将剪辑),这并不总是您想要的。
无论如何,您有一些选择,但将拉伸设置为您想要的将影响图像拉伸的方式。
WPF 不以像素为单位显示内容(至少不在表面上)。 WPF 以与设备无关的单位(特别是 1/96 英寸)显示内容。
图像文件的元数据中包含 DPI/分辨率信息,这些信息告诉计算机该图像的大小(以英寸为单位)。 如果你的图像文件被编程为8英寸宽,那么无论图像有多少像素,这将等于WPF中的768个单位。
您可以使用图像编辑程序(如 Photoshop)或等效程序来更改图像的 DPI,或者在 WPF 中显示图像时为其指定显式宽度和高度。