DrawingGroup.Opacity 属性

定义

获取或设置此 DrawingGroup 的不透明度。

public:
 property double Opacity { double get(); void set(double value); };
public double Opacity { get; set; }
member this.Opacity : double with get, set
Public Property Opacity As Double

属性值

Double

介于 0 和 1 之间(包括 0 和 1)的值,用于描述此 DrawingGroup 的不透明度。 默认值为 1

示例

此示例演示如何修改不 Drawing透明度 。 该DrawingGroup类是唯一Opacity具有属性的对象类型Drawing

若要更改对象的不透明度 Drawing ,请将其添加到对象 DrawingGroup 的属性并设置 OpacityDrawingGroup 对象的属性。

对象的Opacity设置DrawingGroup乘以其子绘图的不透明度;例如,如果一个DrawingGroupOpacity具有 0.5 且包含GeometryDrawing不透明 50% 的画笔,则画笔为 25% 不透明Brush (0.5 * 0.5) 。

若要更改绘图的选定部分的不透明度,请使用一个 OpacityMask

以下示例使用一个 DrawingGroup 组合多个 GeometryDrawing 对象。 该示例还将对象的不透明度 DrawingGroup 设置为 0.25,以便绘图不透明 25%。

此图显示其 DrawingGroup 前后 Opacity 设置为 0.25。

具有不同不透明度设置的 DrawingGroup

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SDKSample
{
    public class OpacityExample : Page
    {
        public OpacityExample()
        {

            //
            // Create a GeometryDrawing.
            //

            // Define the drawing's contents.
            PathFigure pLineFigure = new PathFigure();
            pLineFigure.StartPoint = new Point(25, 25);
            PolyLineSegment pLineSegment = new PolyLineSegment();
            pLineSegment.Points.Add(new Point(0, 50));
            pLineSegment.Points.Add(new Point(25, 75));
            pLineSegment.Points.Add(new Point(50, 50));
            pLineSegment.Points.Add(new Point(25, 25));
            pLineSegment.Points.Add(new Point(25, 0));
            pLineFigure.Segments.Add(pLineSegment);
            PathGeometry pGeometry = new PathGeometry();
            pGeometry.Figures.Add(pLineFigure);

            GeometryDrawing drawing1 = new GeometryDrawing(
                    Brushes.Lime,
                    new Pen(Brushes.Black, 10),
                    pGeometry
                );

            //
            // Create another GeometryDrawing.
            //
            GeometryDrawing drawing2 = new GeometryDrawing(
                    Brushes.Lime,
                    new Pen(Brushes.Black, 2),
                    new EllipseGeometry(new Point(10, 10), 5, 5)
                );

            // Create the DrawingGroup and add the
            // geometry drawings.
            DrawingGroup aDrawingGroup = new DrawingGroup();
            aDrawingGroup.Children.Add(drawing1);
            aDrawingGroup.Children.Add(drawing2);

            //
            // Set the opacity of the DrawingGroup to 0.25.
            //
            aDrawingGroup.Opacity = 0.25;

            // Use an Image control and a DrawingImage to
            // display the drawing.
            DrawingImage aDrawingImage = new DrawingImage(aDrawingGroup);

            // Freeze the DrawingImage for performance benefits.
            aDrawingImage.Freeze();

            Image anImage = new Image();
            anImage.Source = aDrawingImage;
            anImage.Stretch = Stretch.None;
            anImage.HorizontalAlignment = HorizontalAlignment.Left;

            // Create a border around the images and add it to the
            // page.
            Border imageBorder = new Border();
            imageBorder.BorderBrush = Brushes.Gray;
            imageBorder.BorderThickness = new Thickness(1);
            imageBorder.VerticalAlignment = VerticalAlignment.Top;
            imageBorder.HorizontalAlignment = HorizontalAlignment.Left;
            imageBorder.Margin = new Thickness(20);
            imageBorder.Child = anImage;

            this.Background = Brushes.White;
            this.Margin = new Thickness(20);
            this.Content = imageBorder;
        }
    }
}
<Page 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="PresentationOptions"
  Background="White" Margin="20">
  <Border BorderBrush="Gray" BorderThickness="1" 
    HorizontalAlignment="Left" VerticalAlignment="Top"
    Margin="20">
    <Image Stretch="None">
      <Image.Source>
        <DrawingImage PresentationOptions:Freeze="True">
          <DrawingImage.Drawing>

            <!-- The drawing group, with an Opacity of 0.25. -->
            <DrawingGroup Opacity="0.25">
              <GeometryDrawing Brush="Lime" Geometry="M 25,25 L 0,50 25,75 50,50 25,25 25,0">
                <GeometryDrawing.Pen>
                  <Pen Thickness="10" Brush="Black" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
              <GeometryDrawing Brush="Lime">
                <GeometryDrawing.Geometry>
                  <EllipseGeometry Center="10,10" RadiusX="5" RadiusY="5" />
                </GeometryDrawing.Geometry>
                <GeometryDrawing.Pen>
                  <Pen Thickness="2" Brush="Black" />
                </GeometryDrawing.Pen>
              </GeometryDrawing>
            </DrawingGroup>
          </DrawingImage.Drawing>
        </DrawingImage>
      </Image.Source>
    </Image>
  </Border>


</Page>

注解

一个值 1DrawingGroup 指定完全不透明;一个值 0 指定它是完全透明的。 小于 0 的值被视为 0,大于 1 的值被视为 1。

控制其 GeometryDrawing 不透明度的另一种方法是指定 Opacity 它的 Brush不透明度。

DrawingGroup 操作按以下顺序应用:

  1. OpacityMask

  2. Opacity

  3. BitmapEffect

  4. ClipGeometry

  5. GuidelineSet

  6. Transform

依赖项属性信息

标识符字段 OpacityProperty
元数据属性设置为 true

适用于

另请参阅