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

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

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

我故意不这样做是 XAML 和标准 WPF 效果,因为我确实需要它具有超级性能,并且我最终将用更高级的斑点替换椭圆......

有什么想法吗??

谢谢!

有帮助吗?

解决方案

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

如果你说超级性能,你可能想看看像素着色器。它们的处理方式是 GPU,由WPF以自定义效果的形式支持,易于实现。您还可以应用着色器来播放视频,而使用 WritableBitmap 则很难做到这一点。

要编写像素着色器,您需要有 FX 编译器 (fxc.exe) DirectX SDK沙扎姆工具 - Walt Ritscher 所见即所得的 WPF 着色器编译器。

当您同时获得它们后,请继续尝试以下 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 的更多内容,请阅读以下内容:

希望这可以帮助 :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top