• Blog
  • Info Support
  • Career
  • Training
  • International Group
  • Info Support
  • Blog
  • Career
  • Training
  • International Group
  • Search
logo InfoSupport
  • Latest blogs
  • Popular blogs
  • Experts
      • All
      • Bloggers
      • Speakers
  • Meet us
  • About us
    • nl
    • en
    • .NET
    • 3D printing
    • Advanced Analytics
    • Agile
    • Akka
    • Alexa
    • Algorithms
    • Api's
    • Architectuur
    • Artificial Intelligence
    • ATDD
    • Augmented Reality
    • AWS
    • Azure
    • Big Data
    • Blockchain
    • Business Intelligence
    • Chatbots
    • Cloud
    • Code Combat
    • Cognitive Services
    • Communicatie
    • Containers
    • Continuous Delivery
    • CQRS
    • Cyber Security
    • Dapr
    • Data
    • Data & Analystics
    • Data Science
    • Data Warehousing
    • Databricks
    • DataOps
    • Developers life
    • DevOps
    • Digital Days
    • Digital Twin
    • Docker
    • eHealth
    • Enterprise Architecture
    • Event Sourcing
    • Hacking
    • Infrastructure & Hosting
    • Innovatie
    • Integration
    • Internet of Things
    • Java
    • Machine Learning
    • Microservices
    • Microsoft
    • Microsoft Bot Framework
    • Microsoft Data Platform
    • Mobile Development
    • Mutation Testing
    • Open source
    • Pepper
    • Power BI
    • Privacy & Ethiek
    • Python
    • Quality Assistance & Test
    • Quality Assurance & Test
    • Requirements Management
    • Scala
    • Scratch
    • Security
    • SharePoint
    • Software Architecture
    • Software development
    • Software Factory
    • SQL Server
    • SSL
    • Start-up
    • Startup thinking
    • Stryker
    • Test Quality
    • Testing
    • TLS
    • TypeScript
    • Various
    • Web Development
    • Web-scale IT
    • Xamarin
    • All
    • Bloggers
    • Speakers
Home » Adding Noise Using a WPF Shader Effect
  • Adding Noise Using a WPF Shader Effect

    • By Erno de Weerd
    • .NET 11 years ago
    • .NET 0 comments
    • .NET .NET
    Adding Noise Using a WPF Shader Effect

    Inspired by this post I decided to try and create a noise effect to make it easier to add some noise to the, sometimes too smooth, gradients in Windows Presentation Foundation.I wanted the XAML code to be as simple as possible:

    <Rectangle Fill="{StaticResource gradient}">
        <Rectangle.Effect>
            <eff:NoiseEffect Ratio="0.2" />
        </Rectangle.Effect>
    </Rectangle>

    Here is how I accomplished this.

    I started with Shazzam and created a pixel shader:

    sampler2D input : register(s0);
    sampler2D randomInput : register(s1);
    
    
    /// <summary>Ratio: 0 is 100% input source, 1 is full noise</summary>
    /// <minValue>0/minValue>
    /// <maxValue>1</maxValue>
    /// <defaultValue>.5</defaultValue>
    float Ratio : register(C0);
    float4 main(float2 uv : TEXCOORD) : COLOR
    {
        float4 inputTex = tex2D(input, uv);
        float4 randomTex = tex2D(randomInput, uv);
        float4 noisedTex = inputTex + (inputTex * randomTex*Ratio);
        return noisedTex;
    }

    The pixel shader takes the current pixel and highlights the color of the pixel with a sample from a second image. Of course the second image can be anything so this shader can also be used to create a watermark effect.

    I extended constructor in the ShaderEffect class that was generated by Shazzam a little:

    public NoiseEffect()
    {
        PixelShader pixelShader = new PixelShader();
        pixelShader.UriSource =
            new Uri("/Shaders;component/Noise.ps", UriKind.Relative);
        this.PixelShader = pixelShader;
        BitmapImage bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.UriSource =
            new Uri("pack://application:,,,/Shaders;component/Images/noise.png");
        bitmap.EndInit();
        this.RandomInput =
            new ImageBrush(bitmap)
                {
                    TileMode = System.Windows.Media.TileMode.Tile,
                    Viewport = new Rect(0, 0, 800, 600),
                    ViewportUnits = BrushMappingMode.Absolute
                };
    
    
        this.UpdateShaderValue(InputProperty);
        this.UpdateShaderValue(RandomInputProperty);
        this.UpdateShaderValue(RatioProperty);
    }

    I added the loading of a default bitmap (noise.png) that is embedded in the Shaders.dll. The bitmap is 800 by 600 pixels and to prevent scaling I set the TileMode to Tile and adjusted the Viewport so the image will be repeated instead of being stretched.

    Here’s the bitmap, created with Paint.NET:

    noise

    All that was left was creating a demo that shows the difference between not using the effect, the default effect and the effect using a custom image:

    image

    The top image shows the linear gradient without any effects. The second with the default noise and the last one with a custom circular image:

    noise64x64

    The third rectangle is much brighter; the higher the effect ratio the lighter the image will become. The ratio of the middle rectangle is 0.2 and the last rectangle has an effect ratio of 1.0

    Download the effect, sources and dll, here.

    Share this

