Compartir a través de


Cómo: Aplicar un efecto de desenfoque a un objeto Visual

Actualización: noviembre 2007

BlurBitmapEffect se puede utilizar para desenfocar un objeto visible. A continuación figuran varios ejemplos que muestran lo siguiente:

  • Cómo utilizar el marcado simple para aplicar el efecto a un objeto

  • Cómo utilizar un estilo Style para aplicar el efecto a uno o más objetos

  • Cómo utilizar el código para aplicar el efecto a un objeto

  • Cómo utilizar una animación para animar las propiedades de un efecto aplicado a un objeto

Nota: en todos los ejemplos siguientes se aplica un solo efecto a un objeto. Para aplicar varios efectos, puede utilizar BitmapEffectGroup. Vea Cómo: Crear varios efectos visuales para obtener ejemplos.

Ejemplo

En el ejemplo siguiente se muestra cómo utilizar un efecto BlurBitmapEffect para crear un control Button desenfocado.

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">

  <StackPanel>
    <Button  Width="200">You Can't Read This!
      <Button.BitmapEffect>

      <!-- <BitmapEffectGroup> would go here if you wanted to apply more 
             then one effect to the Button. However, in this example only  
             one effect is being applied so BitmapEffectGroup does not need  
             to be included. -->

        <!-- The larger the Radius, the more blurring. The default range is 20.
             In addition, the KernelType is set to a box kernel. A box kernel
             creates less disruption (less blur) then the default Gaussian kernel. -->
        <BlurBitmapEffect Radius="10" KernelType="Box" />

      </Button.BitmapEffect>
    </Button>

  </StackPanel>

</Page>

En la ilustración siguiente se muestra el efecto creado en el ejemplo anterior.

Captura de pantalla: Botón difuminado utilizando BlurBitmapEffect

En el ejemplo siguiente se muestra cómo utilizar Style para aplicar el efecto BlurBitmapEffect a un objeto Button en la página mientras se presiona.

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml">

  <!-- Resources define Styles for the entire page. -->
  <Page.Resources>

    <!-- This style applies to any Button on the page. -->
    <Style TargetType="{x:Type Button}">
      <Style.Triggers>

        <!-- When the Button is pressed, apply the blur. -->
        <Trigger Property="IsPressed" Value="true">
          <Setter Property = "BitmapEffect" >
            <Setter.Value>
              <BlurBitmapEffect Radius="10" />
            </Setter.Value>
          </Setter>
        </Trigger>
      </Style.Triggers>
    </Style>
  </Page.Resources>

  <StackPanel>

    <!-- The Style defined above applies to this Button which makes
         the Button appear blurred while it is pressed. -->
    <Button Width="200" >Blurning down the House!</Button>

  </StackPanel>

</Page>

En el ejemplo siguiente se muestra cómo utilizar el código para aplicar un efecto BlurBitmapEffect a un objeto Button cuando se hace clic en él.

A continuación se muestra el marcado Lenguaje de marcado de aplicaciones extensible (XAML) del ejemplo.

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.BlurExample" >

  <StackPanel>
    <Button Click="OnClickBlurButton" Width="200">Click to Blur!</Button>
  </StackPanel>

</Page>

El ejemplo siguiente es el código que controla el evento para el marcado Lenguaje de marcado de aplicaciones extensible (XAML).

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Media.Effects;

namespace SDKSample
{

   public partial class BlurExample : Page
   {

      // Add Blur effect.
      void OnClickBlurButton(object sender, RoutedEventArgs args)
      {
         // Toggle effect
         if (((Button)sender).BitmapEffect != null)
         {
            ((Button)sender).BitmapEffect = null;
         }
         else
         {
            // Get a reference to the Button.
            Button myButton = (Button)sender;

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

            // 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;

            // Set the KernelType property of the blur. A KernalType of "Box"
            // creates less blur than the Gaussian kernal type.
            myBlurEffect.KernelType = KernelType.Box;

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

   }
}

En el ejemplo siguiente se muestra cómo animar la propiedad Radius de BlurBitmapEffect para que el objeto Button se desenfoque después de hacer clic en él.

<Page xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" >

  <StackPanel>

    <Button  Width="200">
      Click to Blur ME!
      <Button.BitmapEffect>

        <!-- This BitmapEffect is targeted by the animation. -->
        <BlurBitmapEffect x:Name="myBlurBitmapEffect"  Radius="0" />

      </Button.BitmapEffect>
      <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
          <BeginStoryboard>
            <Storyboard>

              <!-- 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>
          </BeginStoryboard>
        </EventTrigger>
      </Button.Triggers>
    </Button>

  </StackPanel>

</Page>

Vea también

Tareas

Cómo: Crear un efecto de resplandor en el margen externo de un objeto

Cómo: Crear un efecto visual de sombra paralela

Cómo: Crear varios efectos visuales

Cómo: Animar varios efectos visuales

Conceptos

Información general sobre efectos de mapa de bits