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

10-14 06:51