Search code examples
c#wpfimageimagesource

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. 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;
}

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.