WPF: How to display an image at its original size?

WpfImageResizeSize

Wpf Problem Overview


I have a problem with displaying images in WPF.

Here's my code:

<Button HorizontalAlignment="Left" Grid.Column="1" Grid.Row="5" Margin="0,5">
		<Button.Content>
			<StackPanel Orientation="Horizontal" Margin="10,0">
				<Image Source="/images/user_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" Width="24" Height="24" />
				<TextBlock Text="添加" />
			</StackPanel>
		</Button.Content>
	</Button>

I have an image with original size 32*32, but when I ran the above code, the image will stretch to fill all the space, beyond its original size. I also set the "Stretch" property to "None", but it seems that it doesn't work.

So, how can I fix this problem? Thank you!

Wpf Solutions


Solution 1 - Wpf

Here is a similar question. Generally setting Stretch="None" is enough.

It is also very important what DPI has the image set in metadata. It took me quite a while before figuring out that if the image's DPI is different from the monitor's DPI (usually 96), WPF will automatically resize the image, as it tries to be DPI-independent.


EDIT

The MSDN link is broken, here is the new link: MSDN Blog - Blurry Bitmaps. Let's keep the old link around to be used for archive.org, in case the new link stops working also.

Solution 2 - Wpf

Try not specifying width or height, use it like this instead:

<Image Source="/images/user_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" />

Solution 3 - Wpf

<Image Source="Images/Background.png" UseLayoutRounding="True" SnapsToDevicePixels="True" Width="600" Height="800" Stretch="Fill" />

This one works for me, for an image with 600x800 pixels and 96dpi.

@rishad2m8 If size is unknown one can detect the size first with https://msdn.microsoft.com/en-us/library/system.drawing.image.size(v=vs.110).aspx I'd guess.

Solution 4 - Wpf

Adding to Paya's answer: to compensate WPF's attempt to adapt to the monitors resolution you should be able to set the Width and Height to the file's original dimensions and use Stretch="Fill". This worked for me.

Solution 5 - Wpf

If you want to display the image with original size, but don't know the image size, I think the best way is to set the image as background of UIElement. Like this:

    <Button Height="100" Width="100">
        <Button.Background>
            <ImageBrush ImageSource="/images/user_add.png" Stretch="None"/>
        </Button.Background>
    </Button>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjiluoView Question on Stackoverflow
Solution 1 - WpfPayaView Answer on Stackoverflow
Solution 2 - WpfVoodooChildView Answer on Stackoverflow
Solution 3 - WpfUlrich-Lorenz SchlüterView Answer on Stackoverflow
Solution 4 - WpfsorrymissjacksonView Answer on Stackoverflow
Solution 5 - WpfshirleyView Answer on Stackoverflow