Question

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. Any ideas why?

In SplashWindow.xaml:

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

In SplashViewModel.cs

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

From 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;
}
Was it helpful?

Solution

In your XAML, set the "Stretch" property to "None" (I believe it defaults to "Fill"):

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

You can also explicitly set the Width and Height properties if you like.

OTHER TIPS

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 displays things in device-independent units, specifically 1/96ths of an inch.

Image files have DPI/resolution information in their metadata which tells the computer how big that image is in inches. 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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top