WPF supports Alpha channels. What else do we need to specify the color transparency? If it is a newly written system, it is unnecessary. However, if the old system is transplanted to the WPF platform, and the previous images are transparent based on pure green, and do not want to take the time to modify them? The simplest method is to handle the problem of Specified Color transparency at the WPF layer.
One idea is to use converter to replace the specified color in the image with a transparent color. However, if the image size is large or the image size is large, the CPU usage will be heavy. Therefore, this kind of pixel-level task should be handed over to the GPU by using a custom effect.
There are already some ready-made effect, but it is better to write an effect step by step for the purpose of learning. I mainly refer to Microsoft's GPU-based effects topic.
Because the logic of this maskcoloreffect is quite simple, it is to compare and replace the color value in one pixel and one pixel. Therefore, it does not involve HLSL writing and other advanced beginners. It's just a reference to the Hello world level step. I hope I can give some feelings to people who have never written a custom effect. (This is my first contact)
A custom effect consists of two parts. One is the effect class written by C #, and the other is the HLSL class that implements the true logic of effect.Code. The effect class willProgramThe obtained image and parameter information are transmitted to HLSL for processing. Describes how HLSL code is applied to the effect class.
After the principles are completed, let's take a look at the running effect.
The words of transparent are red. After this effect is added, the alpha channel value becomes 0, so it becomes transparent.
The HLSL code is simple and can be understood even if it is not written.
Sampler2d implicitinput: Register (S0 );
Float4 mask: Register (C0 );
Float4 main (float2 UV: texcoord): Color
{
Float4 color = tex2d (implicitinput, UV );
If (mask. r = color. R & amp; mask. G = color. G & amp; mask. B = color. B)
{
Color. A = 0;
}
Return color;
}
Actually, I cannot fully understand it. For example, is the UV variable of the main function float2 or texcoord type ?, But looking at the online examples, modifying the function body can still be done.This file must be saved as an ANSI text file. Otherwise, fxc (in DirectX SDK) reports an error saying that the main function cannot be found.
Then there is the effect class. The Code is as follows.
Namespace Effectlibrary { Using System; Using System. windows;Using System. Windows. Media; Using System. Windows. Media. effects; Public Class Maskcoloreffect: shadereffect { Public Static Readonly Dependencyproperty inputproperty = shadereffect. registerpixelshadersamplerproperty ( "Input" , Typeof (Maskcoloreffect), 0 ); Public Static Readonly Dependencyproperty colorproperty = dependencyproperty. Register ("Color" , Typeof (Color ), Typeof (Maskcoloreffect ), New Uipropertymetadata (colors. Transparent, pixelshaderconstantcallback (0 ))); Public Maskcoloreffect () {pixelshader = New Pixelshader () {urisource = New Uri ( "/Effectlibrary; component/maskcolorshader. Ps" , Urikind. relativeorabsolute)}; updateshadervalue (inputproperty); updateshadervalue (colorproperty );} Public Brush input {get {Return Getvalue (inputproperty) As Brush;} set {setvalue (inputproperty, Value );}} Public Color color {get { Return (Color) getvalue (colorproperty);} set {setvalue (colorproperty, Value );}}}}
The Code is also very simple, that is, defining several DP parameters and then applying PS in the constructor. Complete code can be downloaded from here. If you want to change the HLSL code, you need to install the DirectX SDK and compile HLSL again.