来自png图像的平滑动画(视频)

时间:2018-10-19 08:27:46

标签: c# wpf bitmapimage

我不能用图像制作视频文件,因为我需要保持png文件的透明度。

我想不延迟地每秒获得25帧。

1000张图片1920x1080从8 KB到5 MB

我该如何实现?

以下是我尝试过的示例:

1个示例: 禁止图片,因为上传图片。

int counter = 0;
DispatcherTimer dT = new DispatcherTimer();

public Png()
{
   InitializeComponent();

   dT.Interval = new TimeSpan(0, 0, 0, 0, 40);
   dT.Tick += new EventHandler(dT_Tick);
   dT.Start();
}

void dT_Tick(object sender, EventArgs e)
{
   var image = new BitmapImage(new Uri("C:\\" + counter + ".png"));
   imageBox.Source = image;
   counter++;

   if (counter == 1000)
   {
     dT.Stop();
   }
}

2个示例: 由于大量文件,RAM非常堵塞。而且,也感到阻力。

    int counter = 0;
    DispatcherTimer dT = new DispatcherTimer();
    private BitmapImage[] images;

    public Png()
    {
       InitializeComponent();

       images = new BitmapImage[1000];

       for(int i = 0 ; i < images.Length; i++)
       {
          images[i] = new BitmapImage(new Uri("C:\\" + i + ".png"));
       }

       dT.Interval = new TimeSpan(0, 0, 0, 0, 40);
       dT.Tick += new EventHandler(dT_Tick);
       dT.Start();
    }



   void dT_Tick(object sender, EventArgs e)
   {
      imageBox.Source = images[counter];
      counter++;

      if (counter == 1000)
      {
        dT.Stop();
      }
   } 

3个示例:

我尝试使用Queue

Queue试图用作图片的缓冲区。 首先,我将10张图片上传到Queue。 显示第一张图片。 并且,当我显示第二张图片时,我删除了第一张图片,并将第11张图片从另一个线程添加到Queue

但是我觉得自己做错了,也感到刹车,多次更正了代码,最终不小心删除了。

2 个答案:

答案 0 :(得分:0)

我将对示例2进行修改,因为我认为它更有效,我也鼓励您对示例1进行试验。

在表单中再添加一个图像框,并确保两个图像框都具有相同的位置和大小。

void dT_Tick(object sender, EventArgs e)
{
      if (counter % 2 != 0) {
         imageBox1.Source = images[counter];
         imageBox1.Visible = true;
         imageBox2.Visible = false;
      }
      else {
         imageBox2.Source = images[counter];
         imageBox2.Visible = true;
         imageBox1.Visible = false;
      }
      counter++;

      if (counter == 1000)
      {
        dT.Stop();
      }
} 

答案 1 :(得分:-1)

//对于FrameRate,将其添加到XAML中。

<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:0.5" 
                 From="1.0" To="0.5" Timeline.DesiredFrameRate="25" />

在WPF中可以使用关键帧动画。通过为每个图像插入关键帧,在情节提要中添加所有图像。关键帧在您的时间轴中看起来像钻石形状。您可以在“对象和时间轴”中添加它们的内容。

//平滑的动画

  

                                                                            

          <!-- Animate the TranslateTransform's X property
               from 0 to 350, then 50,
               then 200 over 10 seconds. -->

          <DoubleAnimationUsingKeyFrames
            Storyboard.TargetName="MyAnimatedTranslateTransform"
            Storyboard.TargetProperty="X"
            Duration="0:0:10">
            <LinearDoubleKeyFrame Value="0" KeyTime="0:0:0" />
            <LinearDoubleKeyFrame Value="350" KeyTime="0:0:2" />
            <LinearDoubleKeyFrame Value="50" KeyTime="0:0:7" />
            <LinearDoubleKeyFrame Value="200" KeyTime="0:0:8" />                          
          </DoubleAnimationUsingKeyFrames>
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Rectangle.Triggers> 
</Rectangle>