WPF WriteableBitmap et effets
-
06-07-2019 - |
Question
J'essaie d'utiliser la classe WPF WriteableBitmap pour permettre à mon application d'appliquer un masque d'opacité à une image.
En gros, j'ai un rectangle bleu en tant qu'image et une autre image rectangle vert transparent à 100% au-dessus de la bleue.
Lorsque l'utilisateur déplace sa souris sur l'image verte (transparente), je souhaite appliquer le masque d'opacité (éventuellement à l'aide d'une simple ellipse) afin qu'il ressemble à une lueur verte.
Je ne fais pas cela à dessein, c’est le XAML et les effets WPF standard, car j’en ai vraiment besoin pour être super performant et je vais éventuellement échanger l’ellipse avec un blob plus avancé ...
Avez-vous des idées?
Merci!
La solution
Je suis désolé, je ne comprends pas très bien vos intentions. Peut-être que si je pouvais voir l'image, je pourrais répondre correctement dès le début, mais voici ma première réponse, peut-être fausse.
Si vous dites super performant, vous voudrez probablement regarder les pixel shaders. Ils sont traités par G PU, pris en charge par WPF sous la forme d'un effet personnalisé et sont faciles à mettre en œuvre. Vous pouvez également appliquer des shaders à la lecture de vidéos alors que c'est difficile à faire avec WritableBitmap.
Pour écrire un pixel shader, vous devez disposer de FX Compiler (fxc.exe) à partir de Kit de développement DirectX et outil Shazzam - Compilateur WYSIWYG WPF Shaders de Walt Ritscher.
Lorsque vous obtenez les deux, continuez et essayez le code HLSL suivant
float X : register(C0); // Mouse cursor X position
float Y : register(C1); // Mouse cursor Y position
float4 Color : register(C2); // Mask color
float R : register(C3); // Sensitive circle radius.
sampler2D implicitInputSampler : register(S0);
float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 finalColor = tex2D(implicitInputSampler, uv);
if ( (uv.x - X) * (uv.x - X) + (uv.y - Y) * (uv.y - Y) < R*R)
{
finalColor = Color; // Blend/Change/Mask it as you wish here.
}
return finalColor;
}
Cela vous donne l'effet C # suivant:
namespace Shazzam.Shaders {
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
public class AutoGenShaderEffect : ShaderEffect {
public static DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(AutoGenShaderEffect), 0);
public static DependencyProperty XProperty = DependencyProperty.Register("X", typeof(double), typeof(AutoGenShaderEffect), new System.Windows.UIPropertyMetadata(new double(), PixelShaderConstantCallback(0)));
public static DependencyProperty YProperty = DependencyProperty.Register("Y", typeof(double), typeof(AutoGenShaderEffect), new System.Windows.UIPropertyMetadata(new double(), PixelShaderConstantCallback(1)));
public static DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(System.Windows.Media.Color), typeof(AutoGenShaderEffect), new System.Windows.UIPropertyMetadata(new System.Windows.Media.Color(), PixelShaderConstantCallback(2)));
public static DependencyProperty RProperty = DependencyProperty.Register("R", typeof(double), typeof(AutoGenShaderEffect), new System.Windows.UIPropertyMetadata(new double(), PixelShaderConstantCallback(3)));
public AutoGenShaderEffect(PixelShader shader) {
// Note: for your project you must decide how to use the generated ShaderEffect class (Choose A or B below).
// A: Comment out the following line if you are not passing in the shader and remove the shader parameter from the constructor
PixelShader = shader;
// B: Uncomment the following two lines - which load the *.ps file
// Uri u = new Uri(@"pack://application:,,,/glow.ps");
// PixelShader = new PixelShader() { UriSource = u };
// Must initialize each DependencyProperty that's affliated with a shader register
// Ensures the shader initializes to the proper default value.
this.UpdateShaderValue(InputProperty);
this.UpdateShaderValue(XProperty);
this.UpdateShaderValue(YProperty);
this.UpdateShaderValue(ColorProperty);
this.UpdateShaderValue(RProperty);
}
public virtual System.Windows.Media.Brush Input {
get {
return ((System.Windows.Media.Brush)(GetValue(InputProperty)));
}
set {
SetValue(InputProperty, value);
}
}
public virtual double X {
get {
return ((double)(GetValue(XProperty)));
}
set {
SetValue(XProperty, value);
}
}
public virtual double Y {
get {
return ((double)(GetValue(YProperty)));
}
set {
SetValue(YProperty, value);
}
}
public virtual System.Windows.Media.Color Color {
get {
return ((System.Windows.Media.Color)(GetValue(ColorProperty)));
}
set {
SetValue(ColorProperty, value);
}
}
public virtual double R {
get {
return ((double)(GetValue(RProperty)));
}
set {
SetValue(RProperty, value);
}
}
}
}
Vous pouvez maintenant suivre la position de la souris et définir les propriétés correspondantes de votre effet pour déclencher des modifications. Une chose à noter ici: X et Y dans le code HLSL vont de 0 à 1. Il vous faudra donc convertir les coordonnées réelles en pourcentages avant de les passer au shader.
Ce qu'il faut savoir sur les pixel shaders et WPF:
- Référence pour HLSL sur MSDN.
- Écriture basée sur un GPU personnalisé Effets pour WPF de Greg Schechter.
J'espère que cela vous aidera:)