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

Banner

The custom effect

Now that we have the HLSL pixel shader compiled correctly and stored as shader1.ps 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() +
@"\..\..\Shader\shader1.ps",
UriKind.Absolute);
pixelShader.UriSource = uri;
this.PixelShader = pixelShader;
}
}

This first creates an instance of PixelShader, sets its UriSource property to the location of the shader1.ps 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

 

before

 

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

 

after

 

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.

Banner


WPF .NET Core - Inside Dependency Properties

One of the great mysteries of WPF is the strangely named "Dependency Properties". In this chapter we learn how dependency properties really work by creating a custom dependency property



Drawing Bitmaps – DrawingImage and DrawingVisual

 

WPF provides multiple ways to convert vector drawings to bitmaps. Find out how DrawingImage and DrawingVisual work and when to use which. On the way we look at how to create 2D vector drawings.



WPF .NET Core - Routed Events

Routed events are a key feature of WPF. We look at bubbling and tunnelling and how to create your own routed event and how WPF changes the underlying mechanics of Windows.



WPF .NET Core - Creating Objects With XAML

If you've never encountered WPF (Windows Presentation Foundation) you are missing a versatile tool. This article is part of a series devoted to it. XAML can be confusing - especially if you think it i [ ... ]



RenderTargetBitmap - Visual vector to bitmap

RenderTargetBitmap will convert any Visual to a bitmap but sometimes it isn't quite as straighforward as just calling Render().


Other Articles

<ASIN:143022455X>

<ASIN:0596800959>

<ASIN:143022519X>

<ASIN:1430272910>




Last Updated ( Monday, 14 June 2010 )