如何对整个 WPF 应用程序进行灰度

 如何对整个 WPF 应用程序进行灰度

控件名:GrayscaleEffect

作   者:WPFDevelopersOrg - 驚鏵

原文链接[1]:https://github.com/WPFDevelopersOrg/WPFDevelopers 简易源码[2]

  • 框架使用.NET40

  • Visual Studio 2019;

  • 如果要实现灰度第一反是使用主题色更改,但是使用主题色需要重新配色比较慢,需 Effect 类派生以实现自定义位图效果,将使用ShaderEffect[3]进行更改灰度,从 ShaderEffect 类派生,以基于单个像素着色器实现自定义效果。

  • 必须安装 .NET Framework 3.5 sp1 或更高版本才能正常工作。

71698aca108f3c74c0b09f642112dfa2.png
  • 需要安装DirectX SDK才能编译像素着色器效果。

    • PixelShader[4]从预编译的高级着色语言 (HLSL) 字节代码加载 。

    • 定义表示效果参数和基于表面输入的 Brush 依赖属性。使用其中 RegisterPixelShaderSamplerProperty[5] 一个重载将这些输入与 HLSL 字节码中引用的寄存器号相关联。

    • DirectX SDK[6]

    • 下载完成DirectX SDK安装完成。

bcf72e086ea454fa87bbca534a3c2cb8.png
  • 到安装目录下\Utilities\bin\x86执行以下命令,就会输出.ps文件。

  • Nuget 最新[7]Install-Package WPFDevelopers 1.0.9.5-preview

  • Nuget 最新[8]Install-Package WPFDevelopers.Minimal 1.0.0.1-preview

  • 推荐使用Shazzam Shader Editor[9]进行编辑。

1) GrayscaleEffect.hlsl 代码如下:

sampler2D implicitInput : register(s0);
float factor : register(c0);/// <summary>The brightness offset.</summary>
/// <minValue>-1</minValue>
/// <maxValue>1</maxValue>
/// <defaultValue>0</defaultValue>
float brightness : register(c1);float4 main(float2 uv : TEXCOORD) : COLOR
{float4 pixelColor = tex2D(implicitInput, uv);pixelColor.rgb /= pixelColor.a;// Apply brightness.pixelColor.rgb += brightness;// Return final pixel color.pixelColor.rgb *= pixelColor.a;float4 color = pixelColor;float pr = .299;float pg = .587;float pb = .114;float gray = sqrt(color.r * color.r * pr + color.g * color.g * pg + color.b * color.b * pb);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;
}

2)执行命令 代码如下:

fxc /T ps_2_0 /Fo E:\GrayscaleEffect\GrayscaleEffect.ps E:\GrayscaleEffect\GrayscaleEffect.hlsl
fa9c4019d7f3636bf5bce94296ed3ea9.png

3)得到以下文件cf660677836ad7bde68b7ce3b4d3ca88.png

4)新建GrayscaleEffect.cs 代码如下:

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;namespace WPFDevelopers
{public class GrayscaleEffect : ShaderEffect{/// <summary>/// Identifies the Input property./// </summary>public static readonly DependencyProperty InputProperty = RegisterPixelShaderSamplerProperty("Input", typeof(GrayscaleEffect), 0);/// <summary>/// Identifies the Factor property./// </summary>public static readonly DependencyProperty FactorProperty = DependencyProperty.Register("Factor", typeof(double), typeof(GrayscaleEffect), new UIPropertyMetadata(0D, PixelShaderConstantCallback(0)));/// <summary>/// Identifies the Brightness property./// </summary>public static readonly DependencyProperty BrightnessProperty = DependencyProperty.Register("Brightness", typeof(double), typeof(GrayscaleEffect), new UIPropertyMetadata(0D, PixelShaderConstantCallback(1)));/// <summary>/// Creates a new instance of the <see cref="GrayscaleEffect"/> class./// </summary>public GrayscaleEffect(){var pixelShader = new PixelShader();pixelShader.UriSource = new Uri("WPFDevelopers;component/Effects/GrayscaleEffect.ps", UriKind.Relative);PixelShader = pixelShader;UpdateShaderValue(InputProperty);UpdateShaderValue(FactorProperty);UpdateShaderValue(BrightnessProperty);}/// <summary>/// Gets or sets the <see cref="Brush"/> used as input for the shader./// </summary>public Brush Input{get => ((Brush)(GetValue(InputProperty)));set => SetValue(InputProperty, value);}/// <summary>/// Gets or sets the factor used in the shader./// </summary>public double Factor{get => ((double)(GetValue(FactorProperty)));set => SetValue(FactorProperty, value);}/// <summary>/// Gets or sets the brightness of the effect./// </summary>public double Brightness{get => ((double)(GetValue(BrightnessProperty)));set => SetValue(BrightnessProperty, value);}}
}

5)使用Window.Xaml 代码如下,默认原色:

