0

我试图研究使用 WPF WriteableBitmap 类来允许我的应用程序将不透明蒙版应用于图像。

基本上我有一个蓝色矩形作为图像,另一个 100% 透明的绿色矩形图像在蓝色的顶部。

当用户将鼠标移到绿色(透明)图像上时,我想应用不透明蒙版(可能使用简单的椭圆),使其看起来像正在发生绿色光晕。

我故意不这样做是 XAML 和标准 WPF 效果,因为我真的需要它具有超级性能,我最终会用更高级的 blob 替换椭圆......

有什么想法吗??

谢谢!

4

1 回答 1

2

对不起,我不太明白你的意图。也许如果我能看到图像,我可以从一开始就正确回答,但这是我的第一个可能是错误的答案。

如果你说超级性能,你可能想看看像素着色器。它们由G PU处理,由WPF以自定义效果的形式支持,易于实现。您也可以将着色器应用于播放视频,而使用 WritableBitmap 则很难。

要编写像素着色器,您需要拥有DirectX SDK中的 FX 编译器 (fxc.exe)和Shazzam 工具- Walt Ritscher 的 WYSIWYG WPF Shaders 编译器。

当您同时获得它们时,请继续尝试以下 HLSL 代码

float X : register(C0); // Mouse cursor X position
float Y : register(C1); // Mouse cursor Y position
float4 Color : register(C2); // Mask color
float R : register(C3); // Sensitive circle radius.

sampler2D implicitInputSampler : register(S0);


float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 finalColor = tex2D(implicitInputSampler, uv);
    if ( (uv.x - X) * (uv.x - X) + (uv.y - Y) * (uv.y - Y) < R*R)
    {
        finalColor = Color; // Blend/Change/Mask it as you wish here.
    }
    return finalColor;
}

这为您提供了以下 C# 效果:

namespace Shazzam.Shaders {
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Effects;


    public class AutoGenShaderEffect : ShaderEffect {

        public static DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(AutoGenShaderEffect), 0);

        public static DependencyProperty XProperty = DependencyProperty.Register("X", typeof(double), typeof(AutoGenShaderEffect), new System.Windows.UIPropertyMetadata(new double(), PixelShaderConstantCallback(0)));

        public static DependencyProperty YProperty = DependencyProperty.Register("Y", typeof(double), typeof(AutoGenShaderEffect), new System.Windows.UIPropertyMetadata(new double(), PixelShaderConstantCallback(1)));

        public static DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(System.Windows.Media.Color), typeof(AutoGenShaderEffect), new System.Windows.UIPropertyMetadata(new System.Windows.Media.Color(), PixelShaderConstantCallback(2)));

        public static DependencyProperty RProperty = DependencyProperty.Register("R", typeof(double), typeof(AutoGenShaderEffect), new System.Windows.UIPropertyMetadata(new double(), PixelShaderConstantCallback(3)));

        public AutoGenShaderEffect(PixelShader shader) {
            // Note: for your project you must decide how to use the generated ShaderEffect class (Choose A or B below).
            // A: Comment out the following line if you are not passing in the shader and remove the shader parameter from the constructor

            PixelShader = shader;

            // B: Uncomment the following two lines - which load the *.ps file
            // Uri u = new Uri(@"pack://application:,,,/glow.ps");
            // PixelShader = new PixelShader() { UriSource = u };

            // Must initialize each DependencyProperty that's affliated with a shader register
            // Ensures the shader initializes to the proper default value.
            this.UpdateShaderValue(InputProperty);
            this.UpdateShaderValue(XProperty);
            this.UpdateShaderValue(YProperty);
            this.UpdateShaderValue(ColorProperty);
            this.UpdateShaderValue(RProperty);
        }

        public virtual System.Windows.Media.Brush Input {
            get {
                return ((System.Windows.Media.Brush)(GetValue(InputProperty)));
            }
            set {
                SetValue(InputProperty, value);
            }
        }

        public virtual double X {
            get {
                return ((double)(GetValue(XProperty)));
            }
            set {
                SetValue(XProperty, value);
            }
        }

        public virtual double Y {
            get {
                return ((double)(GetValue(YProperty)));
            }
            set {
                SetValue(YProperty, value);
            }
        }

        public virtual System.Windows.Media.Color Color {
            get {
                return ((System.Windows.Media.Color)(GetValue(ColorProperty)));
            }
            set {
                SetValue(ColorProperty, value);
            }
        }

        public virtual double R {
            get {
                return ((double)(GetValue(RProperty)));
            }
            set {
                SetValue(RProperty, value);
            }
        }
    }
}

现在您可以跟踪鼠标位置,并设置效果的相应属性以触发更改。这里需要注意的一件事:HLSL 代码中的 X 和 Y 的范围是 0 到 1。因此,在将它们传递给着色器之前,您必须将实际坐标转换为百分比。

有关像素着色器和 WPF 的更多信息:

希望这可以帮助 :)

于 2009-10-28T00:22:42.483 回答