WPF GeometryDrawing的画笔始终仅是透明的

问题描述

这是一个测试应用,仅在窗口中显示阴影线椭圆。不管我怎么写,椭圆的背景都是透明的。不是我设置GeometryDrawing的颜色。我很沮丧生成图片将椭圆的背景显示为绿色时应显示为透明。

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:WpfApp2"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Background="Red"
        mc:Ignorable="d">
   <Window.Resources>
      <ResourceDictionary>
         <DrawingBrush x:Key="HatchBrush"
                       Stretch="UniformToFill"
                       TileMode="Tile"
                       ViewBox="0 0 10 10"
                       ViewBoxUnits="Absolute"
                       Viewport="0 0 10 10"
                       ViewportUnits="Absolute">
            <DrawingBrush.Drawing>
               <GeometryDrawing Brush="Green">
                  <GeometryDrawing.Geometry>
                     <GeometryGroup>
                        <LineGeometry StartPoint="0 0"
                                      EndPoint="10 0" />
                        <LineGeometry StartPoint="0 0"
                                      EndPoint="0 10" />
                     </GeometryGroup>
                  </GeometryDrawing.Geometry>
                  <GeometryDrawing.Pen>
                     <Pen Brush="Yellow"
                          EndLineCap="Square"
                          StartLineCap="Square"
                          Thickness="3" />
                  </GeometryDrawing.Pen>
               </GeometryDrawing>
            </DrawingBrush.Drawing>
         </DrawingBrush>
       </ResourceDictionary>
   </Window.Resources>
   <Grid Margin="5"
         HorizontalAlignment="Stretch"
         VerticalAlignment="Stretch">
      <Ellipse x:Name="c_Ellipse"
               HorizontalAlignment="Stretch"
               VerticalAlignment="Stretch"
               Fill="{StaticResource HatchBrush}"
               stroke="Black" />
   </Grid>
</Window>

这是我得到的:

Ellipse with a transparent background rather than the assigned Green

椭圆的背景应为绿色:

<GeometryDrawing Brush="Green">

解决方法

没有使用几何图形的Brush填充几何。

您可以使用RectangleGeometry而不是两个LineGeometries:

<GeometryDrawing Brush="Green">
    <GeometryDrawing.Geometry>
        <RectangleGeometry Rect="0,10,10"/>
    </GeometryDrawing.Geometry>
    <GeometryDrawing.Pen>
        <Pen Brush="Yellow" Thickness="1.5"/>
    </GeometryDrawing.Pen>
</GeometryDrawing>