如何在 XAML 中将图像裁剪为椭圆

How to clip image to ellipse in XAML

在这篇文章中:https://msdn.microsoft.com/en-us/library/windows/apps/jj206957%28v=vs.105%29.aspx 展示了一种将图像剪裁成椭圆的方法,但是当我将其复制到我的项目时,出现错误提示我不能使用椭圆几何,因为预期类型是 "RectangleGeometry"。我正在构建一个 Windows Phone 8 应用程序。

这篇文章有什么问题还是我遗漏了什么?

我的 xaml 代码示例:

<Border BorderThickness="1" BorderBrush="AliceBlue">
        <Grid Margin="{StaticResource GridMargin}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="2*"></ColumnDefinition>
                <ColumnDefinition Width="1.5*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Stretch="Uniform" Source="{Binding Photo}">
                <Image.Clip>
                    <EllipseGeometry RadiusX="100" RadiusY="100" Center="225,175"/>
                </Image.Clip>
            </Image>
            <Grid Grid.Column="1" Margin="{StaticResource SmallGridMargin}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1.5*"></RowDefinition>
                    <RowDefinition Height="2*"></RowDefinition>
                    <RowDefinition Height="1*"></RowDefinition>
                </Grid.RowDefinitions>
                <Viewbox Stretch="Uniform" Grid.Row="0">
                    <TextBlock Text="{Binding BookAuthor}"></TextBlock>
                </Viewbox>
                <Viewbox Stretch="Uniform" Grid.Row="1">
                    <TextBlock Text="{Binding BookTitle}"></TextBlock>
                </Viewbox>
                <Viewbox Stretch="Uniform" Grid.Row="2">
                    <TextBlock Text="{Binding Id}"></TextBlock>
                </Viewbox>
            </Grid>

MSDN 说:

The clipping geometry for UIElement.Clip in the Windows Runtime API must be a RectangleGeometry. You can't specify a non-rectangular geometry, as is permitted in some XAML frameworks like Microsoft Silverlight.

但是您可以使用 Ellipse 和 ImageBrush 元素作为填充画笔来获得相同的结果:

<Ellipse Width="100" Height="100">
    <Ellipse.Fill>
       <ImageBrush ImageSource="{Binding Photo}"/>
    </Ellipse.Fill>
</Ellipse>