Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blur effect on image as a window background

I have a window in my WPF app with an image as a background. I want that image to be blurred. This is how I do it:

This is my window:

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

And here's the style I apply to it:

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

This works - the image is blurred. However, there's a thick black border of about 5 mm around the background image. Why is it there and how can I remove it?

Here's what I tried as an alternative:

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

and the border is removed but the image is stretched a lot. Almost half of the image isn't even shown.

How can I fix this?

like image 788
petko_stankoski Avatar asked Oct 16 '25 13:10

petko_stankoski


1 Answers

Do it like shown in this answer to one of your previous questions. Put the Image control in a Grid with ClipToBounds set to true.

<VisualBrush>
    <VisualBrush.Visual>
        <Grid ClipToBounds="True">
            <Image Source="Images\myImage.png">
                <Image.Effect>
                    <BlurEffect Radius="20"/>
                </Image.Effect>
            </Image>
        </Grid>
    </VisualBrush.Visual>
</VisualBrush>
like image 73
Clemens Avatar answered Oct 18 '25 03:10

Clemens