具有透明度的DrawingBrush

时间:2015-11-17 15:52:00

标签: c# wpf xaml

我正在尝试使用透明度创建DrawingBrush并且无法弄清楚如何设置此透明度:(这是XAML:

<Border Grid.Column="1" Grid.Row="1" BorderBrush="Yellow" Background="Green">
        <Rectangle>
            <Rectangle.Fill>
                <DrawingBrush>
                    <DrawingBrush.Drawing>
                        <DrawingGroup>
                            <GeometryDrawing Brush="Red">
                                <GeometryDrawing.Geometry>
                                    <CombinedGeometry GeometryCombineMode="Exclude">
                                        <CombinedGeometry.Geometry1>
                                            <RectangleGeometry Rect="0, 0, 110, 110" />
                                        </CombinedGeometry.Geometry1>
                                        <CombinedGeometry.Geometry2>
                                            <RectangleGeometry Rect="10, 10, 90, 90" />
                                        </CombinedGeometry.Geometry2>
                                    </CombinedGeometry>
                                </GeometryDrawing.Geometry>
                                <GeometryDrawing.Pen>
                                    <Pen Brush="Blue" Thickness=".3" />
                                </GeometryDrawing.Pen>
                            </GeometryDrawing>
                        </DrawingGroup>
                    </DrawingBrush.Drawing>
                </DrawingBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Border>

输出结果为:

output

  1. <GeometryDrawing Brush="Red">
  2. <Border Background="Green">
  3. 如何将画笔的背景颜色设置为Transparent

    如果我在XAML(GeometryDrawing Brush="Transparent")中设置它,那么刷子将从上部控件Border获得背景颜色,一切都将是绿色

    主要想法是在控件/画笔的中间中绘制Rectangle

1 个答案:

答案 0 :(得分:0)

来自你的评论:

  

DrawingBrush仅在内部矩形中绘制它并设置所有“红色”&#39;区域到透明

这不是WPF中渲染模型的工作原理。当您在XAML中声明要绘制的内容时,您将描述要呈现的对象集合。一个透明的颜色(即alpha值为0,就像内置的Transparent值一样)不会设置&#34;它下面的像素是透明的;它根本不会呈现本身。在对象中使用透明颜色只会显示该对象下的任何内容。

如果你想在边框内绘制一个绿色框,它本身就是透明像素,那么你根本不需要填充边框(因为它会通过透明像素显示),你需要定义一个Drawing准确地绘制了您想要的内容。即一个透明的矩形,用于定义所需矩形的实际范围,然后是一个较小的填充矩形。

例如:

<Window x:Class="TestSO33761293InsetRectangleBorder.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="525" Width="525">

  <Window.Resources>
    <DrawingBrush x:Key="drawingBrush1">
      <DrawingBrush.Drawing>
        <DrawingGroup>
          <GeometryDrawing Brush="Green">
            <GeometryDrawing.Geometry>
              <RectangleGeometry Rect="10, 10, 90, 90" />
            </GeometryDrawing.Geometry>
            <GeometryDrawing.Pen>
              <Pen Brush="Blue" Thickness=".3" />
            </GeometryDrawing.Pen>
          </GeometryDrawing>
          <GeometryDrawing Brush="Transparent">
            <GeometryDrawing.Geometry>
              <RectangleGeometry Rect="0, 0, 110, 110" />
            </GeometryDrawing.Geometry>
          </GeometryDrawing>
        </DrawingGroup>
      </DrawingBrush.Drawing>
    </DrawingBrush>
  </Window.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*"/>
      <RowDefinition Height="2*"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="2*"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Border Grid.Column="1" Grid.Row="1" BorderBrush="Yellow" BorderThickness="1">
      <Rectangle Fill="{StaticResource drawingBrush1}"/>
    </Border>
  </Grid>
</Window>