WPF 内发光效果

标签 wpf .net-4.0

有人知道如何在 WPF 中使用表达式混合或已弃用的 BitmapEffects 制作内发光效果吗?

示例图片:

enter image description here

例如,这里有一些带有图像和一些文本的按钮的 xaml。我希望这个按钮具有内部发光(而不是外部发光):

<Button Click="HandleDeleteRows" Style="{StaticResource ButtonCellStyle}">
    <DockPanel>
        <Image Style="{StaticResource DeleteButtonImage}" />
        <TextBlock Style="{StaticResource DeleteButtonCaption}" />
    </DockPanel>
</Button>

最佳答案

虽然 PompolutZ 的答案解决了我上面的简化示例,但我无法覆盖我想要在现实世界示例中应用样式的控件的控件模板 - 所以我开始定义自己的效果,按照说明 here .

第 1 步 - 编写一个 HLSL .FX 文件来实现您想要的效果。我放弃了发光,因为它太复杂了,因为它需要边缘检测。我决定采用一系列标准颜色、亮度、 Gamma 和饱和度调整,这些调整相当容易实现,并且可以让我创建一些良好的视觉提示。使用常识并在线查找像素着色算法,它们很容易实现。

ColourAdjust.fx:

sampler2D implicitInput : register(s0);
float saturation : register(c0);
float gamma : register(c1);
float brightness : register(c2);
float red_adjust : register(c3);
float green_adjust : register(c4);
float blue_adjust : register(c5);

static const float max_gamma = 100;

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 color = tex2D(implicitInput, uv);
    float4 result;

    // Apply greyscale desaturation
    float gray = color.r * 0.3 + color.g * 0.59 + color.b *0.11; 
    result.r = (color.r - gray) * saturation + gray;
    result.g = (color.g - gray) * saturation + gray;
    result.b = (color.b - gray) * saturation + gray;

    // Apply Gamma Adjustment (if it's not approximately 0.5 - which means no adjustment)
    float gammafactor = gamma == 0 ? max_gamma : log(gamma) / log(0.5);
    result.r = pow(result.r, gammafactor);
    result.g = pow(result.g, gammafactor);
    result.b = pow(result.b, gammafactor);

    //Apply linear brightness adjustment
    result.r += brightness + red_adjust;
    result.g += brightness + green_adjust;
    result.b += brightness + blue_adjust;

    //Clamp brightness adjustment result to bounds 0 <= val <= 1
    result.r = (result.r > 1 ? 1 : (result.r < 0 ? 0 : result.r));
    result.g = (result.g > 1 ? 1 : (result.g < 0 ? 0 : result.g));
    result.b = (result.b > 1 ? 1 : (result.b < 0 ? 0 : result.b));

    result.a = color.a;
    return result;
}

第 2 步 - 我必须下载 DirectX SDK 的本地副本这样我就可以将上面的 HLSL 代码编译成一个 PS 文件,这就是 WPF 使用的文件 - 给我 ColourAdjust.ps

> > fxc.exe /T ps_2_0 /E PS /ColourAdjust.ps ColourAdjust.fx

第 3 步 - 编写一个 ShaderEffect 类,该类将通过 DependencyProperties 公开效果参数。这是ColourAdjustEffect.cs:

using System;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;

