0

I am populating a ViewBox with an image, although I cannot seem to get it to center on the image. I've tried setting the ViewBox Fill to UniformToFill, centering the Image vertically and horizontally, etc and nothing seems to work.

XAML

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <Viewbox MaxWidth="500" MaxHeight="500" Name="vb1">
        <Image x:Name="MyImage"/>
    </Viewbox>    
</StackPanel>

XAML.CS

BitmapImage bitimg = new BitmapImage(new Uri("/MyNewLink.jpg", UriKind.Relative));
MyImage.Source = bitimg as ImageSource;
Matthew
  • 3,976
  • 15
  • 66
  • 130
  • I can't speak for `WindowsPhone`, but in WPF, there's absolutely nothing wrong with your used code. I couldn't get the image to *not* be centred, no matter how much I resized the `Window`. How big is your image? – Sheridan Jun 27 '14 at 13:52
  • why you are setting image source in Code behind as well? – Sajeetharan Jun 27 '14 at 13:53
  • @Sheridan the Image is rather large, it comes from PhotoChooserTask so the size depends on the user's device camera settings. – Matthew Jun 27 '14 at 14:29

1 Answers1

0

Set your VerticalAlignment and HorizontalAlignment of ViewBox. Also you would have to set the content alignment of the Image.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396