Custom Bitmap Effects - Getting started
Custom Bitmap Effects - Getting started
Tuesday, 01 June 2010
Article Index
Custom Bitmap Effects - Getting started
Compiling a shader
The custom effect


The custom effect

Now that we have the HLSL pixel shader compiled correctly and stored as in the Shader directory we can write the C# to make use of it.

First we need to add:

using System.Windows.Media.Effects;
using System.Windows.Media.Media3D;
using System.IO;

at the start of the project.

To create a custom effect you have to create a derived class from ShaderEffect:

public class BlankEffect : ShaderEffect

The very minimum that this has to do is to create an instance of PixelShader that wraps your shader code and store a reference to this in the new classes PixelShader property. This is most easily achieved in the constructor:

 public BlankEffect()
PixelShader pixelShader =
new PixelShader();
Uri uri = new Uri(
Directory.GetCurrentDirectory() +
pixelShader.UriSource = uri;
this.PixelShader = pixelShader;

This first creates an instance of PixelShader, sets its UriSource property to the location of the file and then sets the BlankEffect PixelShader property to reference the new instance.

That's all you have to do to create a custom effect.

Now we can try it out. Add a button to the form and change its Click event handler to read:

private void button1_Click(
object sender, RoutedEventArgs e)
button1.Effect = new BlankEffect();

If you now run the program and click on the button




the result should be a red rectangle where the button should be displayed.




As admitted at the start of this article this isn't exactly impressive but, if you think about the complexity of the steps we needed to take to get to this stage, the fact that it all works is very impressive!

Remember if you want to make any changes to the shader you have to edit the .fx file, save it, compile it to generate the .px file and then run the project again.

Now with the mechanics of editing, compiling and using an HLSL program we can move on and learn more about how pixel shaders work and more about writing HLSL.

We will have  to modify the new C# class to take account and make use of the features we introduce, but the basic ideas and procedures remain unchanged.

How to write a more complex shader and generally make progress with HLSL is described in:  Custom Bitmap Effects - HLSL

If you would like to know when the next article on WPF HLSL is published register with iProgrammer or follow us on Twitter.


Loading Bitmaps: DoEvents and the closure pattern

Sometimes loading a bitmap causes an asynchronous download and you have to wait for it to complete before using it - but how best to wait? The standard solution is to use an event but this breaks what [ ... ]

BitmapSource: WPF Bitmaps

Although bitmap graphics are not a main focus of WPF it does offer some useful facilities

Custom Bitmap Effects - HLSL

In the article Custom Bitmap Effects - Getting started we discovered how to work with HLSL in WPF. Now we are in a position to write more sophisticated shaders and  this means learning some more  [ ... ]

Getting Started with WPF


WPF Windows Presentation Foundation is the new way to do forms and all things graphical in .NET. You can still use  Windows Forms but don't expect anything particularly new to be added to [ ... ]

BitmapImage and local files

When and how to use the BitmapImage class

Other Articles





Last Updated ( Monday, 14 June 2010 )

RSS feed of all content
I Programmer - full contents
Copyright © 2015 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.