c# - WPF自定义效果动画以编程方式

标签 c# wpf animation shader hlsl

我尝试在 WPF 中以编程方式使用像素着色器效果动画,但下面的代码使用了大量 CPU 资源。 还有其他方法吗?

■调用方式 边做序列边设置效果。

private void Button_Click(object sender, RoutedEventArgs e)
{
    string path = System.IO.Path.GetFullPath(@"WateryEffect.ps");
    var uri = new Uri(path);
    WpfEffect wpfeffect = new WpfEffect(uri);

    BitmapImage img1 = new BitmapImage(new Uri(System.IO.Path.GetFullPath(@"3a28168e68.jpg")));
    ImageBrush imgbrush1 = new ImageBrush(img1);
    wpfeffect.input1 = imgbrush1;

    VisualBrush vbrush = new VisualBrush(text1);
    wpfeffect.input1 = vbrush;

    BitmapImage img2 = new BitmapImage(new Uri(System.IO.Path.GetFullPath(@"e98f58fa-2e58-4d38-9949-f3f40b6a2b13_7.jpg")));
    ImageBrush imgbrush2 = new ImageBrush(img2);
    wpfeffect.input2 = imgbrush2;

    double i = 1;
    do
    {
        wpfeffect.para0 = i;

        image1.Effect = wpfeffect;
        image2.Effect = wpfeffect;
        image3.Effect = wpfeffect;
        image4.Effect = wpfeffect;
        DoEvents();
        i = i - 0.0001;

    } while (i > -1);

}

最佳答案

为此做一个循环违背了 WPF 的整体优势。您需要将此效果更新作为 DoubleAnimation 运行。我没有您的 .ps 文件,但我假设 para01 是一个 DependencyProperty。

试试这段代码:

DoubleAnimation da = 
new DoubleAnimation(0.0, 1.0, new Duration(new TimeSpan(0,0,1)); // adjust as needed

(image1.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);
(image2.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);
(image3.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);
(image4.Effect as WpfEffect).BeginAnimation(WpfEffect.para01Property, da);

关于c# - WPF自定义效果动画以编程方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32993045/

相关文章:

c# - 以设计器模式打开 .NET 表单 - 获取 "The path is not of a legal form"

c# - 获取网页的 "summary"

wpf - 强制 ComboBox 在 Silverlight 中打开?

c# - 从 XDocument 中获取后代

c# - 为什么在编译 typeModel 时某些内置类型不起作用?

wpf - 在以编程方式添加的控件上使用 Storyboard 动画

WPF奇怪的双击窗口切换

JavaFx文本移动动画

javascript - 遵循 TweenJs 中的自定义路径

javascript - 悬停时切换菜单时的jquery动画