Click here to Skip to main content
11,636,778 members (77,250 online)
Click here to Skip to main content

Blur Effect on Control

, 3 Apr 2014 CPOL 7.4K 347 9
Rate this:
Please Sign up or sign in to vote.
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.

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

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

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

The source code is as follows:

    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)

Share

About the Author

RumataEstorish
Technical Lead
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralGood job Pin
dyma25-Aug-14 1:56
memberdyma25-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
memberTridip Bhattacharjee3-Apr-14 20:45 
AnswerRe: Query about the code Pin
RumataEstorish3-Apr-14 21:35
memberRumataEstorish3-Apr-14 21:35 
Questiontry-finally Pin
p4koo3-Apr-14 11:04
memberp4koo3-Apr-14 11:04 
AnswerRe: try-finally Pin
RumataEstorish3-Apr-14 15:13
memberRumataEstorish3-Apr-14 15:13 
AnswerRe: try-finally Pin
Daniele Rota Nodari3-Apr-14 21:55
memberDaniele Rota Nodari3-Apr-14 21:55 
GeneralRe: try-finally Pin
RumataEstorish3-Apr-14 22:23
memberRumataEstorish3-Apr-14 22:23 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 3 Apr 2014
Article Copyright 2014 by RumataEstorish
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid