SIGN UP MEMBER LOGIN:    
ARTICLE

Implementing Opacity Masks in Silverlight

Posted by Mahesh Chand Articles | Silverlight with C# November 05, 2008
Opacity masks allow us to make portions of an object transparent or semi transparent. This article demonstrates how to implement opacity masks feature in Silverlight objects such as a Rectangle and an Image.
Reader Level:
Download Files:
 

Opacity masks allow us to make portions of an object transparent or semi transparent.  The OpacityMask property of an element (defined in the UIElement class) is used to apply an opacity mask on an object such as a Rectangle or an Image.  

The following XAML code snippet uses an Image object to display an image.

<Image Source="Waterfall.jpg"

               Width="300" Height="300">

The output looks like Figure 1.


Figure 1

The XAML code in Listing 1 sets the Image.OpacityMask property to the Image object to a LinearGradientBrush with three stops.

<Image Source="Waterfall.jpg" Width="300"

       Height="300">              

    <Image.OpacityMask>

       <LinearGradientBrush >

            <GradientStop Color="Transparent" Offset="0"/>

            <GradientStop Color="Black" Offset="0.25"/>

            <GradientStop Color="Transparent" Offset="1"/>

        </LinearGradientBrush>

    </Image.OpacityMask>

</Image>

Listing 1.

 

The new output looks like Figure 2.

 

Figure 2. An opacity masked image

Since the OpacityMask property is defined in the UIElement, it can be applied to any element. For example, the following code generates a rectangle looks like Figure 3.

 

<Rectangle Width="300" Height="200"

           Stroke="Black" StrokeThickness="4"

           Fill="Yellow">

</Rectangle>

 

 

Figure 3. A rectangle

 

 

Now we can apply opacity mask on the rectangle and the new output looks like Figure 4 with the following code.

 

<Rectangle Width="300" Height="200"

   Stroke="Black" StrokeThickness="4"

   Fill="Yellow">

    <Rectangle.OpacityMask>

        <LinearGradientBrush >

            <GradientStop Color="Transparent" Offset="0"/>

            <GradientStop Color="Black" Offset="0.25"/>

            <GradientStop Color="Transparent" Offset="1"/>

        </LinearGradientBrush>

    </Rectangle.OpacityMask>

</Rectangle>

 

 

Figure 4. An opacity masked rectangle

 

Login to add your contents and source code to this article
share this article :
post comment
 




The following code ignores the: rect1.OpacityMask = irgb; to apply the opacity mask at runtime.



public MainPage()
{
InitializeComponent();
Rectangle rect1 = new Rectangle();
rect1.Width = 300;
rect1.Height = 300;
rect1.Fill = new SolidColorBrush(Colors.Red);
RadialGradientBrush irgb = new RadialGradientBrush();
irgb.GradientOrigin = new Point(0.5, 0.5); 
irgb.Center = new Point(0.5, 0.5);
irgb.RadiusX = 0.5;
irgb.RadiusY = 0.5;
GradientStop gs1 = new GradientStop();
GradientStop gs2 = new GradientStop();
GradientStop gs3 = new GradientStop();
GradientStop gs4 = new GradientStop();
// ...
// Set the color for each stop object
gs1.Color = Color.FromArgb(255, 255, 255, 255);
gs2.Color = Color.FromArgb(255, 255, 255, 255);
gs3.Color = Color.FromArgb(255, 150, 150, 150);
gs4.Color = Color.FromArgb(255, 0, 0, 0);
// Set the offset for each stop object
gs1.Offset = 0.0;
gs2.Offset = 0.25;
gs3.Offset = 0.75;
gs4.Offset = 1.0;
// Add the Gradient stops to the Linear Gradient Brush
irgb.GradientStops.Add(gs1);
irgb.GradientStops.Add(gs2);
irgb.GradientStops.Add(gs3);
irgb.GradientStops.Add(gs4);
rect1.OpacityMask = irgb;
LayoutRoot.Children.Add(rect1);
}

Posted by Claudio Soprano Feb 23, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor