简体   繁体   English

为什么显示的图像更大?

[英]Why is my image being displayed larger?

I'm trying to display an image on a splash screen and it's being stretched upon display. 我正在尝试在初始屏幕上显示图像,并且在显示时会被拉伸。 The image I'm trying to display is a simple bmp file. 我要显示的图像是一个简单的bmp文件。 Any ideas why? 有什么想法吗?

In SplashWindow.xaml: 在SplashWindow.xaml中:

<Window ... SizeToContent="WidthAndHeight">
  <Grid>
    ...
    <Image Grid.Row="0" Source="{Binding SplashImage}"></Image>
  </Grid>
</Window>

In SplashViewModel.cs 在SplashViewModel.cs中

public ImageSource SplashImage
{
  get
  {
    return ImageUtilities.GetImageSource(_splashImageFilenameString);
  }
}

From ImageUtilities.cs 来自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;
}

In your XAML, set the "Stretch" property to "None" (I believe it defaults to "Fill"): 在您的XAML中,将“ Stretch”属性设置为“ None”(我相信它默认为“ Fill”):

<Image Grid.Row="0" Source="{Binding SplashImage}" Stretch="None"></Image>

You can also explicitly set the Width and Height properties if you like. 如果愿意,还可以显式设置Width和Height属性。

typically you want: 通常您想要:

<Image Source="{Binding ImagePath}" Stretch="Uniform" />

this value will enlarge the image as much as possible while still fitting entirely within your parent control. 该值将尽可能放大图像,同时仍完全适合您的父控件。 It will not distort it, it will maintain the source's aspect ratio. 它不会扭曲它,它将保持源的宽高比。 If you use 如果您使用

Stretch="None"

it will display the image (or what fits of the image, it will clip) at it's native size which is not always what you want. 它将以原始尺寸显示图像(或图像的适合程度,将进行裁剪),而原始尺寸并不总是您想要的。

Anyhow, you have some choices but setting Stretch to what you want will effect the way the image stretches or not. 无论如何,您都有一些选择,但是将“拉伸”设置为所需的值将影响图像拉伸与否的方式。

WPF doesn't display things in pixels (at least not on the surface). WPF不会以像素为单位显示事物(至少不在表面上)。 WPF displays things in device-independent units, specifically 1/96ths of an inch. WPF以与设备无关的单位显示事物,特别是1/96英寸。

Image files have DPI/resolution information in their metadata which tells the computer how big that image is in inches. 图像文件的元数据中包含DPI /分辨率信息,可告诉计算机该图像的尺寸(以英寸为单位)。 If your image file has been programmed to say it is 8 inches wide, that's going to be equal to 768 units in WPF, regardless of how many pixels the image is. 如果将您的图像文件编程为8英寸宽,则无论图像有多少像素,这都等于WPF中的768单位。

You can use an image editing program like Photoshop or equivalent to change the DPI of your image, or just give it an explicit Width and Height when you display it in WPF. 您可以使用Photoshop之类的图像编辑程序或类似的程序来更改图像的DPI,或者在WPF中显示图像时仅给它明确的宽度和高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM