Click here to Skip to main content
15,884,937 members
Articles / Desktop Programming / WPF
Tip/Trick

Blur Effect on Control

Rate me:
Please Sign up or sign in to vote.
3.88/5 (8 votes)
3 Apr 2014CPOL 39.3K   2.1K   11   10
Simple code for applying and disabling blur effect on any UIElement

Introduction

This is a very simple class to apply and remove blur effect on UIElement in WPF.

Using the Code

You have only two extension methods: BlurApply and BlurDisable. The first one is called before applying effect - for example before showing dialog. The second one is usually called in finally block to guarantee returning to normal view even if exception occurred.

C#
try
{
    this.BlurApply(BlurRadius, new TimeSpan(0, 0, 1), TimeSpan.Zero);
       MessageBox.Show("Test");
}
finally
{
       this.BlurDisable(new TimeSpan(0, 0, 5), TimeSpan.Zero);
}

Image 1

Image 2

Also, you can apply this to any UIElement, like this TextBlock:

C#
try
           {
               txt.BlurApply(BlurRadius, new TimeSpan(0, 0, 1), TimeSpan.Zero);
               MessageBox.Show("Test");
           }
           finally
           {
               txt.BlurDisable(new TimeSpan(0, 0, 5), TimeSpan.Zero);
           }

Image 3

The source code is as follows:

C#
public static class BlurElementExtension
{
    /// <summary>
    /// Turning blur on
    /// </summary>
    /// <param name="element">bluring element</param>
    /// <param name="blurRadius">blur radius</param>
    /// <param name="duration">blur animation duration</param>
    /// <param name="beginTime">blur animation delay</param>
    public static void BlurApply(this UIElement element,
        double blurRadius, TimeSpan duration, TimeSpan beginTime)
    {
            BlurEffect blur = new BlurEffect() { Radius = 0 };
            DoubleAnimation blurEnable = new DoubleAnimation(0, blurRadius, duration)
                { BeginTime = beginTime };
            element.Effect = blur;
            blur.BeginAnimation(BlurEffect.RadiusProperty, blurEnable);
    }
    /// <summary>
    /// Turning blur off
    /// </summary>
    /// <param name="element">bluring element</param>
    /// <param name="duration">blur animation duration</param>
    /// <param name="beginTime">blur animation delay</param>
    public static void BlurDisable(this UIElement element, TimeSpan duration, TimeSpan beginTime)
    {
        BlurEffect blur = element.Effect as BlurEffect;
        if (blur == null || blur.Radius == 0)
        {
            return;
        }
        DoubleAnimation blurDisable = new DoubleAnimation(blur.Radius, 0, duration) { BeginTime = beginTime };
        blur.BeginAnimation(BlurEffect.RadiusProperty, blurDisable);
    }
}

Thank you!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNo Util project Pin
Dominique Bijnens25-Feb-20 0:50
Dominique Bijnens25-Feb-20 0:50 
QuestionColor of blur Pin
EasyHero5-Oct-17 23:23
professionalEasyHero5-Oct-17 23:23 
GeneralGood job Pin
dyma25-Aug-14 1:56
dyma25-Aug-14 1:56 
GeneralMy vote of 5 Pin
Volynsky Alex4-Apr-14 11:33
professionalVolynsky Alex4-Apr-14 11:33 
QuestionQuery about the code Pin
Tridip Bhattacharjee3-Apr-14 20:45
professionalTridip Bhattacharjee3-Apr-14 20:45 
AnswerRe: Query about the code Pin
RumataEstorish3-Apr-14 21:35
RumataEstorish3-Apr-14 21:35 
Questiontry-finally Pin
Pakosh3-Apr-14 11:04
Pakosh3-Apr-14 11:04 
AnswerRe: try-finally Pin
RumataEstorish3-Apr-14 15:13
RumataEstorish3-Apr-14 15:13 
AnswerRe: try-finally Pin
Daniele Rota Nodari3-Apr-14 21:55
Daniele Rota Nodari3-Apr-14 21:55 
GeneralRe: try-finally Pin
RumataEstorish3-Apr-14 22:23
RumataEstorish3-Apr-14 22:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.