petko_stankoski
petko_stankoski

Reputation: 10713

Blur effect on window background image

I have a window with image as background. On that window I also have buttons and other controls.

This is my style for that window:

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

The problem is that the blur effect is applied to the whole window and not just the background image. So, my buttons are also blurred, which is not what I want. I want just the image background to be blurred, and not the buttons. How can I do that?

Upvotes: 3

Views: 8671

Answers (1)

Clemens
Clemens

Reputation: 128013

Instead of using an ImageBrush for the Background of your window, add an Image control as first (lowest) element to the top-level container of your window, and set the Effect there:

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

        <!-- other UI elements -->

    </Grid>
</Window>

If you really need a Background Brush, you might use a VisualBrush instead of an ImageBrush like this:

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

In order to crop the border of the blurred background brush, you could adjust the Viewbox (which by default is given in relative units) like so:

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

Of course the precise values depend on the absolute size of the image. You might also specifiy the Viewbox in absolute units by setting ViewboxUnits="Absolute":

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

Upvotes: 7

Related Questions