Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 8 Image UniformFill centered

I have a little problem, I have a group item which I want to give a background image which it should scale keeping it's correct dimensions but by default it shows the image from the top left, I want the image to be centered.

Here is an illustration to further explain my problem. (the gray part is what is clipped)

My problem

And I have this XAML

<Image Source="/url/to/image.jpg" Stretch="UniformToFill"/> 
like image 832
Mats Hofman Avatar asked Nov 23 '12 19:11

Mats Hofman


2 Answers

I managed to solve my problem, I made the image larger than the container it was placed in and vertical aligned it in the center.

<Grid HorizontalAlignment="Left" Width="250" Height="125">     <Image Source="/url/to/image.jpg" Stretch="UniformToFill" Height="250" Margin="0" VerticalAlignment="Center"/> </Grid> 

The overflow of the image was invisible :)

like image 123
Mats Hofman Avatar answered Oct 19 '22 13:10

Mats Hofman


In case the size of the source image is unknown in advance I had to use different trick:

<Border Width="250" Height="250">     <Border.Background>         <ImageBrush ImageSource="/url/to/image.jpg"                     Stretch="UniformToFill"/>     </Border.Background> </Border> 
like image 26
aguyngueran Avatar answered Oct 19 '22 13:10

aguyngueran