namespace WPF.Utilities.UI
{
    public class ColourAdjustEffect : ShaderEffect
    {
        private static PixelShader _pixelShader = new PixelShader() { UriSource = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly() + ";component/Effects/ColourAdjust.ps") };
        public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(ColourAdjustEffect), 0);
        public static readonly DependencyProperty SaturationProperty = DependencyProperty.Register("Saturation", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(1.0, PixelShaderConstantCallback(0), CoerceFactor));
        public static readonly DependencyProperty GammaProperty = DependencyProperty.Register("Gamma", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.5, PixelShaderConstantCallback(1), CoerceFactor));
        public static readonly DependencyProperty BrightnessAdjustmentProperty = DependencyProperty.Register("BrightnessAdjustment", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(2), CoerceBrightnessAdjustment));
        public static readonly DependencyProperty RedAdjustmentProperty = DependencyProperty.Register("RedAdjustment", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(3), CoerceBrightnessAdjustment));
        public static readonly DependencyProperty GreenAdjustmentProperty = DependencyProperty.Register("GreenAdjustment", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(4), CoerceBrightnessAdjustment));
        public static readonly DependencyProperty BlueAdjustmentProperty = DependencyProperty.Register("BlueAdjustment", typeof(double), typeof(ColourAdjustEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(5), CoerceBrightnessAdjustment));

        public ColourAdjustEffect()
        {
            PixelShader = _pixelShader;

            UpdateShaderValue(InputProperty);
            UpdateShaderValue(SaturationProperty);
            UpdateShaderValue(GammaProperty);
            UpdateShaderValue(BrightnessAdjustmentProperty);
            UpdateShaderValue(RedAdjustmentProperty);
            UpdateShaderValue(GreenAdjustmentProperty);
            UpdateShaderValue(BlueAdjustmentProperty);
        }

        public Brush Input
        {
            get { return (Brush)GetValue(InputProperty); }
            set { SetValue(InputProperty, value); }
        }

        /// <summary>A value between 0 and 1 to alter the amount of colour left in the image. 0 is entirely greyscale, and 1 is unaffected. Default is 1.</summary>
        public double Saturation
        {
            get { return (double)GetValue(SaturationProperty); }
            set { SetValue(SaturationProperty, value); }
        }

        /// <summary>A value between 0 and 1 to alter the lightness of the greyscale without altering true black or true white. 
        /// 0 shifts shades closer to true black, and 1 shifts shades closer to true white. Default is 0.5.</summary>
        public double Gamma
        {
            get { return (double)GetValue(GammaProperty); }
            set { SetValue(GammaProperty, value); }
        }

        /// <summary>A value between -1 and 1 to linearly move the end result closer to true black or true white respectively.
        /// -1 will result in an entirely black image, +1 will result in an entirely white image. Default is 0.</summary>
        public double BrightnessAdjustment
        {
            get { return (double)GetValue(BrightnessAdjustmentProperty); }
            set { SetValue(BrightnessAdjustmentProperty, value); }
        }

        /// <summary>A value between -1 and 1 to linearly increase the Red component of the result.
        /// -1 will remove all Red from the image, +1 will maximize all Red in the image. Default is 0.</summary>
        public double RedAdjustment
        {
            get { return (double)GetValue(RedAdjustmentProperty); }
            set { SetValue(RedAdjustmentProperty, value); }
        }
        /// <summary>A value between -1 and 1 to linearly increase the Green component of the result.
        /// -1 will remove all Green from the image, +1 will maximize all Green in the image. Default is 0.</summary>
        public double GreenAdjustment
        {
            get { return (double)GetValue(GreenAdjustmentProperty); }
            set { SetValue(GreenAdjustmentProperty, value); }
        }
        /// <summary>A value between -1 and 1 to linearly increase the Blue component of the result.
        /// -1 will remove all Blue from the image, +1 will maximize all Blue in the image. Default is 0.</summary>
        public double BlueAdjustment
        {
            get { return (double)GetValue(BlueAdjustmentProperty); }
            set { SetValue(BlueAdjustmentProperty, value); }
        }

        private static object CoerceFactor(DependencyObject d, object value)
        {
            double newFactor = (double)value;

            if( newFactor < 0.0 ) return 0.0;
            if( newFactor > 1.0 ) return 1.0;
            return newFactor;
        }

        private static object CoerceBrightnessAdjustment(DependencyObject d, object value)
        {
            double newFactor = (double)value;

            if( newFactor < -1.0 ) return -1.0;
            if( newFactor > 1.0 ) return 1.0;
            return newFactor;
        }
    }
}

第 4 步:在 xaml 中使用您的效果:

<Setter Property="Effect">
    <Setter.Value>
        <ui:ColourAdjustEffect Saturation="0" Gamma="0.6" 
                               BrightnessAdjustment="-0.2" RedAdjustment="0.04" />
    </Setter.Value>
</Setter>

因此,虽然我没有获得发光效果,但我有足够的参数可以使用,我可以获得“突出显示”的视觉提示,这是我真正的目标。以下是我可以用它做的一些事情:

enter image description here

关于WPF 内发光效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317496/

相关文章:

c# - 后台 worker 不工作 WPF

c# - 依赖属性绑定(bind)未更新

c# - C# 中的二进制序列化(实际上是所见即所得序列化)

javascript - 从 Jquery 或 JavaScript 调用时的 WCF 身份验证方法?

c# - 一键式应用程序,并检测新版本的首次启动

c# - 在 C# 中转换泛型接口(interface)和类

wpf - 标题未显示在 WPF ListView 中

.net - 当 Collection 数据源更新时更新 ListView

wpf - 当 DataGrid 绑定(bind)到 DataTable 时,如何使用 MVVM 在 WPF 中选择 DataGrid 行

unit-testing - 运行单元测试时出现奇怪的 .net 4.0 异常