13

I have a 27 x 27 pixel image that I am displaying in WPF but it displays larger than the size of the window.

How can I get it to display its actual size?

alt text http://www.deviantsart.com/upload/m20dk6.png

XAML:

<Window x:Class="TestImage23434.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel x:Name="MainStackPanel"/>
</Window>

Code Behind:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System;

namespace TestImage23434
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            TextBlock tb = new TextBlock();
            tb.Text = "above image ";
            MainStackPanel.Children.Add(tb);

            Image img = new Image();
            img.Source = new BitmapImage(new Uri(@"C:\test\circle.png"));
            img.HorizontalAlignment = HorizontalAlignment.Left;
            img.VerticalAlignment = VerticalAlignment.Top;
            MainStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
            MainStackPanel.VerticalAlignment = VerticalAlignment.Top;
            MainStackPanel.Children.Add(img);

            TextBlock tb2 = new TextBlock();
            tb2.Text = "below image";
            MainStackPanel.Children.Add(tb2);
        }
    }
}
1

4 Answers 4

23

img.Stretch = Stretch.None

By default, the Stretch property has a value of Uniform, which resizes the image to fill all available space.

2
  • 2
    I don't think I've ever used an image in such a way I've had to USE the Stretch property... I forgot it even existed. Commented Dec 3, 2009 at 18:19
  • 2
    if we Set "img.Stretch = Stretch.None" there is problem when the image is too large some portion of image is not get visible.Any solution for this Commented Dec 6, 2013 at 5:47
8

None of these tips were working for me. Some binding tricks is what got me going.

<Image Source="yourPic.png" Stretch="UniformToFill"
Width="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelWidth}"
Height="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelHeight}" />

You could probably set Strech to None but this worked for my app using .net 4.5.1.

2
  • Binding the width and height works well in combination with a static MaxWidth and MaxHeight and Stretch="Uniform" in order to have original size up to some limit. The accepted answer shows only part of the image in such a scenario.
    – grek40
    Commented Jan 18, 2016 at 10:13
  • You can't have Stretch="None" if you want this to work! I lost almost an hour before I realized it. Commented Feb 1, 2020 at 19:58
2
img.HorizontalAlignment = HorizontalAlignment.Left;
img.VerticalAlignment = VerticalAlignment.Top;
img.Stretch = Stretch.None;

The StackPanel will stretch to fill/overflow whatever container it's in unless its size is specified. Since you aren't setting margins or alignments on anything, the default behavior for everything is Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch", or simply put "Stretch to fill". Aligning/positioning your elements will fix this issue.

EDIT: Added img.Stretch = Stretch.None;, also I built a sample app and tested it... it works.

4
  • How does the alignment affect the size of the displayed image?
    – dtb
    Commented Dec 3, 2009 at 17:34
  • The standard alignment is Stretch, which does affect the size.
    – Heinzi
    Commented Dec 3, 2009 at 17:35
  • I tried that but it still expands to the size of its possible area. I tried putting these settings on the StackPanel as well but it still expands. Commented Dec 3, 2009 at 17:36
  • @Heinzi - Thx, was wondering why it got a downvote until I saw your edit... :D Commented Dec 3, 2009 at 17:58
2

It could also be your image's DPI property in its metadata. It should be 96. https://stackoverflow.com/a/4965404/659326

Not the answer you're looking for? Browse other questions tagged or ask your own question.