【发布时间】:2014-05-05 10:08:37
【问题描述】:
我正在为图像使用 PixelShader 效果。
- 使用
SLIDERS调整对比度、亮度、CMY 和 RGB 的图像效果 - 混合模式图像效果预定义并加载在组合框中,用户可以选择自己的选择。
这是 Image 元素,调整对比度、亮度、CMY 和 RGB 的图像效果以这种方式应用。
<Viewbox x:Name="cImage" Stretch="Uniform" Grid.Column="1" Grid.Row="1" Grid.RowSpan="2" Margin="0" >
<Image x:Name="ViewedPhoto" Source="IMG_0071.jpg"
Stretch="None" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,95" >
<Image.Effect>
<l:BrightContrastEffect
Brightness="{Binding Value, ElementName=bVal}"
Contrast="{Binding Value, ElementName=cVal}"
Red="{Binding Value, ElementName=rVal}"
Green="{Binding Value, ElementName=gVal}"
Blue="{Binding Value, ElementName=blVal}"
/>
</Image.Effect>
</Image>
</Viewbox>
亮度滑块:
<Slider Maximum="1" Minimum="-1" x:Name="bVal" TickFrequency="1" TickPlacement="BottomRight" />
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Value, ElementName=bVal, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right" Width="30" Height="10" Margin="0" RenderTransformOrigin="-1.167,0.423" Visibility="Hidden"/>
</StackPanel>
当我移动用于调整对比度、亮度、CMY 和 RGB 的滑块时,it's working。
我使用组合框应用预定义的混合模式图像效果。
<ComboBox x:Name="cColorEffects" SelectionChanged="cColorEffects_SelectionChanged" />
为了显示不同的效果,我首先调用ViewedPhoto.Effect = null;,然后应用选定的效果。
if (cColorEffects.SelectedIndex == 0)
{
ViewedPhoto.Effect = null;
ViewedPhoto.Effect = new AverageEffect();
}
if (cColorEffects.SelectedIndex == 1)
{
ViewedPhoto.Effect = null;
ViewedPhoto.Effect = new ColorBurnEffect();
}
问题:
我正在通过组合框调用另一个图像效果,因此用于调整对比度、亮度、CMY 和 RGB 的图像效果不起作用。
我正在使用
ViewedPhoto.Effect = null;调整对比度、亮度、CMY 和 RGB 的图像效果不起作用。
我想制作the sliders working for adjusting 对比度、亮度、CMY 和RGB 以及apply the blend mode image effects simultaneously。我怎样才能解决这个问题?或者给我一个解决这个问题的想法?
编辑:
我一直在想我可以让滑块以编程方式绑定和图像效果。是否有意义?如果好的话;我该如何应用这个?
<Image x:Name="ViewedPhoto" >
<Image.Effect>
<l:BrightContrastEffect
Brightness="{Binding Value, ElementName=bVal}"
Contrast="{Binding Value, ElementName=cVal}"
Red="{Binding Value, ElementName=rVal}"
Green="{Binding Value, ElementName=gVal}"
Blue="{Binding Value, ElementName=blVal}"
/>
</Image.Effect>
</Image>
<Slider Maximum="1" Minimum="-1" x:Name="bVal" TickFrequency="1" TickPlacement="BottomRight" />
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Value, ElementName=bVal, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden"/>
<Button x:Name="bReset" Content="R" Height="10" Width="30" Margin="35,0,0,0" Click="bReset_Click"/>
</StackPanel>
<Slider Maximum="1" Minimum="-1" x:Name="cVal" TickFrequency="1" TickPlacement="BottomRight" />
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Value, ElementName=cVal, UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden" />
<Button x:Name="cReset" Content="R" Height="10" Width="30" Margin="35,0,0,0" Click="cReset_Click"/>
</StackPanel>
带有代码的其他信息:
public class BlendModeEffect : ShaderEffect
{
public BlendModeEffect()
{
UpdateShaderValue(InputProperty);
UpdateShaderValue(TextureProperty);
}
public Brush Input
{
get { return (Brush)GetValue(InputProperty); }
set { SetValue(InputProperty, value); }
}
public static readonly DependencyProperty InputProperty =
ShaderEffect.RegisterPixelShaderSamplerProperty
(
"Input",
typeof(BlendModeEffect),
0
);
public Brush Texture
{
get { return (Brush)GetValue(TextureProperty); }
set { SetValue(TextureProperty, value); }
}
public static readonly DependencyProperty TextureProperty =
ShaderEffect.RegisterPixelShaderSamplerProperty
(
"Texture",
typeof(BlendModeEffect),
1
);
}
//Contrast, Brightness, CMY & RGB Effect
public class BrightContrastEffect : ShaderEffect
{
private static PixelShader m_shader =
new PixelShader() { UriSource = MakePackUri("bricon.ps") };
public BrightContrastEffect()
{
PixelShader = m_shader;
UpdateShaderValue(InputProperty);
UpdateShaderValue(BrightnessProperty);
UpdateShaderValue(ContrastProperty);
UpdateShaderValue(RedProperty);
UpdateShaderValue(GreenProperty);
UpdateShaderValue(BlueProperty);
}
// MakePackUri is a utility method for computing a pack uri
// for the given resource.
public static Uri MakePackUri(string relativeFile)
{
Assembly a = typeof(BrightContrastEffect).Assembly;
// Extract the short name.
string assemblyShortName = a.ToString().Split(',')[0];
string uriString = "pack://application:,,,/" +
assemblyShortName +
";component/" +
relativeFile;
return new Uri(uriString);
}
public Brush Input
{
get { return (Brush)GetValue(InputProperty); }
set { SetValue(InputProperty, value); }
}
public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(BrightContrastEffect), 0);
public float Brightness
{
get { return (float)GetValue(BrightnessProperty); }
set { SetValue(BrightnessProperty, value); }
}
public static readonly DependencyProperty BrightnessProperty = DependencyProperty.Register("Brightness", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(0)));
public float Contrast
{
get { return (float)GetValue(ContrastProperty); }
set { SetValue(ContrastProperty, value); }
}
public static readonly DependencyProperty ContrastProperty = DependencyProperty.Register("Contrast", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(1)));
public float Red
{
get { return (float)GetValue(RedProperty); }
set { SetValue(RedProperty, value); }
}
public static readonly DependencyProperty RedProperty = DependencyProperty.Register("Red", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(2)));
public float Green
{
get { return (float)GetValue(GreenProperty); }
set { SetValue(RedProperty, value); }
}
public static readonly DependencyProperty GreenProperty = DependencyProperty.Register("Green", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(3)));
public float Blue
{
get { return (float)GetValue(BlueProperty); }
set { SetValue(BlueProperty, value); }
}
public static readonly DependencyProperty BlueProperty = DependencyProperty.Register("Blue", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(4)));
//private static PixelShader m_shader = new PixelShader() { UriSource = new Uri(@"pack://application:,,,/CustomPixelRender;component/bricon.ps") };
}
//Average Blend Mode Effect
public class AverageEffect : BlendModeEffect
{
public static Uri MakePackUri(string relativeFile)
{
Assembly a = typeof(ColorBurnEffect).Assembly;
// Extract the short name.
string assemblyShortName = a.ToString().Split(',')[0];
string uriString = "pack://application:,,,/" +
assemblyShortName +
";component/" +
relativeFile;
return new Uri(uriString);
}
static AverageEffect()
{
_pixelShader.UriSource = MakePackUri("AverageEffect.ps");
}
public AverageEffect()
{
this.PixelShader = _pixelShader;
}
private static PixelShader _pixelShader = new PixelShader();
}
//ColorBurn Effect
public class ColorDodgeEffect : BlendModeEffect
{
public static Uri MakePackUri(string relativeFile)
{
Assembly a = typeof(ColorBurnEffect).Assembly;
// Extract the short name.
string assemblyShortName = a.ToString().Split(',')[0];
string uriString = "pack://application:,,,/" +
assemblyShortName +
";component/" +
relativeFile;
return new Uri(uriString);
}
static ColorDodgeEffect()
{
_pixelShader.UriSource = MakePackUri("ColorDodgeEffect.ps");
}
public ColorDodgeEffect()
{
this.PixelShader = _pixelShader;
}
private static PixelShader _pixelShader = new PixelShader();
}
//BlendModeEffect
public class BlendModeEffect : ShaderEffect
{
public BlendModeEffect()
{
UpdateShaderValue(InputProperty);
UpdateShaderValue(TextureProperty);
}
public Brush Input
{
get { return (Brush)GetValue(InputProperty); }
set { SetValue(InputProperty, value); }
}
public static readonly DependencyProperty InputProperty =
ShaderEffect.RegisterPixelShaderSamplerProperty
(
"Input",
typeof(BlendModeEffect),
0
);
public Brush Texture
{
get { return (Brush)GetValue(TextureProperty); }
set { SetValue(TextureProperty, value); }
}
public static readonly DependencyProperty TextureProperty =
ShaderEffect.RegisterPixelShaderSamplerProperty
(
"Texture",
typeof(BlendModeEffect),
1
);
}
【问题讨论】:
标签: wpf image slider shader pixel-shader