首页 > 解决方案 > WPF 通过触发器或行为更改/修改图像的颜色饱和度/亮度

问题描述

我想Image根据一个bool值改变饱和度。

<Image Source="/Images/256/dialog-error-4.png" />

原来的

在此处输入图像描述

我想要什么 - 改变饱和度

在此处输入图像描述

是否可以通过我不知道的隐藏功能来实现这一点,或者我应该创建图像的副本并将它们替换为我Controls的 by Trigger


解决方案

来源:http ://bursjootech.blogspot.com/2008/06/grayscale-effect-pixel-shader-effect-in.html

灰度效果.cs

public class GrayscaleEffect : ShaderEffect
{
    private static readonly PixelShader _PixelShader = new PixelShader() { UriSource = new Uri(@"pack://application:,,,/MyApplicationName;component/Effects/GrayscaleEffect.ps") };

    // ##############################################################################################################################
    // Properties
    // ##############################################################################################################################

    #region Properties

    public static readonly DependencyProperty InputProperty = RegisterPixelShaderSamplerProperty("Input", typeof(GrayscaleEffect), 0);
    public Brush Input
    {
        get => (Brush)GetValue(InputProperty);
        set => SetValue(InputProperty, value);
    }

    public static readonly DependencyProperty DesaturationFactorProperty = DependencyProperty.Register("DesaturationFactor", typeof(double), typeof(GrayscaleEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(0), CoerceDesaturationFactor));
    public double DesaturationFactor
    {
        get => (double)GetValue(DesaturationFactorProperty);
        set => SetValue(DesaturationFactorProperty, value);
    }

    #endregion

    // ##############################################################################################################################
    // Constructor
    // ##############################################################################################################################

    #region Constructor

    public GrayscaleEffect()
    {
        PixelShader = _PixelShader;

        UpdateShaderValue(InputProperty);
        UpdateShaderValue(DesaturationFactorProperty);
    }

    #endregion

    // ##############################################################################################################################
    // private methods
    // ##############################################################################################################################

    #region private methods

    private static object CoerceDesaturationFactor(DependencyObject d, object value)
    {
        GrayscaleEffect effect = (GrayscaleEffect)d;
        double newFactor = (double)value;

        if (newFactor < 0.0 || newFactor > 1.0)
        {
            return effect.DesaturationFactor;
        }

        return newFactor;
    }

    #endregion        
}

灰度效果.fx

在 GrayscaleEffect-project 中,添加两个新文件(Add -> New Item)。GrayscaleEffect.fx 和 GrayscaleEffect.ps 作为文本文件。确保 GrayscaleEffect.ps 在属性中设置为资源(在构建操作中)。注意: GrayscaleEffect.fx 必须是 ANSI 格式。通过在记事本中打开文件将其转换为 ANSI 并保存,在保存对话框中选择 ANSI 格式。

sampler2D implicitInput : register(s0);
float factor : register(c0);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 color = tex2D(implicitInput, uv);
    float gray = color.r * 0.3 + color.g * 0.59 + color.b *0.11;    

    float4 result;    
    result.r = (color.r - gray) * factor + gray;
    result.g = (color.g - gray) * factor + gray;
    result.b = (color.b - gray) * factor + gray;
    result.a = color.a;

    return result;
}

灰度效果.ps

编译后的源文件:https ://files.dominic-jonas.de/stackoverflow/GrayscaleEffect.ps

标签: c#wpfimagecolors

解决方案


推荐阅读