Erno de Weerd

View profile

Related IT training

Go to training website

Related Consultancy solutions

Go to infosupport.com

Related blogs

  • Building a custom Kubernetes operator in C#

    Building a custom Kubernetes operator in C# Willem Meints - 3 weeks ago

  • Transform SpecFlow Table Column

    Transform SpecFlow Table Column Ronald Bosma - 3 months ago

  • Building a passwordless login flow with WebAuthn and th…

    Building a passwordless login flow with WebAuthn and th… Willem Meints - 5 months ago

Data Discovery Channel

  • Modern Data Platform

  • Gartner Data & Analytics Summit 2022

  • De Data Architecture ®Evolution

Nieuwsbrief

* verplichte velden

Contact

  • Head office NL
  • Kruisboog 42
  • 3905 TG Veenendaal
  • T +31 318 552020
  • Call
  • Mail
  • Directions
  • Head office BE
  • Generaal De Wittelaan 17
  • bus 30 2800 Mechelen
  • T +32 15 286370
  • Call
  • Mail
  • Directions

Follow us

  • Twitter
  • Facebook
  • Linkedin
  • Youtube

Newsletter

Sign in

Extra

  • Media Library
  • Disclaimer
  • Algemene voorwaarden
  • ISHBS Webmail
  • Extranet
Beheer cookie toestemming
Deze website maakt gebruik van Functionele en Analytische cookies voor website optimalisatie en statistieken.
Functioneel Always active
De technische opslag of toegang is strikt noodzakelijk voor het legitieme doel het gebruik mogelijk te maken van een specifieke dienst waarom de abonnee of gebruiker uitdrukkelijk heeft gevraagd, of met als enig doel de uitvoering van de transmissie van een communicatie over een elektronisch communicatienetwerk.
Voorkeuren
De technische opslag of toegang is noodzakelijk voor het legitieme doel voorkeuren op te slaan die niet door de abonnee of gebruiker zijn aangevraagd.
Statistieken
De technische opslag of toegang die uitsluitend voor statistische doeleinden wordt gebruikt. De technische opslag of toegang die uitsluitend wordt gebruikt voor anonieme statistische doeleinden. Zonder dagvaarding, vrijwillige naleving door uw Internet Service Provider, of aanvullende gegevens van een derde partij, kan informatie die alleen voor dit doel wordt opgeslagen of opgehaald gewoonlijk niet worden gebruikt om je te identificeren.
Marketing
De technische opslag of toegang is nodig om gebruikersprofielen op te stellen voor het verzenden van reclame, of om de gebruiker op een website of over verschillende websites te volgen voor soortgelijke marketingdoeleinden.
Manage options Manage services Manage vendors Read more about these purposes
Voorkeuren
{title} {title} {title}