wpf 对窗口背景图像的模糊效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24904292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Blur effect on window background image
提问by petko_stankoski
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?
问题是模糊效果应用于整个窗口而不仅仅是背景图像。所以,我的按钮也模糊了,这不是我想要的。我只想模糊图像背景,而不是按钮。我怎样才能做到这一点?
回答by Clemens
Instead of using an ImageBrush for the Background of your window, add an Imagecontrol as first (lowest) element to the top-level container of your window, and set the Effect there:
不要使用 ImageBrush 作为窗口的背景,而是将Image控件作为第一个(最低)元素添加到窗口的顶级容器,并在那里设置 Effect:
<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 VisualBrushinstead of an ImageBrush like this:
如果你真的需要一个背景画笔,你可以VisualBrush像这样使用 a而不是 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>
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:
为了裁剪模糊背景画笔的边框,您可以Viewbox像这样调整(默认情况下以相对单位给出):
<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":
当然,精确值取决于图像的绝对大小。您还可以通过设置以绝对单位指定 Viewbox ViewboxUnits="Absolute":
<VisualBrush Viewbox="0,0,1024,1280" ViewboxUnits="Absolute">

