网站制作价格,商城网站具体需求,免费行情网站推荐,网站建设 职责灰白色很多时候用于纪念#xff0c;哀悼等。那么使用 WPF如何来做到这种效果呢#xff1f;要实现的这种效果#xff0c;我们会发现#xff0c;它其实不仅仅是要针对图片#xff0c;而是要针对整个窗口来实现灰白色。
如果只是针对图片的话#xff0c;我可以可以对图片进…灰白色很多时候用于纪念哀悼等。那么使用 WPF如何来做到这种效果呢要实现的这种效果我们会发现它其实不仅仅是要针对图片而是要针对整个窗口来实现灰白色。
如果只是针对图片的话我可以可以对图片进行灰阶转换即可达到灰色效果。
以下是图片转灰阶的代码当然方法不仅仅是这一种
//……省略…… FormatConvertedBitmap grayImage new FormatConvertedBitmap();grayImage.BeginInit();grayImage.Source originImage as BitmapSource;grayImage.DestinationFormat PixelFormats.Gray32Float;grayImage.EndInit(); //……省略……
上述方法也可以直接在 xaml里面表示。 Image.Source FormatConvertedBitmap DestinationFormatGray32 FormatConvertedBitmap.Source BitmapImage UriSourceImages\1.png / /FormatConvertedBitmap.Source /FormatConvertedBitmap /Image.Source 如果我们想针对整个窗口应用这种灰色的效果那我们可以用着色器特效 ShaderEffect来实现。
说明 *.fx格式文件是一种应用特效效果文件也叫渲染管线的配置文件。编写该文件语言名为高阶着色器语言High Level Shader Language简称HLSL由微软拥有及开发的一种语言。 *.ps格式文件是通过 *.fx文件编译出来的文件该文件是通过 ShaderEffect所指定的原始文件。通过 ShaderEffect类将其应用于 WPF。
灰阶相关 fx, ps等文件已有大佬写好了我们直接拿来用即可跳转下载https://github.com/abursjoo/GrayscaleEffectSample/tree/master/WpfApplication1/WpfApplication1。相关解释会在注释中说明。
渲染管线配置文件*.fx
//sampler2D 2D纹理采样器//s0中s表示取样器0是表示取样器的子组件编号sampler2D implicitInput : register(s0);//c 表示缓冲区偏移量float factor : register(c0);//TEXCOORD 表示纹理坐标COLOR 漫反射/镜面反射颜色float4 main(float2 uv : TEXCOORD) : COLOR{ //返回纹理implicitInput在uv位置的颜色。 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;}
应用WPF中特效类 //灰阶特效public class GrayscaleEffect : ShaderEffect{//将像素着色器映射到灰阶着色器private static PixelShader _pixelShader new PixelShader() { UriSource new Uri(pack://application:,,,/GrayscaleEffect;component/GrayscaleEffect.ps) };public GrayscaleEffect(){PixelShader _pixelShader;//更新着色器的值UpdateShaderValue(InputProperty);//该属性可以不要该熟悉用于彩色到灰色过渡效果UpdateShaderValue(DesaturationFactorProperty);}public static readonly DependencyProperty InputProperty ShaderEffect.RegisterPixelShaderSamplerProperty(Input, typeof(GrayscaleEffect), 0);public Brush Input{get { return (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 { return (double)GetValue(DesaturationFactorProperty); }set { SetValue(DesaturationFactorProperty, value); }}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;}}
在 WPF里面的使用示例:
!--effect 代表GrayscaleEffect所在的命名空间--Grid.Effecteffect:GrayscaleEffect//Grid.Effect
需要应用到哪个根元素上就将该特效给对应元素的 Effect 赋值即可。
这种实现方法性能还是很不错的如果想对 Shader有更深入的认识可以阅读 《DirectX3DHLSL高级实例精讲》.