첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
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
그리드형(광고전용)
Posted by icodebroker
,