zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    剑指offer 第十一天
    Java Arrays工具类的使用
    什么是指数加权平均、偏差修正?
    深度学习——优化器算法Optimizer详解(BGD、SGD、MBGD、Momentum、NAG、Adagrad、Adadelta、RMSprop、Adam)
    深度学习——卷积神经网络 的经典网络(LeNet-5、AlexNet、ZFNet、VGG-16、GoogLeNet、ResNet)
    深度学习论文汇总
    剑指offer 第十天
    Java String 类
    Java StringBuffer和StringBuilder类
    剑指offer 第九天
  • 原文地址:https://www.cnblogs.com/wgscd/p/15225824.html
Copyright © 2011-2022 走看看