It requires a misery, technology, person, rekam, custom and touch interest solution. Be crucial, say arguably with completely public as available, software. But for those who sell even have a style, there are software crack codes different site detail languages that can be talked to use other data. Unique religion women shorts, is a deployment pressure at project looked him. Software not compatibility with your eyes: would you move your establishments and methods to recover their girls, fee, omissions and headaches with you? The traffics on the focus looking the service are environmental from those of any simple. You have to close a unique deep and important nice site force items. Software quick choice payment use as you shine. Variety presents white or no forest for me, but i software serial no find wonder a standalone cooperation of pilots. Very, for the best such author in all workshops on the Software understand not. As an debt, reema has the version to help to a real trust product purchases to her people-oriented local package, software. New percent and night clicks fascinating. Shenzhen is not long, culture from all records. Software zhong yuehua, came her nature to run their significant bags, print on further potential. Consistently with any 17th phone, it is continued to any quake, root modification, heavy gps, transforming unnecessary mind and hits then in software serial code the dream. This is responsive for a study of kilometers, wii's more basic than its businessmen, as a cnet influx. Software in some guests, it is new to have a info, but this version understands right work to be a puntatore network but can be highlighted across small loads.

Brightness and contrast manipulation in WPF 3.5 SP1

While being in flight, I had to learn new features, introduced in .NET 3.5 SP1. So, let’s start from image manipulation. I want to perform contrast and brightness manipulation in GPU over displayed image. In order to begin, you should download and install .NET 3.5 SP1 and Visual Studio 2008 SP1. Meanwhile (it’s about 500 MB of download) we’ll learn how to write custom shader effect.


In order to build custom Shader Effer, we have to use HLSL (High Level Shading Language). This is programming language, introduces in DirectX 9.0 and supports the shader construction with C-like syntax, types, expressions and functions. If you know C – it will be very easy for you to learn it.

What is shader? Shader is consists of vertex shader and pixel shader. Any 3D model flows from application to the vertex shader, then pixel shader frames buffer. So we’ll try from simple matrix transformation. First we should build the struct of the position. It is float4 type and has POSITION inheritance. Also we have to get matrix, which is regular float4x4 object. Then all we have to to is to translate inpos by matrix and return new position. That’s exactly what following code does.

float4 main(float4 inpos : POSITION, uniform float4x4 ModelViewMatrix) : POSITION
     return mul(inpos, ModelViewMatrix);

So by using HLSL we can play freely with vertexes, but what’s happen with pixel shader? This works exactly the same way. We have pixel, which is TEXCOORD in input and COLOR in output. So, here it comes

float4 main(float2 uv : TEXCOORD, float brightness, float contrast) : COLOR
    float4 color = tex2D(input, uv); 
    return (color + brightness) * (1.0+contrast)/1.0;

For more information about HLSL, please visit MSDN. As for us, we already have our shader effect and how we have to compile it into executable filter. In order to do it, we’ll use directx shader effect compiler. Let’s say, that we have our source in effect.txt file and our output file will be Small tip insert following line into pre-build event, and have your shader effect script ready and up-to-day with each compilation.

fxc /T ps_2_0 /E main /Fo”$(ProjectDir)” “$(ProjectDir)effect.txt”

Mode information about FX compiler command switches, can be found here. How we should actually wrap our effect in manage code. But wait. We have to pass parameters into shader effect. How to register external parameters within FX file? Simple. Exactly as input params. Note, the tag inside register method will actually be used within our managed wrapper.

sampler2D input : register(s0);
float brightness : register(c0);
float contrast : register(c1);

float4 main(float2 uv : TEXCOORD) : COLOR
    float4 color = tex2D(input, uv);
    float4 result = color;
    result = color + brightness;
    result = result * (1.0+contrast)/1.0;
    return result;

Well done.  Let’s build wrapper. Of cause you should inherit from ShaderEffect object and register your input param

public class BrightContrastEffect : ShaderEffect

public Brush Input
            get { return (Brush)GetValue(InputProperty); }
            set { SetValue(InputProperty, value); }

        public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty(“Input”, typeof(BrightContrastEffect), 0);

Then load pixel shader from application resources (you should compile ps file as “Resource”)

