有没有办法使要绘制的对象模糊

本文关键字:对象 模糊 绘制 有没有 | 更新日期: 2023-09-27 18:35:28

有什么方法可以以任何相对简单的方式在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
}

谢谢!

有没有办法使要绘制的对象模糊

是的,你几乎自己做到了。

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>

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