SIGN UP MEMBER LOGIN:    
ARTICLE

Using Pixel Shader Effects in Silverlight 3

Posted by Mamta M Articles | Silverlight with C# July 20, 2009
In this article you will learn how to use pixel shader effects in Silverlight 3.
Reader Level:

One of the new exciting features in Silverlight 3 is pixel shader effects. These effects can be applied to any user interface element on the Silverlight UI.

Introduction:

Pixel shader effects allow you to add effects, such as grayscale, red eye removal, pixel brightness, and shadows, to rendered objects. You can use the pixel shader effects built in with the Silverlight runtime or if you wish, you can create your own.

There are two pixel shader effects that are available in Silverlight 3 namely, DropShadowEffect and BlurEffect. You can apply only one effect directly to an element at a time. For example, you cannot apply both BlurEffect and a DropShadowEffect to the same element directly.

Here we shall examine the DropShadow Effect using a simple example. 


The DropShadowEffect has several important properties that determine characteristics of the drop shadow:

Property Name Description
Color Specifies the color of the drop shadow. The default is black.
ShadowDepth Specifies how much the shadow will be displaced from the object that is casting the shadow. The default is 5.
Direction Specifies in what direction the shadow is cast from the object. It is an angle between 0 and 360 with 0 starting on the right hand side and moving counter-clockwise around the object. The default is 315.
BlurRadius Specifies how blurred the shadow is. Typical range is between 0 and 1. The default is 5.
Opacity Specifies how transparent the shadow is. Typical range is between 0 and 1, where 1 means fully opaque and 0 means fully transparent (not visible). The default is 1.

Table 1: Properties of the DropShadowEffect

To start with, let us create a new Silverlight 3 application using Visual Web Developer 2008. If you have worked with Silverlight 2, you will find that the new project window is almost the same with just some minor changes (the template Silverlight Navigation Application is new):

PixelShader1.gif

Figure 1: New Project window

The default XAML page is now called MainPage instead of Page as it used to be called earlier in Silverlight 2.

The default code in this XAML file will be seen as follows:

<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
  </Grid>
</
UserControl>

Observe the new namespaces included in the code. For more information on the same, refer the MSDN library.

Add a StackPanel control from the Toolbox into the XAML code between the <Grid></Grid> tags:

<UserControl x:Class="SilverlightApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">}
        <StackPanel>
       </StackPanel>
    </Grid>
</
UserControl>

The StackPanel is a layout control that allows you to organize child controls in a horizontal or vertical fashion.

Next, add a button control between the StackPanel tags and configure it as shown below:

<UserControl x:Class="SilverlightApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
        <StackPanel>
            <Button Content="New Pixel Effect Demo"
                    Width="200" Margin="5" Background="Orchid">
            </Button>
        </StackPanel>
    </Grid>
</
UserControl>

Finally, add the new pixel effects as follows: (this code is to be added between the <Button></Button> tags.

                <Button.Effect>
               <DropShadowEffect Color="DarkMagenta" Direction="320" ShadowDepth="25" BlurRadius="5" Opacity="0.5" />
               </Button.Effect>

The final MainPage.xaml will look as follows:

<UserControl x:Class="SilverlightApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
        <StackPanel>
            <Button Content="New Pixel Effect Demo"
                    Width="200" Margin="5" Background="Orchid">
                <Button.Effect>
               <DropShadowEffect Color="DarkMagenta" Direction="320" ShadowDepth="25"
                 BlurRadius="5" Opacity="0.5" />
               </Button.Effect>
            </Button>
        </StackPanel>
    </Grid>
</
UserControl>

When you finally, run the application, you will see the output as follows:

PixelShader2.gif

Figure 2: Output

Conclusion: This article explored how to use pixel shader effects in Silverlight 3.

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

hi shashank,

I have already covered this in one of my articles. Please refer to

http://www.c-sharpcorner.com/UploadFile/mamta_m/PixelShaderinSilverlight07272009003254AM/PixelShaderinSilverlight.aspx
 
The article shows how to apply drop shadow effect programmatically at runtime. You can follow the same principle for pixel shader effect.

-Mamta

Posted by Mamta M Feb 17, 2010

how can the effect be applied at runtime?

Posted by shashank Feb 11, 2010

Thanks Diptimaya. Yes, I know it's available in Blend 3. This article is intended for those people who may not work with Blend but only Silverlight.

-Mamta

Posted by Mamta M Jul 23, 2009

Nice Article, Directly you can add Drop Shadow Effect in every control in Blend 3. :)

Posted by Diptimaya Patra Jul 21, 2009
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor