c# - 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
解决方案
推荐阅读
- haskell - Haskell:发现两次相同的模块 - “模棱两可的模块名称”
- node.js - 如何响应 Node/Express/Postgresql 中的查询结果数组?
- c# - 在 RowDoubleClicked Blazorise DataGrid 上显示 DetailRow
- angular - Karma 代码覆盖率未在 Sonarqube 中显示
- javascript - Client.query 未在 Node JS 中检索数据
- c++ - AVL树删除c ++
- mongodb - StitchServiceError "aws: "aws_service" 是必需的字符串", errorCodeName: InvalidParameter
- node.js - 在 cloudformation 中修剪机密管理器 ARN
- java - FlatMapFunction 没有定义一个 serialversionUid
- php - 以安全、私密和可扩展的方式将用户图像保存在服务器中的最佳方法是什么?