WPF通过触发器或行为更改/修改图像的色彩饱和度/亮度

时间:2018-07-19 07:41:54

标签: c# wpf image colors

我想基于Image值更改bool的饱和度。

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

原始

enter image description here

我想要的-更改饱和度

enter image description here

是否可以通过我不知道的隐藏功能来实现此目的,或者应该创建图像的副本并将其Controls内替换为Trigger


解决方案

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

GrayscaleEffect.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        
}

GrayscaleEffect.fx

  

在GrayscaleEffect项目中,添加两个新文件(Add-> New Item)。 GrayscaleEffect.fx和GrayscaleEffect.ps作为文本文件。确保在Properties中将GrayscaleEffect.ps设置为资源(在Build Action中)。注意: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;
}

GrayscaleEffect.ps

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

0 个答案:

没有答案
相关问题