Share via


PixelShader Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.Media.Effects.PixelShader

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

Syntax

'Declaration
Public NotInheritable Class PixelShader _
    Inherits DependencyObject
public sealed class PixelShader : DependencyObject

The PixelShader type exposes the following members.

Constructors

  Name Description
Public method PixelShader Initializes a new instance of the PixelShader class.

Top

Properties

  Name Description
Public property Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public property UriSource Gets or sets a URI reference to HLSL bytecode in the assembly.

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.)
Public method ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
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.)

Top

Fields

  Name Description
Public fieldStatic member UriSourceProperty Identifies the UriSource dependency property.

Top

Remarks

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.

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="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="https://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.