WPF WriteableBitmap e efeitos
-
06-07-2019 - |
Pergunta
Eu estou tentando olhar para usar a classe WPF WriteableBitmap para permitir que a minha candidatura para aplicar uma máscara de opacidade a uma imagem.
Basicamente, eu tenho um retângulo azul como uma imagem, e outra imagem retângulo verde transparente 100% sobre a parte superior do azul.
Quando o usuário move o mouse sobre a imagem verde (transparente), eu quero aplicar a máscara de opacidade (talvez usando um simples elipse) para que ele se parece com um brilho verde está ocorrendo.
Im propositadamente não fazer isso é XAML e efeitos WPF padrão porque eu realmente precisa dele para ser super performance e eu, eventualmente, trocar a elipse com um mais blob antecedência ...
Todos os pensamentos ??
Obrigado!
Solução
Sinto muito, eu não entendo muito bem as suas intenções. Talvez se eu podia ver a imagem, eu poderia responder corretamente desde o início, mas aqui é o meu primeiro-talvez-errada resposta.
Se você diz super-performance, você provavelmente vai querer olhar para pixel shaders. Eles são processados ??por G PU, apoiado pelo WPF em uma forma de um efeito personalizado e são fáceis de implementar. Além disso, você pode aplicar shaders para a reprodução de vídeo, embora seja difícil de fazer com WritableBitmap.
Para escrever um pixel shader, você precisa ter FX Compiler (fxc.exe) de DirectX SDK e Shazzam ferramenta -. compilador WYSIWYG WPF Shaders por Walt Ritscher
Quando você pegá-los tanto, vá em frente e tentar o seguinte código HLSL
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;
}
Isto dá-lhe o seguinte C # efeito:
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);
}
}
}
}
Agora você pode acompanhar a posição do mouse e defina as propriedades do seu efeito às mudanças de gatilho correspondente. Uma coisa a notar aqui:. X e Y em código HLSL são variou de 0 a 1. Então você vai ter que converter as coordenadas reais para porcentagens, antes de passá-los para shader
Coisas para ler mais sobre o pixel shaders e WPF:
- Referência para HLSL no MSDN.
- baseada em GPU Writing personalizado efeitos para WPF por Greg Schechter.
Espero que isso ajude:)