ShaderEffect Class

Provides a custom bitmap effect by using a PixelShader.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.Media.Effects.Effect
      System.Windows.Media.Effects.ShaderEffect

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

Syntax

'Declaration
Public MustInherit Class ShaderEffect _
    Inherits Effect
public abstract class ShaderEffect : Effect

The ShaderEffect type exposes the following members.

Constructors

  Name Description
Protected method ShaderEffect Initializes a new instance of the ShaderEffect class.

Top

Properties

  Name Description
Protected property DdxUvDdyUvRegisterIndex Gets or sets a value that indicates the shader register to use for the partial derivatives of the texture coordinates with respect to screen space.
Public property Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Protected property EffectMapping When overridden in a derived class, transforms mouse input and coordinate systems through the effect. (Inherited from Effect.)
Protected property PaddingBottom Gets or sets the amount by which the effect's output texture is larger than its input texture along the bottom edge of the effect.
Protected property PaddingLeft Gets or sets the amount by which the effect's output texture is larger than its input texture along the left edge.
Protected property PaddingRight Gets or sets the amount by which the effect's output texture is larger than its input texture along the right edge.
Protected property PaddingTop Gets or sets the amount by which the effect's output texture is larger than its input texture along the top edge.
Protected property PixelShader Gets or sets the PixelShader to use for the effect.

Top

Methods

  Name Description
Public method CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public method ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetAnimationBaseValue Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodStatic member PixelShaderConstantCallback Associates a dependency property value with a pixel shader's float constant register.
Protected methodStatic member PixelShaderSamplerCallback(Int32) Associates a dependency property value with a pixel shader's sampler register.
Protected methodStatic member PixelShaderSamplerCallback(Int32, SamplingMode) Associates a dependency property value with a pixel shader's sampler register and a SamplingMode.
Public method ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Protected methodStatic member RegisterPixelShaderSamplerProperty(String, Type, Int32) Associates a dependency property with a shader sampler register.
Protected methodStatic member RegisterPixelShaderSamplerProperty(String, Type, Int32, SamplingMode) Associates a dependency property with a shader sampler register and a sampling mode.
Public method SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Protected method UpdateShaderValue Notifies the effect that the shader constant or sampler corresponding to the specified dependency property should be updated.

Top

Fields

  Name Description
Protected fieldStatic member PixelShaderProperty Identifies the PixelShader dependency property.

Top

Remarks

Shader effects allow you to add effects to rendered objects such as grayscale, redeye removal, pixel brightness effects, shadows, etc by enabling you to use an algorithm to alter how pixels are displayed.

NoteNote:

Silverlight shader effects are rendered in software mode. Any object that applies an effect will also be rendered in software. Performance is degraded the most when using effects on large visuals or animating properties of an effect. This is not to say that you should not use shader effects in this way at all, but you should use caution and test thoroughly to ensure that your users are getting the experience you expect.

Examples

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"
    xmlns:shader="clr-namespace:PixelShaderSample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    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">
            <Image.Effect>
                <shader:MyPixelInvertedEffect />
            </Image.Effect>
        </Image>
    </StackPanel>
</UserControl>

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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

Thread Safety

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