<wpfdev:Window x:Class="WPFDevelopers.Samples.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"><wpfdev:Window.Effect><wpfdev:GrayscaleEffect x:Name="grayscaleEffect" Factor="1"/></wpfdev:Window.Effect><TextBlock Text="开启程序灰度" FontSize="20" Margin="0,20,0,0"/><ToggleButton Margin="10" Name="tbGrayscale" Checked="tbGrayscale_Checked"Unchecked="tbGrayscale_Unchecked"HorizontalAlignment="Left"/>

6)使用Window.Xaml.cs 灰度代码如下:

private void tbGrayscale_Checked(object sender, RoutedEventArgs e){Create(0);}void Create(double to){var sineEase = new SineEase() { EasingMode = EasingMode.EaseOut };var doubleAnimation = new DoubleAnimation{To = to,Duration = TimeSpan.FromMilliseconds(1000),EasingFunction = sineEase};grayscaleEffect.BeginAnimation(GrayscaleEffect.FactorProperty, doubleAnimation);}private void tbGrayscale_Unchecked(object sender, RoutedEventArgs e){Create(1);}
63eefac988eae2382733e095a0c7ddbe.gif

参考资料

[1]

原文链接: https://github.com/WPFDevelopersOrg/WPFDevelopers

[2]

简易源码: https://github.com/yanjinhuagood/WPFApplicationGrayscale

[3]

ShaderEffect: https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.media.effects.shadereffect?view=windowsdesktop-7.0

[4]

PixelShader: https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.media.effects.pixelshader?view=windowsdesktop-7.0

[5]

RegisterPixelShaderSamplerProperty: https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.media.effects.shadereffect.registerpixelshadersamplerproperty?view=windowsdesktop-7.0

[6]

DirectX SDK: https://www.microsoft.com/zh-cn/download/details.aspx?id=6812

[7]

Nuget : https://www.nuget.org/packages/WPFDevelopers/

[8]

Nuget : https://www.nuget.org/packages/WPFDevelopers.Minimal/

[9]

Shazzam Shader Editor: https://github.com/JohanLarsson/Shazzam

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/280541.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

django19:项目开发流程

参考&#xff1a;https://www.bilibili.com/video/BV1QE41147hU?p831&spm_id_frompageDriver

React Native - FlexBox弹性盒模型

FlexBox布局 1. 什么是FlexBox布局? 弹性盒模型(The Flexible Box Module),又叫FlexBox,意为"弹性布局",旨在通过弹性的方式来对齐和分布容器中内容的空间,使其能适应不同屏幕,为盒模型提供最大的灵活性.   Flex布局主要思想是: 让容器有能力让其子项目能够改变其…

java虚拟机读写其他进程的数据

在java中&#xff0c;process类提供了如下3个方法&#xff0c;用于让程序和其他子进程进行通信。 InputStream getErrorStream&#xff08;&#xff09;&#xff1a;获取子进程的错误流。 InputStream getInputStream&#xff08;&#xff09;&#xff1a;获取子进程的输入流。…

release8_如何在Windows 8 Release Preview中将Chrome用作Metro浏览器

release8Windows 8 allows third-party browser to replace Internet Explorer in the Metro environment — except on Windows RT. You can use Google Chrome in Metro today, and Firefox for Metro is on the way. Windows 8允许第三方浏览器在Metro环境中替换Internet Ex…

html jQuery/bootstrap通过网络bootcdn导入连接

网络连接网址 https://www.bootcdn.cn/ <!DOCTYPE html> <html lang"zh-CN"><head><meta charset"utf-8"><title>title</title><!-- Bootstrap --><link href"https://cdn.bootcdn.net/ajax/libs/twi…

Python深入类和对象

一. 鸭子类型和多态 1.什么是鸭子类型&#xff1a; 在程序设计中&#xff0c;鸭子类型&#xff08;英语&#xff1a;Duck typing&#xff09;是动态类型和某些静态语言的一种对象推断风格。"鸭子类型"像多态一样工作&#xff0c;但是没有继承。“鸭子类型”的语言是这…

linux中/usr下文件权限修改setuid导致的问题

2019独角兽企业重金招聘Python工程师标准>>> 在Ubuntu系统中因为一些原因我使用如下命令修改了/usr目录的拥有者权限&#xff1a; chown -R root:root /usr结果直接导致系统无法正常启动&#xff0c;通过跟踪系统启动日志/var/log/syslog找到如下失败原因&#xff1…

[转载]unix环境高级编程备忘:理解保存的设置用户ID,设置用户ID位,有效用户ID,实际用户ID...

