有没有办法让一个物体被画成模糊?

时间:2016-02-20 22:20:34

标签: c# .net wpf

有没有办法,以任何相对简单的方式在WPF(特别是图像)中绘制具有模糊效果的对象,就像这样

private void AnyObjectIWantToBeBlured_MouseEnter(object sender, MouseEventArgs e)
{
(sender as Image).Effect = System.Windows.Media.Effects.BlurEffect;
// i do know this one is not going to work
}
private void AnyObjectIWantTodeblure_MouseLeave(object sender, MouseEventArgs e)
{
(sender as Image).Effect = System.Windows.Media.Effects.NotBlur;
// and this one too
}

谢谢!

1 个答案:

答案 0 :(得分:0)

是的,你差不多自己做了。

private void UIElement_OnMouseEnter(object sender, MouseEventArgs e)
{
    var uiElement = sender as UIElement;

    if (uiElement != null)
    {
        uiElement.Effect = new BlurEffect();
    }
}

private void UIElement_OnMouseLeave(object sender, MouseEventArgs e)
{
    var uiElement = sender as UIElement;

    if (uiElement != null)
    {
        uiElement.Effect = null;
    }
}

我会用Style来做。

<Style x:Key="BlurOnMouseOverStyle" TargetType="{x:Type FrameworkElement}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Effect">
                <Setter.Value>
                    <BlurEffect></BlurEffect>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

您不需要这样的事件处理程序。