填充矩形的外部

Ben*_*Ben 10 .net c# wpf rectangles fill

我想在WPF(通过代码)中绘制一个矩形并填充它的外部.

这是一个例子:

在此输入图像描述

矩形的外部是灰色的(低不透明度),矩形的填充是透明的.

Cle*_*ens 7

您还Path可以使用半透明元素覆盖图像,该元素使用CombinedGeometry将一个非常大的外部矩形与内部矩形组合在一起的半透明元素:

<Grid>
    <Image Name="image" Source="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"/>
    <Path Fill="#AAFFFFFF">
        <Path.Data>
            <CombinedGeometry GeometryCombineMode="Xor">
                <CombinedGeometry.Geometry1>
                    <RectangleGeometry Rect="0,0,10000,10000"/>
                </CombinedGeometry.Geometry1>
                <CombinedGeometry.Geometry2>
                    <RectangleGeometry x:Name="transparentRect" Rect="150,100,200,100"/>
                </CombinedGeometry.Geometry2>
            </CombinedGeometry>
        </Path.Data>
    </Path>
</Grid>
Run Code Online (Sandbox Code Playgroud)

您现在可以根据需要以编程方式调整成员的Rect属性transparentRect.