Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 338
jiluo Avatar asked Jun 16 '10 17:06

jiluo


2 Answers

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.

like image 60
Paya Avatar answered Oct 23 '22 20:10

Paya


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

<Image Source="/images/user_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
like image 45
VoodooChild Avatar answered Oct 23 '22 19:10

VoodooChild