模糊对窗口背景图像的影响

时间:2014-07-23 07:28:17

标签: wpf xaml background blur effects

我有一个以图像为背景的窗口。在那个窗口我也有按钮和其他控件。

这是我对那个窗口的风格:

<Style x:Key="MyWindow" TargetType="{x:Type Window}">
        <Setter Property="Background">
            <Setter.Value>
                <ImageBrush ImageSource="Images\myImage.png" />
            </Setter.Value>
        </Setter>
        <Setter Property="Effect">
            <Setter.Value>
                <BlurEffect Radius="20" />
            </Setter.Value>
        </Setter>
    </Style>

问题是模糊效果应用于整个窗口而不仅仅是背景图像。所以,我的按钮也模糊不清,这不是我想要的。我想只是图像背景模糊,而不是按钮。我怎么能这样做?

1 个答案:

答案 0 :(得分:5)

不使用ImageBrush作为窗口背景,而是将Image控件作为第一个(最低)元素添加到窗口的顶级容器中,并在那里设置效果:

<Window ...>
    <Grid>
        <Image Source="Images\myImage.png" Stretch="Fill">
            <Image.Effect>
                <BlurEffect Radius="20"/>
            </Image.Effect>
        </Image>

        <!-- other UI elements -->

    </Grid>
</Window>

如果你真的需要背景画笔,你可以使用VisualBrush而不是像这样的ImageBrush:

<Style TargetType="Window">
    <Setter Property="Background">
        <Setter.Value>
            <VisualBrush>
                <VisualBrush.Visual>
                    <Image Source="Images\myImage.png">
                        <Image.Effect>
                            <BlurEffect Radius="20"/>
                        </Image.Effect>
                    </Image>
                </VisualBrush.Visual>
            </VisualBrush>
        </Setter.Value>
    </Setter>
    ...
</Style>

为了裁剪模糊背景画笔的边框,您可以调整Viewbox(默认情况下以相对单位给出),如下所示:

<VisualBrush Viewbox="0.05,0.05,0.9,0.9">

当然,精确值取决于图像的绝对大小。您也可以通过设置ViewboxUnits="Absolute"

以绝对单位指定Viewbox
<VisualBrush Viewbox="0,0,1024,1280" ViewboxUnits="Absolute">
相关问题