简体   繁体   English

如何在ViewBox中居中放置图像

[英]How to Center Image in ViewBox

I am populating a ViewBox with an image, although I cannot seem to get it to center on the image. 我正在用图像填充ViewBox,尽管我似乎无法使其在图像上居中。 I've tried setting the ViewBox Fill to UniformToFill, centering the Image vertically and horizontally, etc and nothing seems to work. 我尝试将ViewBox Fill设置为UniformToFill,将Image垂直和水平居中居中,等等,似乎没有任何效果。

XAML XAML

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

XAML.CS XAML文件

BitmapImage bitimg = new BitmapImage(new Uri("/MyNewLink.jpg", UriKind.Relative));
MyImage.Source = bitimg as ImageSource;

Set your VerticalAlignment and HorizontalAlignment of ViewBox. 设置您的ViewBox的VerticalAlignmentHorizo​​ntalAlignment Also you would have to set the content alignment of the Image. 另外,您还必须设置图像的内容对齐方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM