• WPF Play Image slider animation using Storyboard


    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>
    

      

    有的电脑会出现诡异现象:Sb_Completed 不触发!。。。。。。。。。不是不得骑姐。

    fffffffffffffffff
    test red font.
  • 相关阅读:
    CSS
    javaScript的DOM操作
    模板Template
    Django的View(视图)
    路由系统(urls.py)
    MTV和MVC模型和创建Django项目
    rest-framework认证组件
    property classmethod staticmethod和反射
    ModelSerializer和视图
    rest-framework序列化
  • 原文地址:https://www.cnblogs.com/wgscd/p/15225824.html
Copyright © 2020-2023  润新知