wpf - 对作为窗口背景的图像的模糊效果

标签 wpf xaml background blur effects

我的 WPF 应用程序中有一个以图像作为背景的窗口。我希望该图像模糊。我是这样做的:

这是我的窗口:

<Window x:Class="kiosk.UI.Views.PageSwitch"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converters="clr-namespace:myProjectName.Converters"
    Title="PageSwitch" Height="1024" Width="1280"
    Style="{StaticResource WindowStyle}"
    WindowStyle="None" ResizeMode="NoResize"
    WindowStartupLocation="CenterScreen">        
</Window>   

这是我应用的样式:

<Style x:Key="WindowStyle" TargetType="{x:Type Window}">
        <Setter Property="FontFamily" Value="Verdana" />
        <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>

这有效 - 图像模糊。但是,背景图像周围有一个大约 5 毫米的黑色粗边框。为什么它在那里,我该如何删除它?

这是我尝试过的替代方法:

<VisualBrush Viewbox="0, 0, 1280, 1024" ViewboxUnits="Absolute" >

并且边框被移除但是图像被拉伸(stretch)了很多。几乎一半的图像甚至没有显示。

我该如何解决这个问题?

最佳答案

按照 this answer 中所示进行操作到你之前的一个问题。将 Image 控件放在一个网格中,并将 ClipToBounds 设置为 true。

<VisualBrush>
    <VisualBrush.Visual>
        <Grid ClipToBounds="True">
            <Image Source="Images\myImage.png">
                <Image.Effect>
                    <BlurEffect Radius="20"/>
                </Image.Effect>
            </Image>
        </Grid>
    </VisualBrush.Visual>
</VisualBrush>

关于wpf - 对作为窗口背景的图像的模糊效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25018331/

相关文章:

c# - 将 StackPanel.Visibility 绑定(bind)到其子项的 Visibility 属性

html - 如何用CSS放置背景图片

c# - UWP 仅后台应用程序和异步

WPF 默认 TextBox ContextMenu 样式

c# - ICollectionView获取特定对象

c# - 构建支持我自己的自定义事件的 EventTriggerBehavior

c# - UWP 将 child 转移到其他 parent 问题

css - 加载 CSS 时背景属性的 URL 消失

c# - 如何添加动态文本作为任务栏图标覆盖?

wpf - 是否可以在后面的代码中声明命名空间