绘制WPF多边形,笔触为纯色或透明

问题描述 投票:0回答:1

我需要在WPF中创建一个通用三角形,指示笔划和填充区域的坐标,笔划和颜色。问题是,我想要指示颜色也是中性透明,这意味着比使用相同的坐标,我想要绘制,所有在这个形状内,一个透明的笔触,取代他的厚度填充颜色,但似乎不可能(我发现并解决了圆形和矩形形状相同的问题,但使用Polygon它不起作用:Drawing a WPF shape, as a Rectangle: StrokeThickness halve if Stroke is Transparent)。我现在使用的代码包括一个剪辑,因为笔划在内部和部分外部的坐标轮廓中生长部分,我需要移除外部部分,以及一些转换器以正确的方式给出一系列点:

<UserControl.Clip>
    <PathGeometry>
        <PathFigure StartPoint="{Binding Triangle.Points, Converter={StaticResource PointLocationClipConverter}}" IsClosed="True">
            <PolyLineSegment Points="{Binding Triangle.Points, Converter={StaticResource PointCollectionClipConverter}}" />
        </PathFigure>
    </PathGeometry>
</UserControl.Clip>

<Grid x:Name="_grid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Polygon Points="{Binding Triangle.Points, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource PointCollectionConverter}}" Grid.Row="0" Grid.Column="0" Margin="0,0,0,0"
             Width="{Binding ActualWidth, ElementName=_grid}"
             Height="{Binding ActualHeight, ElementName=_grid}"
             Stroke="{Binding Triangle.BorderColorBrush}"
             StrokeThickness="{Binding Triangle.Thick, Converter={StaticResource PoligonThickConverter}}" 
             Fill="{Binding Triangle.BackgroundBrush}" />

</Grid>

我现在拥有的是:

current result

我想要的是:

wanted result

注意:对于两个示例已发布,第一个图像具有用于笔划和填充区域的纯色,第二个图像具有笔触上的透明和用于填充区域的纯色(这是问题),第三个图像具有用于笔划的纯色和用于填充的透明区域。所有图像都具有相同的坐标和StrokeThickness值。

感谢帮助!

c# wpf polygon draw stroke
1个回答
0
投票

从@Clemens指示开始,我通过一系列转换精心设计了一个解决方案,没有任何几何微积分。解决方案在这里越来越少,我将尝试应用于我的上下文,但最终是:

    <Path MouseLeftButtonUp="PathPolyline_MouseLeftButtonUp"
          StrokeThickness="0" Stroke="Transparent" Fill="Gold" ClipToBounds="False">
        <Path.Data>
            <PathGeometry FillRule="EvenOdd" >
                <PathFigure StartPoint="0,0" IsClosed="True">
                    <PolyLineSegment Points="50,50 0,80"/>
                </PathFigure>
            </PathGeometry>
        </Path.Data>
    </Path>

并在C#代码中详细说明将执行以下操作:

private void PathPolyline_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
  Brush brush = Brushes.Black;
  Pen pen = new Pen(brush, 20.0); //thickness double than wanted measure: 20 to reduce 10
  //
  Path path = sender as Path;
  PathGeometry pg = path.Data as PathGeometry;
  PathGeometry pg2 = pg.GetWidenedPathGeometry(pen);
  //
  Transform tr = Transform.Identity;
  PathGeometry pg324 = Geometry.Combine(pg, pg2, GeometryCombineMode.Intersect, tr);
  pg.AddGeometry(pg324);
}
© www.soinside.com 2019 - 2024. All rights reserved.