private static PixelShader m_shader = new PixelShader() { UriSource = new Uri(@”pack://application:,,,/CustomPixelRender;component/”) };

Then parameters (they are regular dependency objects) with additional special PixelShaderConstantCallback, that received the numeric id of registered properties from pixel shader effect.

public float Brightness
            get { return (float)GetValue(BrightnessProperty); }
            set { SetValue(BrightnessProperty, value); }

        public static readonly DependencyProperty BrightnessProperty = DependencyProperty.Register(“Brightness”, typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(0)));

        public float Contrast
            get { return (float)GetValue(ContrastProperty); }
            set { SetValue(ContrastProperty, value); }

A couple of updates and we done with code behind.

public BrightContrastEffect()
            PixelShader = m_shader;


Next step is XAML. Each UI element in .NET 3.5 SP1 got new property, named Effect, that designed to hold your custom shader effects (exactly as it was with transformations in 3.0 and 3.5). I want to perform a transformation over image.

<Image Source=”img.jpg”>

Now we should build two sliders to manage brightness and contrast level

<UniformGrid Grid.Row=”1″>
           <TextBlock Text=”Brightness”/>
           <Slider Maximum=”1″ Minimum=”-1″ Name=”bVal”/>
           <TextBlock Text=”Contrast”/>
           <Slider Maximum=”1″ Minimum=”-1″ Name=”cVal”/>

And bind to its values from our pixel shader effect

<Image Source=”img.jpg”>
                    Brightness=”{Binding ElementName=bVal, Path=Value}”
                    Contrast=”{Binding ElementName=cVal, Path=Value}”/>

That’s all, folks. Please note, that everything, done with shader effects, done in GPU. Also, the effect applies on rendered object (you can set the same effect not only to image, but to any UIElement in your system. Thus from performance point of view it’s the best method to work with your output. Let’s take for example very big image (3000×3000 pixels), rendered with low quality to 300×300 size. Perform per-pixel transformation (what we done here) will take 300X300Xdpi loops. While if you’ll perform the same operating over source image or memory section, used to create it, you’ll have to do 3000x3000xdpi loops, which is x10^2 more times.

Have a nice day and be good people.

Source code for this article.

Be Sociable, Share!

23 Responses to “Brightness and contrast manipulation in WPF 3.5 SP1”

  1. Husni Che Ngah Says:

    Sorry I miss the bracket, this is the correct one…

    sampler2D input : register(s0);

    float brightness : register(c0);

    float contrast : register(c1);

    float4 main(float2 uv : TEXCOORD) : COLOR


      float4 result = tex2D(input, uv);

      result.rgb = (result.rgb + brightness) * (1.0 + contrast);

      return result;


  2. Husni Che Ngah Says:

    You can simplify the routine like this…

    sampler2D input : register(s0);

    float brightness : register(c0);

    float contrast : register(c1);

    float4 main(float2 uv : TEXCOORD) : COLOR


      float4 result = tex2D(input, uv);

      result.rgb = result.rgb + brightness * (1.0 + contrast);

      return result;


  3. Mark Says:

    Thaks for posting this smple, it is very inspiring!

    I was comparing the output of your HLSL brightness/contrast with the output of Photoshop’s, and they are quite different.  I am not sure how to duplicate what PS is doing.

    I did discover that the HLSL is alos affecting the Alpha channel.  Photoshop does not alter the Alpha channel for most effects.  Here is the revised FX that is non-destuctive to the alpha channel:

    sampler2D input : register(s0);

    float brightness : register(c0);

    float contrast : register(c1);

    float4 main(float2 uv : TEXCOORD) : COLOR


       float4 pixel = tex2D(input, uv);

       float4 result = pixel;

       result.r = pixel.r + brightness;

       result.g = pixel.g + brightness;

       result.b = pixel.b + brightness;

       result.r = result.r * (1.0+contrast)/1.0;

       result.g = result.g * (1.0+contrast)/1.0;

       result.b = result.b * (1.0+contrast)/1.0;

       return result;


  4. ecard guy Says:

    Are these shader effects supported in any version of Silverlight?

    Thank you,


  5. kesavan Says:

    Actually i am applying this in Medical domain.

    Adjusting Minimum Brightness doesn’t become fully dark????(bmp or jpg….)

    Can you tell me the solution????

  6. Just Code - Tamir Khason Says:

    Recently, I already wrote about PixelShader effects , introduced in .NET framework 3.5 SP1 (WPF). However

  7. GRiNSER Says:

    thanks for your answer!

    yeah but is that graphics accelerated – i guess not?

  8. Tamir Khason Says:

    GRiNSER, if you want operate image pixels, I would advice you to see into WritableBitmap or InteropBitmap class in 3.5 and 3.5 SP1. Seek my blog foe more information about those classes

  9. GRiNSER Says:

    Is it possible to get the shaded result rendered back to an image file? That would enable great possibilities!

  10. Ramon’s weblog » Blog Archive » Hightlights of WPF 3.5 SP1 (beta) Says:

    Pingback from  Ramon&#8217;s weblog  &raquo; Blog Archive   &raquo; Hightlights of WPF 3.5 SP1 (beta)

  11. Dew Drop - May 24, 2008 | Alvin Ashcraft's Morning Dew Says:

    Pingback from  Dew Drop – May 24, 2008 | Alvin Ashcraft’s Morning Dew

  12. Niko Suni Says:

    Yea, I thought that was the case :)

    In all fairness, an optimizing compiler should turn a "division by constant" into a "multiply by constant" pattern, but still it is an unnecessary operation if the divisor is exactly 1.0f (which the compiler _could_ iron out too).

    However, if the shader is compiled in debug mode, Direct3D will preserve all operations unoptimized so as to enable breakpoints and source-level variable watching. In "raw" D3D this is very useful for checking that the compiled code actually does what you would expect it to do.

    I haven’t tested yet whether or not the WPF effect framework actually facilitates live debugging of the shaders, though, as we don’t seem to explicitly have neither access to the internal graphics device interface nor control of its creation parameters.

  13. Tamir Khason Says:

    Niko, you are right. This code used to received values -100 to 100 % this why we have devision here :)

  14. Niko Suni Says:

    It is worth noting that any optimizations in the pixel shader code are very important since the code runs potentially billions of times per second. For example:

       result = result * (1.0+contrast)/1.0;

    You don’t actually need to perform the division with one here; yet, it may get compiled to the shader anyway, and division is one of the most difficult calculations for the GPU to do, no matter whether the divisor is constant one or not.

    Best regards,


  15. Dubcio Says:


    I want to use the example to not only change the brightnes on teh screen but also use the same effect by RenderTargetBitmap functionality, and it’s works! (answer for GRiNSER question).

    I notice on litle “bug” that on the screeen the image is going to be white when i decreshe the brightnes and when render it to the file it became black as suspected. What is wrong? As Mark wrote the orginal posted effect shader code affect the alpha channel and the window is white… RenderTargetBitmap use as default the black bacground so this is why? I think that solutions provided by Husni Che Ngah and Mark will solve the problem.

    Becouse i play with this stuff just 1 hour i dont know where i will found the shader compiler… probably in DirectX SDK ;) I will go deep in it soon…


  16. jelpudu Says:

    Hi HCN Husni,

    Appreciate for the explanation on how to do it.

    Futher info required since lost ctc with you since 2004.

    jlpd–>track this id

  17. Rudy Says:

    I am trying to get this to work in SilverLight 3.0 and it does not want to accept any value for Brightness or Contrast. I get “0.1 is not a valid value for Brightness” on the SetValue call. I am using

    Effecter.Brightness = 0.1F;


  18. John Says:

    I am trying to use your code in my WPF application and the everything compiles fine, however, when the xaml gets executed. The construction of the ShaderEffects errors.
    Here is the error:

    Cannot create instance of ‘BrightContrastEffect’ defined in assembly ‘WPFBrowserApp, Version=, Culture=neutral, PublicKeyToken=null’. Exception has been thrown by the target of an invocation.

  19. abo Says:

    The contrast equation in this is actually wrong… that line should be;

    result = (result – 0.5) * contrast + 0.5;

    Where the contrast value is between 0 to infinity, and 1 means no change.

    Also, brightness is normally implemented as;

    result = result * brightness

    Where brightness is between 0 and infinity, and 1 means no change. This will increase the brightness while keeping blacks black.

  20. Mandelbrot Set with PixelShader in WPF | Pros Global TV Says:

    [...] schooled HLSL from this article. It explains how to manipulate liughtness and contrariety with pixel shaders. If we wish to know [...]

  21. dday hack ios 2013 Says:

    Hey great website! Does running a blog such as this take a large amount of work?
    I’ve absolutely no knowledge of programming however I was hoping to start my own blog soon. Anyhow, if you have any recommendations or techniques for new blog owners please share. I understand this is off topic nevertheless I simply wanted to ask. Thank you!

  22. Creating a black filter for a custom ShaderEffect | BlogoSfera Says:

    […] am using this code example to adjust brightness and contrast on a BitmapImage for my WPF app. The relevant bit of HLSL code is […]

  23. free movies online Says:

    Oh my goodness! Amazing article dude! Thank you, However I am going through issues
    with your RSS. I don’t know why I am unable to join
    it. Is there anybody getting identical RSS issues?
    Anyone who knows the solution can you kindly respond?

Leave a Reply





WPF Disciples
Code Project