728x90
728x170
■ ShaderEffect 클래스를 사용해 흑백 출력 효과를 만드는 방법을 보여준다.
GrayscaleEffect.fx
0.00MB
GrayscaleEffect.ps
0.00MB
▶ 예제 코드 (C#)
using System;
using System.Windows.Media.Effects;
using System.Windows;
using System.Windows.Media;
/// <summary>
/// 회색조 효과
/// </summary>
public class GrayscaleEffect : ShaderEffect
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region Field
/// <summary>
/// 입력 속성
/// </summary>
public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty
(
"Input",
typeof(GrayscaleEffect),
0
);
/// <summary>
/// 포화도 저하 팩터 속성
/// </summary>
public static readonly DependencyProperty DesaturationFactorProperty = DependencyProperty.Register
(
"DesaturationFactor",
typeof(double),
typeof(GrayscaleEffect),
new UIPropertyMetadata
(
0.0,
PixelShaderConstantCallback(0),
CoerceDesaturationFactor
)
);
#endregion
//////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 픽셀 셰이더
/// </summary>
private static PixelShader _pixelShader = new PixelShader()
{
UriSource = new Uri(@"pack://application:,,,/HowToCustomizeTheImageEditorMenu;component/GrayscaleEffect.ps")
};
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 입력 - Input
/// <summary>
/// 입력
/// </summary>
public Brush Input
{
get
{
return (Brush)GetValue(InputProperty);
}
set
{
SetValue(InputProperty, value);
}
}
#endregion
#region 포화도 저하 팩터 - DesaturationFactor
/// <summary>
/// 포화도 저하 팩터
/// </summary>
public double DesaturationFactor
{
get
{
return (double)GetValue(DesaturationFactorProperty);
}
set
{
SetValue(DesaturationFactorProperty, value);
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - GrayscaleEffect
/// <summary>
/// 생성자
/// </summary>
public GrayscaleEffect()
{
PixelShader = _pixelShader;
UpdateShaderValue(InputProperty);
UpdateShaderValue(DesaturationFactorProperty);
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 포화도 저하 팩터 강제하기 - CoerceDesaturationFactor(dependencyObject, value)
/// <summary>
/// 포화도 저하 팩터 강제하기
/// </summary>
/// <param name="dependencyObject">이벤트 발생자</param>
/// <param name="value">이벤트 인자</param>
/// <returns>처리 결과</returns>
private static object CoerceDesaturationFactor(DependencyObject dependencyObject, object value)
{
GrayscaleEffect grayscaleEffect = (GrayscaleEffect)dependencyObject;
double newFactor = (double)value;
if(newFactor < 0d || newFactor > 1d)
{
return grayscaleEffect.DesaturationFactor;
}
return newFactor;
}
#endregion
}
※ 아래 첨부 파일을 프로젝트에 포함시킨다.
첨부 파일 | 빌드 작업 |
GrayscaleEffect.fx | 없음 |
GrayscaleEffect.ps | Resource |
728x90
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] 벡터 라디안 각도 구하기 (0) | 2015.01.01 |
---|---|
[C#/WPF] 장치 독립적 단위 수 구하기 (0) | 2015.01.01 |
[C#/WPF] 픽셀 수 구하기 (0) | 2015.01.01 |
[C#/WPF] IValueConverter 인터페이스 : 진리 값 반전 변환자 사용하기 (0) | 2014.05.08 |
[C#/WPF] RichTextBox 엘리먼트 사용하기 (0) | 2014.03.31 |
[C#/WPF] Uri 클래스 : 리소스 URI 구하기 (상대적) (0) | 2014.03.30 |
[C#/WPF] IValueConverter 인터페이스 : 정수→선형 그라디언트 브러시 변환자 사용하기 (0) | 2014.03.23 |
[C#/WPF] IValueConverter 인터페이스 : 정수↔실수 변환자 사용하기 (0) | 2014.03.18 |
[C#/WPF] Rectangle 엘리먼트 : RenderTransformOrigin 속성 사용하기 (0) | 2014.03.04 |
[C#/WPF] Image 엘리먼트 : Clip 속성 사용하기 (0) | 2014.03.04 |