0

我的 WPF 应用程序中有一个显示图像的控件。它使用ShaderEffect我从网上找到的代码编写的。这是ShaderEffect课程:

public class BrightContrastEffect : ShaderEffect {

    public static readonly DependencyProperty BrightnessProperty = 
        DependencyProperty.Register( "Brightness", typeof( double ), typeof( BrightContrastEffect ), 
                                     new UIPropertyMetadata( 0.0, PixelShaderConstantCallback( 0 ) ) );

    public static readonly DependencyProperty ContrastProperty = 
        DependencyProperty.Register( "Contrast", typeof( double ), typeof( BrightContrastEffect ), 
                                     new UIPropertyMetadata( 0.0, PixelShaderConstantCallback( 1 ) ) );

    public static readonly DependencyProperty InputProperty = 
        ShaderEffect.RegisterPixelShaderSamplerProperty( "Input", typeof( BrightContrastEffect ), 0 );

    public float Brightness {
        get { return (float) GetValue( BrightnessProperty ); }
        set { SetValue( BrightnessProperty, value ); }
    }

    public float Contrast {
        get { return (float) GetValue( ContrastProperty ); }
        set { SetValue( ContrastProperty, value ); }
    }

    public Brush Input {
        get { return (Brush) GetValue( InputProperty ); }
        set { SetValue( InputProperty, value ); }
    }

    public BrightContrastEffect() {
        PixelShader = m_shader;
        UpdateShaderValue( InputProperty );
        UpdateShaderValue( BrightnessProperty );
        UpdateShaderValue( ContrastProperty );

    }

    private static PixelShader m_shader = new PixelShader() { 
        UriSource = new Uri( @"pack://application:,,,/CustomControls;component/bricon.ps" ) 
    };

}

这是使用 DirectX SDK 编译的 PixelShader 的代码:

sampler2D input : register(s0);
float brightness : register(c0);
float contrast : register(c1);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 color = tex2D(input, uv); 
    float4 result = color;
    result = color + brightness;
    result = result * (1.0 + contrast) / 1.0;

    return result;
}

我的程序中有一个显示Image控件的表单,该控件的Effect属性设置为上述类的实例。图像旁边有 4 个按钮,其中一个以固定步长增加亮度,另一个以相同固定步长降低亮度,第三个类似地增加控制,第四个减少对比度。

我的问题是这段代码不仅改变了对比度,它还改变了图像的不透明度,因此图像后面的任何东西都开始通过它显示出来。它背后的东西不仅仅是白色背景。

我该如何解决这个问题,这样它就不会改变图像的不透明度?PixelShader我对所写的语言一无所知。

托尼

编辑:这是使用此 ShaderEffect 的控件的 XAML。

<Grid Background="{Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type cs:Channel}}}" 
      MouseLeftButtonDown="MouseLeftButtonDownHandler" 
      MouseLeftButtonUp="MouseLeftButtonUpHandler"
      MouseMove="MouseMoveHandler"
      Name="ChannelGrid">

    <Canvas Name="ChannelCanvas">
        <Rectangle Canvas.Left="0"
                   Canvas.Top="0"
                   Fill="Black"
                   Height="{Binding ElementName=ChannelCanvas, Path=ActualHeight}"
                   Name="MapHider"
                   Width="{Binding ElementName=ChannelCanvas, Path=ActualWidth}" />

        <Image Canvas.Left="0" 
               Canvas.Top="0"
               Height="{Binding ElementName=ChannelCanvas, Path=ActualHeight}" 
               Name="CanvasImage" 
               Stretch="Fill" 
               Width="{Binding ElementName=ChannelCanvas, Path=ActualWidth}">
            <Image.Effect>
                <cs:BrightContrastEffect Brightness="{Binding Mode=TwoWay, Path=Brightness, RelativeSource={RelativeSource AncestorType={x:Type cs:Channel}}}"
                                         Contrast="{Binding Mode=TwoWay, Path=Contrast, RelativeSource={RelativeSource AncestorType={x:Type cs:Channel}}}" />
            </Image.Effect>
        </Image>
        <Rectangle Fill="Transparent" 
                   Name="LicensePlateRectangle"
                   Stroke="Red" 
                   StrokeThickness="1"
                   Visibility="Hidden" />
    </Canvas>
    <Canvas>
        <!-- This canvas is overlaid over the previous canvas and is used to place 
             the rectangle that implements the drag selection box. -->
        <Rectangle Name="ZoomRectangle" 
                   Stroke="White" 
                   StrokeThickness="1" 
                   StrokeDashArray="5 5" 
                   StrokeDashCap="Square" 
                   Visibility="Hidden" />
    </Canvas>
    <ComboBox Background="Transparent"
              BorderBrush="{DynamicResource ComboBoxBorder}"
              FontSize="18" 
              Foreground="{DynamicResource ComboBoxForeground}" 
              HorizontalAlignment="Center" 
              Margin="0,5,0,0" 
              MinHeight="25" 
              Name="CameraPicker" 
              Panel.ZIndex="1" 
              SelectionChanged="Picker_SelectionChanged"
              VerticalAlignment="Top" 
              Visibility="Hidden" />
    <Border BorderBrush="{DynamicResource ControlBorder}"
            BorderThickness="1"
            HorizontalAlignment="Center" 
            Margin="0,5,0,0" 
            MinHeight="35" 
            MinWidth="35"
            Name="NameBorder"
            Padding="5,0"
            Panel.ZIndex="1"
            VerticalAlignment="Top"
            Visibility="Hidden">
        <cs:OutlinedText Fill="White" 
                         FontSize="18" 
                         FontWeight="Bold"
                         HorizontalAlignment="Center" 
                         MinHeight="25" 
                         MinWidth="25"
                         x:Name="CameraName" 
                         Stroke="Black" 
                         StrokeThickness="1"
                         VerticalAlignment="Center" 
                         Visibility="Hidden" />
    </Border>
</Grid>
4

1 回答 1

1

更改您的像素着色器以始终返回 alpha = 1

修改这一行

    return result;

    return float4(result.r, result.g, result.b, 1.0);

不确定确切的语法,但你明白了。:)

于 2012-07-25T20:07:50.180 回答