當前位置:首頁 > IT技術 > Windows編程 > 正文

WPF Play Image slider animation using Storyboard
2021-09-04 10:35:05

?

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            imgSlider = new Image() { Stretch = Stretch.UniformToFill };
            rootGrid.Children.Add(imgSlider);
            LoadMedias();

            sb = new Storyboard()
            {
                Duration = TimeSpan.FromSeconds(3)
            };

            DoubleAnimation t = new DoubleAnimation()
            {
                From = 0,
                To = 1,
                Duration = TimeSpan.FromSeconds(1)
            };
            
            Storyboard.SetTarget(t,imgSlider);
            Storyboard.SetTargetProperty(t, new PropertyPath("Opacity"));
            sb.Children.Add(t);
            sb.Completed += Sb_Completed;
            Loaded += MainWindow_Loaded;
        }
        int mediaIndex = 0;
        List<string> mediaList = new List<string>();
        Storyboard sb;
        Image imgSlider;
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Play();
        }


        private void Sb_Completed(object sender, EventArgs e)
        {
            Debug.Print("Sb_Completed");
            Play();
        }
        private void Play()
        {

            if (mediaIndex >= mediaList.Count)
            {
                mediaIndex = 0;
            }
            sb.Stop();
            var url = mediaList[mediaIndex];
            BitmapImage bmp = new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute));
            imgSlider.Source = bmp;
            sb.Begin();
            mediaIndex++;

        }


        private void LoadMedias()
        {
            var files = Directory.GetFiles(@"d:PicturesCryptoArtCollection", "*.jpg").Take(5);
            mediaList.AddRange(files);

        }



    }
}

  

?

視頻和圖片混播:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            player.MediaEnded += Player_MediaEnded;
            imgSlider = new Image() { Stretch = Stretch.UniformToFill };
            rootGrid.Children.Add(imgSlider);
            LoadMedias();

            sb = new Storyboard()
            {
                Duration = TimeSpan.FromSeconds(3)
            };

            DoubleAnimation t = new DoubleAnimation()
            {
                From = 0,
                To = 1,
                Duration = TimeSpan.FromSeconds(1)
            };
            
            Storyboard.SetTarget(t,imgSlider);
            Storyboard.SetTargetProperty(t, new PropertyPath("Opacity"));
            sb.Children.Add(t);
            sb.Completed += Sb_Completed;
            Loaded += MainWindow_Loaded;
        }

        private void Player_MediaEnded(object sender, RoutedEventArgs e)
        {
            Play();
        }

        int mediaIndex = 0;
        List<string> mediaList = new List<string>();
        Storyboard sb;
        Image imgSlider;
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Play();
        }


        private void Sb_Completed(object sender, EventArgs e)
        {
            Debug.Print("Sb_Completed");
            Play();
        }
        private void Play()
        {

            if (mediaIndex >= mediaList.Count)
            {
                mediaIndex = 0;
            }
            sb.Stop();
            var url = mediaList[mediaIndex];
            if (url.EndsWith(".jpg"))
            {
                PlayImage(url);
            }
            else {
                sb.Stop();
                imgSlider.Visibility = Visibility.Collapsed;

                player.Visibility = Visibility.Visible;
                player.Source = new Uri(url,UriKind.RelativeOrAbsolute);
                player.Play();

            }
            mediaIndex++;

        }

        private void PlayImage(string url)
        {
            player.Stop();
            player.Visibility = Visibility.Collapsed;

            sb.Stop();
            BitmapImage bmp = new BitmapImage(new Uri(url, UriKind.RelativeOrAbsolute));
            imgSlider.Source = bmp;
            imgSlider.Visibility = Visibility.Visible;
            sb.Begin();
  
        }




        private void LoadMedias()
        {
            var files = Directory.GetFiles(@"d:PicturesTestMedia");
            mediaList.AddRange(files);


        }



    }
}

  

UI:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="1920" Width="1080">
    <Grid Name="rootGrid"  Background="BlueViolet">
        <MediaElement Name="player" LoadedBehavior="Manual" />
    </Grid>
</Window>

  

?

有的電腦會出現(xiàn)詭異現(xiàn)象:Sb_Completed 不觸發(fā)!。。。。。。。。。不是不得騎姐。

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務立即開通 >