如何對整個 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.pswp.cn/news/280541.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280541.shtml
英文地址,請注明出處:http://en.pswp.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; # 控制面板 > 系統和安全 > 系統 > 高級系統設…