Compartilhar via


BlurEffect Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents an effect that you can apply to an object that simulates looking at the object through an out-of-focus lens.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.Media.Effects.Effect
      System.Windows.Media.Effects.BlurEffect

Namespace:  System.Windows.Media.Effects
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public NotInheritable Class BlurEffect _
    Inherits Effect
public sealed class BlurEffect : Effect
<BlurEffect .../>

The BlurEffect type exposes the following members.

Constructors

  Name Description
Public method BlurEffect Initializes a new instance of the BlurEffect class.

Top

Properties

  Name Description
Public property Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Protected property EffectMapping When overridden in a derived class, transforms mouse input and coordinate systems through the effect. (Inherited from Effect.)
Public property Radius Gets or sets the amount of blurring applied by the BlurEffect.

Top

Methods

  Name Description
Public method CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public method ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetAnimationBaseValue Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public method SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Fields

  Name Description
Public fieldStatic member RadiusProperty Identifies the Radius dependency property.

Top

Remarks

The following illustration shows a BlurEffect applied to a visual object (in this case, applied to a Button).

A blurry button versus not blurry

In addition to creating blur effects by using BlurEffect, you can create a drop shadow by using DropShadowEffect.

You can apply only one effect directly to an element at a time. For example, you cannot apply a BlurEffect and a DropShadowEffect to the same element directly. However, you can apply an effect to the container of an element and therefore apply the effect to the nested child. For instance, you could apply a DropShadowEffect to a Button and then apply a BlurEffect to a Grid that contains the Button. This would create a blurry button with a drop shadow. However, stacking effects on one another in this way may have adverse effects on the performance of your application.

Note   Silverlight shader effects are rendered in software mode. Any object that applies an effect will also be rendered in software. Performance is degraded the most when using effects on large visuals or animating properties of an effect. This is not to say that you should not use shader effects in this way at all, but you should use caution and test thoroughly to ensure that your users are getting the experience you expect.

Examples

The following examples show the following:

  • How to use simple markup to blur an object by using XAML.

  • How to use a Style to apply the effect to one or more objects.

  • How to use code to apply the effect to an object.

  • How to use an animation to animate the properties of an effect applied to an object.

The following example shows how to use a BlurEffect to create a blurry Button.

Run this sample

<StackPanel>
    <Button Content="You Can't Read This!"  Width="200">
        <Button.Effect>
            <BlurEffect Radius="10" />
        </Button.Effect>
    </Button>
</StackPanel>

The following example shows how to use a Style to apply a BlurEffect to a Button.

Run this sample

<StackPanel x:Name="LayoutRoot" Background="White">
    <StackPanel.Resources>
        <Style x:Name="buttonStyle" TargetType="Button" >
            <Style.Setters>
                <Setter Property = "Effect" >
                    <Setter.Value>
                        <BlurEffect Radius="10" />
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
    </StackPanel.Resources>
    <Button Width="200" Height="30" Style="{StaticResource buttonStyle}" 
            Margin="20" Content="Blurning down the House!" />
</StackPanel>

The following example shows how to use code to apply a BlurEffect to a Button when it is clicked.

Run this sample

<StackPanel>
    <Button Click="OnClickBlurButton" Width="200" Margin="10" 
            Content="Click to Blur!" />
</StackPanel>
' Toggle Blur effect.
Private Sub OnClickBlurButton(ByVal sender As Object, ByVal args As RoutedEventArgs)

    ' Toggle effect
    If (Not (CType(sender, Button).Effect) Is Nothing) Then
        CType(sender, Button).Effect = Nothing
    Else

        ' Get a reference to the Button.
        Dim myButton As Button = CType(sender, Button)

        ' Initialize a new BlurEffect that will be applied
        ' to the Button.
        Dim myBlurEffect As BlurEffect = New BlurEffect

        ' Set the Radius property of the blur. This determines how 
        ' blurry the effect will be. The larger the radius, the more
        ' blurring. 
        myBlurEffect.Radius = 10

        ' Apply the effect to the Button.
        myButton.Effect = myBlurEffect
    End If
End Sub
// Toggle Blur effect.
private void OnClickBlurButton(object sender, RoutedEventArgs args)
{
    // Toggle effect
    if (((Button)sender).Effect != null)
    {
        ((Button)sender).Effect = null;
    }
    else
    {
        // Get a reference to the Button.
        Button myButton = (Button)sender;

        // Initialize a new BlurEffect that will be applied
        // to the Button.
        BlurEffect myBlurEffect = new BlurEffect();

        // Set the Radius property of the blur. This determines how 
        // blurry the effect will be. The larger the radius, the more
        // blurring. 
        myBlurEffect.Radius = 10;


        // Apply the effect to the Button.
        myButton.Effect = myBlurEffect;
    }
}

The following example shows how to animate the Radius property of the BlurEffect to make the Button become blurry and return to normal after it is clicked.

Run this sample

<StackPanel>
    <StackPanel.Resources>
        <Storyboard x:Name="myStoryboard">
            <!-- Blur the Button and then animate back to normal. -->
            <DoubleAnimation
             Storyboard.TargetName="myBlurBitmapEffect"
             Storyboard.TargetProperty="Radius"
             From="0" To="40" Duration="0:0:0.3"
             AutoReverse="True" />
        </Storyboard>
    </StackPanel.Resources>
    <Button Content="Click to Blur ME!" Click="StartAnimation" Width="200"
            Margin="20">
        <Button.Effect>
            <BlurEffect x:Name="myBlurBitmapEffect" Radius="0" />
        </Button.Effect>
    </Button>
</StackPanel>
Private Sub StartAnimation(ByVal sender As Object, ByVal args As RoutedEventArgs)
    myStoryboard.Begin()
End Sub
private void StartAnimation(object sender, RoutedEventArgs args)
{
    myStoryboard.Begin();
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.