摘要
WPF提供了可应用于任何元素的可视化效果。效果的目标是提供一种简单的声明式方法,从而改进文本、图像、按钮以及其他控件的外观。不是编写自己的绘图代码,而是使用某个继承自Effect的类(位于System.Windows.Media.Effects名称空间中)以立即获得诸如模糊、辉光以及阴影等效果。

正文
BlurEffect类
最简单的WPF效果是BlurEffect类。该类模糊元素的内容,就想通过失焦透镜观察到得效果。通过增加Radiu属性的值(默认值是5)可增加模糊程度。
<StackPanel>
<Button Content="Text" Margin="10" Padding="10">
<Button.Effect>
<BlurEffect Radius="5"></BlurEffect>
</Button.Effect>
</Button>
<TextBlock Text="This is test" Margin="10" Padding="10">
<TextBlock.Effect>
<BlurEffect Radius="2"></BlurEffect>
</TextBlock.Effect>
</TextBlock>
<Button Content="Text" Margin="10" Padding="10"></Button>
</StackPanel>
DropShadowEffect类
DropShadowEffect类在元素背后添加了轻微的偏移阴影。

<StackPanel>
<TextBlock Text="This is test DropShadowEffect" Margin="10" Padding="10">
<TextBlock.Effect>
<DropShadowEffect></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
<TextBlock Text="This is test DropShadowEffect" Margin="10" Padding="10">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="0"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
<TextBlock Text="This is test DropShadowEffect" Margin="10" Padding="10">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="10"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
<TextBlock Text="This is test DropShadowEffect" Margin="10" Padding="10">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="10" Color="Red" BlurRadius="2"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>
</StackPanel>
ShaderEffect类
ShaderEffect类没有提供就绪的效果。相反,它是一个抽象类,可继承该类以创建自己的自定义像素着色器。通过使用ShaderEffect类(或从该类派生的自定义效果),可实现更多的效果,而不仅局限于模糊和阴影
派生自 ShaderEffect 类,以基于单个像素着色器实现自定义效果。 以下步骤演示如何创建自定义效果。 1.PixelShader从预编译的高级着色语言 (HLSL) 字节码加载。 2.定义表示效果的参数和 Brush基于图面输入的依赖属性。 使用其中 RegisterPixelShaderSamplerProperty 一个重载将这些输入与 HLSL 字节码中引用的寄存器号相关联。
使用 Shazzam Shader Editor 编写 HLSL 像素着色器代码
HLSL,High Level Shader Language,高级着色器语言,是 Direct3D 着色器模型所必须的语言。WPF 支持 Direct3D 9,也支持使用 HLSL 来编写着色器。你可以使用任何一款编辑器来编写 HLSL,但 Shazzam Shader Editor 则是专门为 WPF 实现像素着色器而设计的一款编辑器,使用它来编写像素着色器,可以省去像素着色器接入到 WPF 所需的各种手工操作。
Shazzam Shader Editor 工具
GitHub - JohanLarsson/Shazzam: A fork of https://shazzam.codeplex.com/ *载下**
目录→AppData\Local\Shazzam\GeneratedShadersg下找到你需要的效果,Copy ".ps" ".cs"文件到你的项目中,修改项目中".ps"文件属性

修改".cs"文件
pixelShader.UriSource = new Uri("InvertColorEffect.ps", UriKind.Relative);
<Window x:Class="_32.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_32"
xmlns:eff="clr-namespace:Shazzam.Shaders"
mc:Ignorable="d"
Title="Window2" Height="450" Width="800">
<Grid x:Name="grid">
<Grid.Background>
<ImageBrush ImageSource="/碳.jpg"></ImageBrush>
</Grid.Background>
<CheckBox x:Name="chk" Click="chk_Click">修改效果</CheckBox>
</Grid>
</Window>
private void chk_Click(object sender, RoutedEventArgs e)
{
if(chk.IsChecked == true)
{
grid.Effect = new InvertColorEffect();
}
else
{
grid.Effect = null;
}
}