PixelShader Class

Provides a managed wrapper around a High Level Shading Language (HLSL) pixel shader.


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

Public NotInheritable Class PixelShader _
	Inherits DependencyObject

The PixelShader type exposes the following members.

Public methodPixelShaderInitializes a new instance of the PixelShader class.

Public propertyDispatcherGets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertyUriSourceGets or sets a URI reference to HLSL bytecode in the assembly.

Public methodCheckAccessDetermines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodClearValueClears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetAnimationBaseValueReturns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetValueReturns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodReadLocalValueReturns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSetValueSets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public fieldStatic memberUriSourcePropertyIdentifies the UriSource dependency property.

Use the PixelShader class to access precompiled HLSL bytecode in a Silverlight application.

Set the UriSource property to load the shader bytecode.

To create a custom effect, assign the PixelShader to the PixelShader property of a ShaderEffect.

The following example shows an image with and without a shader effect applied to it.

Run this sample

This "invert colors" effect is a custom shader effect. To create your own effect, do the following:

  1. Create a shader using High Level Shader Language (HLSL), part of the DirectX SDK, and compile into a .ps file. To compile .ps files, you can directly use the fxc command line tool provided with the DirectX SDK or another tool like Shazzam which gives you a convenient UI for working with the fxc command line tool.

  2. Derive your custom effect from the ShaderEffect class and set the PixelShader property of your custom shader effect to your .ps file.

using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System;

namespace PixelShaderSample

    public class MyPixelInvertedEffect : ShaderEffect

        public MyPixelInvertedEffect()

            // Include your project name and .ps file.
            Uri u = new Uri(@"/PixelShaderSample;component/InvertColors.ps", UriKind.Relative);

            // Set the PixelShader of the custom shader effect to use your custom .ps file.
            PixelShader myPixelShader = new PixelShader();
            myPixelShader.UriSource = u;
            this.PixelShader = myPixelShader;


3. Apply the custom shader effect to your object (in this case an Image).

    <UserControl x:Class="PixelShaderSample.MainPage"
    Width="400" Height="300">
    <StackPanel Orientation="Horizontal">

        <!-- Image without PixelShader effect on the left and image with
             custom shader on the right. -->
        <Image Height="200" Width="200" Source="tree_blossoms.jpg" Stretch="Fill"/>
        <Image Height="200" Width="200" Source="tree_blossoms.jpg" Stretch="Fill">
                <shader:MyPixelInvertedEffect />


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions