WPF中的图像部分透明度

时间:2013-08-14 21:17:52

标签: c# wpf xaml transparency opacitymask

我将两张图片一个在另一个上面对齐,

我试图在顶部设置一个透明圆圈

出于某种原因,EllipseGeometry Radius和Center属性无效。

这是XAML代码

谢谢!

    <Viewbox Grid.Row="1" Stretch="Uniform" HorizontalAlignment="Center">
        <Image Name="BackImage" Width="640" Height="480" Source="Images/black.png"/>

    </Viewbox>
    <Viewbox Grid.Row="1" Stretch="Uniform" HorizontalAlignment="Center">
        <Image Name="FrontImage" Width="640" Height="480" Source="Images/white.png">
            <Image.OpacityMask>
                <DrawingBrush>
                    <DrawingBrush.Drawing>
                            <GeometryDrawing>
                                <GeometryDrawing.Pen>
                                    <Pen Thickness="0" Brush="Transparent" />
                                </GeometryDrawing.Pen>
                                <GeometryDrawing.Brush>
                                    <SolidColorBrush Color="Black" />
                                </GeometryDrawing.Brush>
                                <GeometryDrawing.Geometry>
                                        <EllipseGeometry Center="0,0" RadiusX="1" RadiusY="3"/>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Image.OpacityMask>
        </Image>

    </Viewbox>

Here is what I try to acheieve

1 个答案:

答案 0 :(得分:3)

为了以绝对坐标指定GeometryDrawing,您必须将DrawingBrush的Stretch属性设置为None

此外,我还建议将两个图像放在一个ViewBox中。

<Viewbox Stretch="Uniform" HorizontalAlignment="Center">
    <Grid>
        <Image Name="BackImage" Width="640" Height="480" Source="Images/black.png"/>
        <Image Name="FrontImage" Width="640" Height="480" Source="Images/white.png">
            <Image.OpacityMask>
                <DrawingBrush Stretch="None">
                    <DrawingBrush.Drawing>
                        <GeometryDrawing Brush="Black">
                            <GeometryDrawing.Geometry>
                                <EllipseGeometry Center="320,240"
                                                 RadiusX="150" RadiusY="100"/>
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Image.OpacityMask>
        </Image>
    </Grid>
</Viewbox>