在wpf中将背景变成灰度

sud*_*yes 1 wpf colors panel opacitymask

考虑一个上面有大量彩色控件的窗口。当表单中发生某些触发器时,我想在其上放置一个面板,以便所有控件都失去其颜色(所有内容都以灰度显示),除了刚刚弹出的面板。有人可以帮我解决这个问题吗?

And*_*ert 5

我将使用您想要灰度的任何客户区域的 Effect 属性。但是,您需要创建自己的像素着色器来进行灰度转换。

http://windowsclient.net/wpf/wpf35/wpf-35sp1-more-effects.aspx

您可以使用 BlurEffect 类而不是自定义着色器来快速测试您的概念。

<Window x:Class="WpfGrayscaleSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="327" Width="526">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="239*" />
        <RowDefinition Height="50*" />
    </Grid.RowDefinitions>

    <Canvas Name="clientArea" Background="Transparent" Grid.Row="0">
        <!-- Just some controls -->
        <Button Height="31" Name="button1" Width="80" Canvas.Left="30" Canvas.Top="125">Button</Button>
        <Button Height="28" Name="button2" VerticalAlignment="Bottom" Click="button2_Click" HorizontalAlignment="Right" Width="75" Margin="0,0,16,34" Canvas.Left="66" Canvas.Top="54">Button</Button>
        <Rectangle Margin="86,43,0,0" Name="rectangle1" Stroke="Black" Fill="Crimson" Height="59" HorizontalAlignment="Left" VerticalAlignment="Top" Width="109" Canvas.Left="145" Canvas.Top="44" />
    </Canvas>

    <!-- Button to activate the shader effect -->
    <Button Height="23" Margin="15,0,0,21" Name="button3" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="75" Grid.Row="1" Click="button3_Click">Button</Button>
</Grid>
Run Code Online (Sandbox Code Playgroud)

Button3 的事件处理程序就是

 private void button3_Click(object sender, RoutedEventArgs e)
 {
     clientArea.Effect = new BlurEffect() { Radius = 10.0 };
 }
Run Code Online (Sandbox Code Playgroud)

当然,为灰度缩放连接自定义着色器需要做更多的工作,但像素着色器的额外好处将是性能。