Export (0) Print
Expand All

DrawingGroup.OpacityMask Property

Gets or sets the brush used to alter the opacity of select regions of this DrawingGroup.

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)

public Brush OpacityMask { get; set; }

Property Value

Type: System.Windows.Media.Brush
A Brush that describes the opacity of this DrawingGroup; null indicates that no opacity mask exists and the opacity is uniform. The default is null.

Identifier field


Metadata properties set to true


The Brush is mapped to the DrawingGroup. The opacity value of each pixel of the mapped Brush is used to determine the resulting opacity of each corresponding pixel of the DrawingGroup. Only the opacity value of each color in the brush is used for this processing; all other color information is ignored.

The opacity that this property specifies is multiplied with the Opacity value of the DrawingGroup object. For more information about opacity masks and how they work, see Opacity Masks Overview.

DrawingGroup operations are applied in the following order:

  1. OpacityMask

  2. Opacity

  3. BitmapEffect

  4. ClipGeometry

  5. GuidelineSet

  6. Transform

This example shows how to apply an opacity mask to a Drawing. The DrawingGroup class is the only type of Drawing object that supports opacity masks.

To apply an opacity mask to a Drawing object, add it to a DrawingGroup and set the OpacityMask property of the DrawingGroup object.

The following illustration shows three views of the DrawingGroup: the drawing without an opacity mask, the opacity mask alone, and the DrawingGroup after the opacity mask is applied.

A DrawingGroup with an opacity mask

The following example uses a RadialGradientBrush as an opacity mask for a DrawingGroup.


Although this example uses a RadialGradientBrush as an opacity mask, LinearGradientBrush, DrawingBrush, ImageBrush, and VisualBrush objects can also make good opacity masks. For more information about opacity masks and how they work, see the Opacity Masks Overview.

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

namespace SDKSample
    /// <summary>
    /// Shows how to create and apply an OpacityMask to
    /// a DrawinGroup.
    /// </summary>
    public class OpacityMaskExample : Page
        public OpacityMaskExample()

            // 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));
            PathGeometry pGeometry = new PathGeometry();

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

            // Create another GeometryDrawing.
            GeometryDrawing drawing2 = new GeometryDrawing(
                    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();

            // Define an opacity mask and apply it to the
            // drawing group.
            RadialGradientBrush opacityMask = new RadialGradientBrush();
            opacityMask.GradientStops.Add(new GradientStop(Color.FromArgb(255, 0, 0, 0), 0.0));
            opacityMask.GradientStops.Add(new GradientStop(Color.FromArgb(0, 0, 0, 0), 0.55));
            opacityMask.GradientStops.Add(new GradientStop(Color.FromArgb(255, 0, 0, 0), 0.65));
            opacityMask.GradientStops.Add(new GradientStop(Color.FromArgb(0, 0, 0, 0), 0.75));
            opacityMask.GradientStops.Add(new GradientStop(Color.FromArgb(255, 0, 0, 0), 0.80));
            opacityMask.GradientStops.Add(new GradientStop(Color.FromArgb(0, 0, 0, 0), 0.90));
            opacityMask.GradientStops.Add(new GradientStop(Color.FromArgb(255, 0, 0, 0), 1.0));

            aDrawingGroup.OpacityMask = opacityMask;

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

            // Freeze the DrawingImage for performance benefits.

            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;


  Margin="20" Background="White">
  <Border BorderBrush="Gray" BorderThickness="1" 
    HorizontalAlignment="Left" VerticalAlignment="Top" Margin="20">
    <Image Stretch="None" HorizontalAlignment="Left">
        <DrawingImage PresentationOptions:Freeze="True">

            <!-- A DrawingGroup with a RadialGradientBrush as its opacity mask. -->
              <GeometryDrawing Brush="Lime" Geometry="M 25,25 L 0,50 25,75 50,50 25,25 25,0">
                  <Pen Thickness="10" Brush="Black" />
              <GeometryDrawing Brush="Lime">
                  <EllipseGeometry Center="10,10" RadiusX="5" RadiusY="5" />
                  <Pen Thickness="2" Brush="Black" />

                <!-- The opacity mask. -->
                  <GradientStop Offset="0.0" Color="#FF000000" />
                  <GradientStop Offset="0.55" Color="#00000000" />
                  <GradientStop Offset="0.65" Color="#FF000000" />
                  <GradientStop Offset="0.75" Color="#00000000" />
                  <GradientStop Offset="0.80" Color="#FF000000" />
                  <GradientStop Offset="0.90" Color="#00000000" />
                  <GradientStop Offset="1.0" Color="#FF000000" />


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2015 Microsoft