Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF内发光效应_Wpf_.net 4.0 - Fatal编程技术网

WPF内发光效应

WPF内发光效应,wpf,.net-4.0,Wpf,.net 4.0,有人知道如何在WPF中制作内部辉光效果,而不使用expression blend或不推荐使用的BitmapEffects吗 示例图像: 例如,这里有一些xaml用于带有图像和文本的按钮。我希望此按钮具有内部辉光(而不是外部辉光): 也许您可以尝试类似的方法(注意!这不是一个完整的解决方案,只是一个想法): 我们可以通过在网格中的某些内容周围使用边框来产生“发光”效果。我觉得它当然不会像InnerGlow位图效果那样灵活,但无论如何它已经过时了 也许你可以试试这样的方法(注意!这不是一个完整

有人知道如何在WPF中制作内部辉光效果,而不使用expression blend或不推荐使用的BitmapEffects吗

示例图像:

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


也许您可以尝试类似的方法(注意!这不是一个完整的解决方案,只是一个想法):



我们可以通过在网格中的某些内容周围使用边框来产生“发光”效果。我觉得它当然不会像InnerGlow位图效果那样灵活,但无论如何它已经过时了

也许你可以试试这样的方法(注意!这不是一个完整的解决方案,只是一个想法):



我们可以通过在网格中的某些内容周围使用边框来产生“发光”效果。我觉得它当然不会像InnerGlow位图效果那样灵活,但无论如何它已经过时了

虽然上面的简化示例通过PompolutZ的回答得到了解决,但在我的真实示例中,我无法覆盖我想要应用样式的控件的控件模板,因此我开始按照说明定义自己的效果

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

colorAdjust.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;
}
步骤3-编写一个ShaderEffect类,该类将通过DependencyProperties公开效果参数。下面是
色彩调整效果.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;
        }
    }
}
使用系统;
运用系统反思;
使用System.Windows;
使用System.Windows.Media;
使用System.Windows.Media.Effects;
命名空间WPF.Utilities.UI
{
公共类颜色调整效果:ShaderEffect
{
私有静态PixelShader _PixelShader=新PixelShader(){UriSource=新Uri(“pack://application:,,,/“+Assembly.getExecutionGassembly()+”;组件/Effects/colorAdjust.ps”)};
public static readonly dependencProperty InputProperty=ShaderEffect.RegisterPixelShaderSamplerProperty(“输入”,typeof(colorAdjustEffect),0);
public static readonly dependencProperty SaturationProperty=dependencProperty.Register(“饱和度”、typeof(双精度)、typeof(colorAdjustEffect)、新UIPropertyMetadata(1.0、PixelShaderConstantCallback(0)、强制因子));
public static readonly dependencProperty GammaProperty=dependencProperty.Register(“Gamma”,typeof(double),typeof(colorAdjustEffect),新UIPropertyMetadata(0.5,PixelShaderConstantCallback(1),强制因子));
公共静态只读DependencyProperty BrightnessAdjustmentProperty=DependencyProperty.Register(“BrightnessAdjustment”、typeof(double)、typeof(ColorAdjustEffect)、new UIPropertyMetadata(0.0、PixelShaderConstantCallback(2)、强制亮度调整));
public static readonly dependencProperty RedAdjustmentProperty=dependencProperty.Register(“RedAdjustment”、typeof(double)、typeof(colorAdjustEffect)、新UIPropertyMetadata(0.0、PixelShaderConstantCallback(3)、强制亮度调整));
公共静态只读DependencyProperty GreenAdjustmentProperty=DependencyProperty.Register(“GreenAdjustment”、typeof(double)、typeof(ColorAdjustEffect)、new UIPropertyMetadata(0.0,PixelShaderConstantCallback(
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;
}
> > fxc.exe /T ps_2_0 /E PS /ColourAdjust.ps ColourAdjust.fx
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;
        }
    }
}
<Setter Property="Effect">
    <Setter.Value>
        <ui:ColourAdjustEffect Saturation="0" Gamma="0.6" 
                               BrightnessAdjustment="-0.2" RedAdjustment="0.04" />
    </Setter.Value>
</Setter>