Opacity Masks WPF in VB.NET

In this article you will learn about the opacity masks in the WPF.
  • 2273

Opacity Mask: OpacityMask property is used to make specific regions of an element transparent or partially transparent. It's name implies a mask which is given to the visibility of a visual element.So the places which are not covered by the mask will be transparent.
The OpacityMask property accepts any brush. The alpha channel of the brush determines where the transparency occurs. For example, if you use a SolidColorBrush that's set to a transparent color for your OpacityMask, your entire element will disappear. If you use a SolidColorBrush that's set to use a nontransparent color, your element will remain completely visible.
The main application of OpacityMask is to define some transparent areas in visual elements.Let's consider the example below.

Example of the Opacity Mask

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid x:Name="Opacity">
    <Rectangle Fill="Blue" />
    <Ellipse Stroke="Gainsboro">
        <Ellipse.OpacityMask>
            <LinearGradientBrush EndPoint="0.10,.70" StartPoint="0.600,0.500">
                <GradientStop Color="#FF000011" Offset="0.400"/>
                <GradientStop Color="#11FFFFFF" Offset="0.500"/>
            </LinearGradientBrush>
        </Ellipse.OpacityMask>
        <Ellipse.Fill>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#11111111" Offset="0.54"/>
                <GradientStop Color="#FFFF00" Offset="0.4"/>
            </LinearGradientBrush>
        </Ellipse.Fill>
    </Ellipse>
</
Grid>
</
Window>

In above example I have used a LinearGradientBrush with Brush transform to implement this.When we are specifying OpacityMask,it won't consider the color of the brush.

Output Window

opac1.gif
 

Conclusion

Hope this article helps you understand all about the opacity masks in the WPF.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.