好的,我找到了解决方案.4个简单的步骤.
1)下载并安装Shader Effects BuildTask和模板
2)下载WPF Pixel Shader库.在MainSolution文件夹中打开解决方案,右键单击WPFShaderEffectLibrary进行编译,然后将已编译的DLL的引用添加到项目中.注意:只编译WPFShaderEffectLibrary,如果您尝试编译整个解决方案,它可能无法正常工作.
3)使用像素着色器如下(我在MainWindow构造函数中使用它,但这并不重要):
public MainWindow()
{
InitializeComponent();
ColorKeyAlphaEffect effect = new ColorKeyAlphaEffect();
Brush brush = Effect.ImplicitInput;
effect.Input = brush;
// This is the Image control in your xaml. It should contain
// the image in which you want the green screen effect
imageControl.Effect = effect;
}
Run Code Online (Sandbox Code Playgroud)
你需要这个库:
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using ShaderEffectLibrary;
Run Code Online (Sandbox Code Playgroud)
4)着色器只适用于黑色,所以有一些东西你需要改变才能使它与绿色(或你喜欢的任何其他颜色)一起使用,在我的情况下,我希望它能够使用绿色(如绿屏) ,所以我改变了效果的价值.在从Codeplex下载的解决方案中,您必须转到WPFShaderEffectLibrary项目 - > ShaderSource文件夹ColorKeyAlpha.fx文件.在那里,您必须更改以下代码:
float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 color = tex2D( implicitInputSampler, uv );
// FROM THIS
if( color.r + color.g + color.b < 0.3 ) {
color.rgba = 0;
}
return color;
}
Run Code Online (Sandbox Code Playgroud)
对此
float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 color = tex2D( implicitInputSampler, uv );
// TO THIS
if( color.r == 0 && color.g == 1.0 && color.b == 0 ) {
color.rgba = 0;
}
return color;
}
Run Code Online (Sandbox Code Playgroud)
完成后,重新编译WPFShaderEffectLibrary项目,并更新项目中的引用(步骤#2中的引用).更新参考后,PixelShader将开始使绿色值(R = 0,G = 255,B = 0)完全透明.它适用于图像和视频.
我真的很难实现这一点,我希望它对读这个的人有用:).
谢谢!