【问题标题】:Brush only parts of an Ellipse in WPF在 WPF 中只画椭圆的一部分
【发布时间】:2014-05-17 09:28:48
【问题描述】:

我很难找到绘制以下形状的最佳方法。我正在使用下面的代码在可视层上绘制Ellipse

但是我怎么只能刷宿舍呢?我认为可以使用LinearGradientBrushRadialGradientBrush 来完成,但我不知道如何使用它。

var cntrpoint = space.FlipYAxis(x2, y2);
dc.DrawEllipse(Brushes.Transparent, pen, cntrpoint, 30, 30);

【问题讨论】:

    标签: c# wpf ellipse brush


    【解决方案1】:

    像这样:

    var geometry = new GeometryGroup();
    geometry.Children.Add(new RectangleGeometry(new Rect(1, 0, 1, 1)));
    geometry.Children.Add(new RectangleGeometry(new Rect(0, 1, 1, 1)));
    var drawing = new GeometryDrawing(Brushes.Black, null, geometry);
    var brush = new DrawingBrush(drawing);
    
    dc.DrawEllipse(brush, pen, cntrpoint, 30, 30);
    

    【讨论】:

    • 非常感谢克莱门斯。你能告诉我RectangleGeometry在这里做什么吗?我很难理解那部分。
    • 两个 RectangleGeometries(宽度和高度 1)填充 2x2 网格的左下角和右上角。因此(相对)坐标(1, 0)(0, 1)
    • 这个答案真的很优雅。谢谢。
    【解决方案2】:

    我在 XAML 中找到了解决方案
    (受此帖子启发:https://stackoverflow.com/a/5670388/3047078):

    <Image Width="200" Height="200" Margin="20">
      <Image.Source>
        <DrawingImage>
          <DrawingImage.Drawing>
            <DrawingGroup>
    
              <GeometryDrawing Brush="Black">
                <GeometryDrawing.Pen>
                  <Pen Brush="Black" />
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                  <PathGeometry>
                    <PathFigure StartPoint="100,100">
                      <PathFigure.Segments>
                        <LineSegment Point="100,0"/>
                        <ArcSegment Point="200,100"  SweepDirection="Clockwise" Size="100,100"/>
                        <LineSegment Point="100,100"/>
                      </PathFigure.Segments>
                    </PathFigure>
                  </PathGeometry>
                </GeometryDrawing.Geometry>
              </GeometryDrawing>
    
              <GeometryDrawing Brush="White">
                <GeometryDrawing.Pen>
                  <Pen Brush="Black"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                  <PathGeometry>
                    <PathFigure StartPoint="200,100">
                      <PathFigure.Segments>
                        <ArcSegment Point="100,200" SweepDirection="Clockwise" Size="100,100"/>
                        <LineSegment Point="100,100"/>
                      </PathFigure.Segments>
                    </PathFigure>
                  </PathGeometry>
                </GeometryDrawing.Geometry>
              </GeometryDrawing>
    
              <GeometryDrawing Brush="Black">
                <GeometryDrawing.Pen>
                  <Pen Brush="Black"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                  <PathGeometry>
                    <PathFigure StartPoint="100,100">
                      <PathFigure.Segments>
                        <LineSegment Point="100,200"/>
                        <ArcSegment Point="0,100" SweepDirection="Clockwise" Size="100,100"/>
                        <LineSegment Point="100,100"/>
                      </PathFigure.Segments>
                    </PathFigure>
                  </PathGeometry>
                </GeometryDrawing.Geometry>
              </GeometryDrawing>
    
              <GeometryDrawing Brush="White">
                <GeometryDrawing.Pen>
                  <Pen Brush="Black"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                  <PathGeometry>
                    <PathFigure StartPoint="100,100">
                      <PathFigure.Segments>
                        <LineSegment Point="0,100"/>
                        <ArcSegment Point="100,0" SweepDirection="Clockwise" Size="100,100"/>
                      </PathFigure.Segments>
                    </PathFigure>
                  </PathGeometry>
                </GeometryDrawing.Geometry>
              </GeometryDrawing>
    
            </DrawingGroup>
          </DrawingImage.Drawing>
        </DrawingImage>
      </Image.Source>
    </Image>
    

    【讨论】:

    • 感谢 Flat,它可以完成这项工作,但我想要的是一个实现 Brush 的解决方案。我不想结合不同的几何图形。
    【解决方案3】:

    您可以使用DrawingBrushGeometryDrawing 来做到这一点

    <Ellipse Width="300" Height="300" Stroke="DarkGray" StrokeThickness="1.5">
        <Ellipse.Fill>
            <DrawingBrush>
                <DrawingBrush.Drawing>
                    <GeometryDrawing>
                        <GeometryDrawing.Brush>
                            <RadialGradientBrush>
                                <GradientStop Color="Black" Offset="1.0" />
                                <GradientStop Color="White" Offset="1.0" />
                            </RadialGradientBrush>
                        </GeometryDrawing.Brush>
                        <GeometryDrawing.Geometry>
                            <GeometryGroup>
                                <RectangleGeometry Rect="0,0,50,50" />
                                <RectangleGeometry Rect="50,50,50,50" />
                            </GeometryGroup>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </Ellipse.Fill>
    </Ellipse>
    

    输出:

    【讨论】:

    • 谢谢,我不知道如何适应我的情况。因为我在后面的代码中画了椭圆。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多