wpf writeblitybitmap 및 효과
-
06-07-2019 - |
문제
IM은 내 응용 프로그램이 이미지에 불투명도 마스크를 적용 할 수 있도록 WPF Writ
기본적으로 나는 이미지로 파란색 사각형을 가지고 있으며 파란색 상단 위에 또 다른 100% 투명한 녹색 사각형 이미지가 있습니다.
사용자가 녹색 (투명한) 이미지 위로 마우스를 움직일 때 불투명도 마스크 (아마도 간단한 타원 사용)를 적용하여 녹색 빛이 발생하는 것처럼 보입니다.
의도적 으로이 작업을 수행하지 않는 IM은 XAML 및 표준 WPF 효과입니다. 왜냐하면 나는 정말로 슈퍼 성능이 필요하고 결국 타원을 더 발전된 블로브로 교환 할 것입니다 ...
이견있는 사람??
감사!
해결책
죄송합니다. 귀하의 의도를 잘 이해하지 못합니다. 어쩌면 이미지를 볼 수 있다면 처음부터 올바르게 대답 할 수 있지만 여기에 첫 번째 정답이 있습니다.
부적합한 말을한다면 아마도 픽셀 셰이더를보고 싶을 것입니다. 그들은 처리됩니다 GPU, WPF가 사용자 정의 효과 형태로 지원하고 구현하기 쉽습니다. 또한 셰이더를 비디오 재생에 적용 할 수 있지만 WritableBitMap과는 어렵습니다.
픽셀 셰이더를 작성하려면 FX Compiler (fxc.exe)가 있어야합니다. Directx SDK 그리고 Shazzam 도구 -Wysiwyg WPF 셰이더 컴파일러 Walt Ritscher.
둘 다 받으면 계속해서 다음 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;
}
이것은 다음 c# 효과를 제공합니다.
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);
}
}
}
}
이제 마우스 위치를 추적하고 효과의 해당 속성을 설정하여 변경 사항을 트리거 할 수 있습니다. 여기에 주목해야 할 사항 : HLSL 코드의 x와 y는 0에서 1 사이입니다. 따라서 실제 좌표를 셰이더로 전달하기 전에 실제 좌표를 백분율로 변환해야합니다.
픽셀 셰이더 및 WPF에 대한 자세한 내용 :
- HLSL에 대한 참조 MSDN에서.
- WPF에 대한 맞춤형 GPU 기반 효과 작성 Greg Schechter에 의해.
도움이 되었기를 바랍니다 :)