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

WPF custom effect animation programmatically

我尝试在 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);