转载自http://www.cnblogs.com/stemon/p/5287631.html 一、基本概念 实际用户ID(RUID)&#xff1a;用于标识一个系统中用户是谁&#xff0c;一般是在登录之后&#xff0c;就被唯一的确定&#xff0c;就是登录的用户的uid。 有效用户ID(EUID)&#xff1a;用于系统决定用户对系统…

django20:BBS网页设计/注册功能/验证码代码

表设计 注册功能 """ 1.注册功能需要forms组件 不同功能&#xff0c;可单独一个py文件2.利用forms组件渲染前端标签1.利用ajax提交2.forms组件获取用户数据的数据。$(#form).serializeArray()获取forms标签所有用户普通键值对的数据3. 手动渲染头像label里面内…

用最少的代码打造一个Mini版的gRPC框架

在《用最少的代码模拟gRPC四种消息交换模式》中&#xff0c;我使用很简单的代码模拟了gRPC四种消息交换模式&#xff08;Unary、Client Streaming、Server Streaming和Duplex Streaming&#xff09;&#xff0c;现在我们更近一步&#xff0c;试着使用极简的方式打造一个gRPC框架…

Windows 10的下一个更新将在您观看视频时隐藏通知

Windows 10’s Focus Assist feature temporarily hides incoming notifications. In Windows 10’s next update, Focus Assist can activate when you’re using any full-screen app, whether that’s YouTube in a browser, Netflix, or a desktop video player like VLC. …

Ubuntu安装Samba文件共享服务器(NAS)

终于有点时间来解决下家中NAS需求了。一般自制NAS&#xff0c;只有选Samba。速度比FTP快&#xff0c;便利性比Windows文件夹共享好&#xff0c;设置多等等。 ▶参考&#xff1a;samba简介 安装Samba $ sudo apt-get update $ sudo apt-get install samba samba-common-bin 核心…

刚毕业的ERP实施顾问做甲方

我刚毕业进入了一家小公司做ERP实施顾问&#xff0c;是一个台湾的ERP软件&#xff0c;就简单培训了一天&#xff0c;第二天就进入一家客户公司解决问题&#xff0c;软件都还没有熟悉呢&#xff0c;更别说业务流程了&#xff0c;一天下来&#xff0c;人家员工问一个问题我记下来…

django21:admin后台管理\media配置\图片防盗链\暴露后端资源\路由分发\时间分类

admin后台管理 创建超级用户 createsuperuser 1.到应用下的admin.py注册模型表 from django.contrib import admin from blog import models # Register your models here.admin.site.register(models.UserInfo) admin.site.register(models.Article) admin.site.register(m…

Flask博客开发——Tinymce编辑器

之前Flask博客的文本编辑器比较简陋&#xff0c;这里为博客添加个优雅易用的Tinymce文本编辑器。 github见&#xff1a;https://github.com/ikheu/my_flasky 1 项目中添加Tinymce 下载好Tinymce包以及语言包&#xff0c;并添加到项目中。添加到项目的方法&#xff0c;参考了这篇…

Go开发Struct转换成map两种方式比较

最近做Go开发的时候接触到了一个新的orm第三方框架gorose&#xff0c;在使用的过程中&#xff0c;发现没有类似beego进行直接对struct结构进行操作的方法&#xff0c;有部分API是通过map进行数据库相关操作&#xff0c;那么就需要我们把struct转化成map&#xff0c;下面是是我尝…

Hello, Raspberry Pi.

1.概要最近在研究自动升级开源项目的时候偶然想到IoT领域的自动升级&#xff0c;突然想起2016年买的树莓派&#xff08;Raspberry Pi&#xff09;。那就分享一下如何入门树莓派的教程&#xff0c;我当时一共买了两块一款是Raspberry Pi 3b&#xff08;2016年价格259元去年以抽奖…

supersu_SuperSU已从Play商店中删除,这是替代使用的方法

supersuSuperSU has long been a staple in the rooted Android community. For years, the process for getting a rooted handset was: unlock the bootloader, flash a custom recovery, install SuperSU. That’s just how it was. 长期以来&#xff0c;SuperSU一直是扎根于…

Oracle 11g DRCP连接方式——基本原理

学习Oracle是一个复杂、繁琐的过程。在浩如烟海的Oracle官方资料、新特性、MOS资料和各种Internal知识面前&#xff0c;我们总是觉得力不从心、不知所措。但是&#xff0c;这往往也就是我们不断坚持、积累和追寻的乐趣。  在Oracle 11g中&#xff0c;提出了突破传统专用/共享…

django项目开发1:搭建虚拟环境

需求 不同项目依赖不同模块版本&#xff0c;不能共用一套环境&#xff0c;虚拟环境。在系统的python环境安装 安装 pip3 install virtualenv pip3 install virtualenvwrapper-win环境变量 # 配置环境变量&#xff1a; # 控制面板 > 系统和安全 > 系统 > 高级系统设…