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:\Pictures\CryptoArt\Collection", "*.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:\Pictures\TestMedia");
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 不触发!。。。。。。。。。不是不得骑姐。
WPF Play Image slider animation using Storyboard的更多相关文章
- 示例:WPF中自定义StoryBoarService在代码中封装StoryBoard、Animation用于简化动画编写
原文:示例:WPF中自定义StoryBoarService在代码中封装StoryBoard.Animation用于简化动画编写 一.目的:通过对StoryBoard和Animation的封装来简化动画 ...
- WPF 和 UWP 中,不用设置 From 或 To,Storyboard 即拥有更灵活的动画控制
无论是 WPF 还是 UWP 开发,如果用 Storyboard 和 Animation 做动画,我们多数时候都会设置 From 和 To 属性,用于从起始值动画到目标值.然而动画并不总是可以静态地指 ...
- WPF.UIShell UIFramework之自定义窗口的深度技术 - 模态闪动(Blink)、窗口四边拖拽支持(WmNCHitTest)、自定义最大化位置和大小(WmGetMinMaxInfo)
无论是在工作和学习中使用WPF时,我们通常都会接触到CustomControl,今天我们就CustomWindow之后的一些边角技术进行探讨和剖析. 窗口(对话框)模态闪动(Blink) 自定义窗口的 ...
- WPF之小动画一
定义动画: 直接使用Element进行BeginAnimation DoubleAnimation animation = new DoubleAnimation(); animation.By = ...
- WPF中反转3D列表项
原文:WPF中反转3D列表项 WPF中反转3D列表项 周银辉记得在苹果电脑中有一个很酷的 ...
- [No000012F]WPF(7/7) - 样式,触发器和动画
WPF Tutorial : Beginning [^] WPF Tutorial : Layout-Panels-Containers & Layout Transformation [^] ...
- 实现一个 WPF 版本的 ConnectedAnimation
Windows 10 的创造者更新为开发者们带来了 Connected Animation 连接动画,这也是 Fluent Design System 的一部分.它的视觉引导性很强,用户能够在它的帮助 ...
- WPF/WP/Silverlight/Metro App代码创建动画的思路
在2010年之前,我都是用Blend创建动画,添加触发器实现自动动画,后来写成代码创建的方式.如今Blend已经集成到Visual Studio安装镜像中了,最新的VS2015安装,Blend的操作界 ...
- WPF动画的几种模式
最近在用WPF做简单动画,以下是几点经验总结: 1. 使用DispatcherTimer做动画 VB6的年代大家就用Timer做动画了,不用多解释,这个DispatcherTimer和本身的Timer ...
- [WPF自定义控件库]了解WPF的布局过程,并利用Measure为Expander添加动画
1. 前言 这篇文章介绍WPF UI元素的两步布局过程,并且通过Resizer控件介绍只使用Measure可以实现些什么内容. 我不建议初学者做太多动画的工作,但合适的动画可以引导用户视线,提升用户体 ...
随机推荐
- 我的博客网站为什么又回归Blazor了
引言 在博客网站的开发征程中,站长可谓是一路披荆斩棘.从最初的构思到实践,先后涉足了多种开发技术,包括 [MVC](ASP.NET Core MVC 概述 | Microsoft Learn).[Ra ...
- Java中序列化与反序列化
序列化(Serialization)和反序列化(Deserialization)是计算机科学中用于数据存储和传输的两种基本操作. 序列化: 序列化是将对象的状态信息转换为可以存储或传输的形式的过程.简 ...
- 使用Boost.asio与Boost.beast基于协程连接ws
目录 目录 前言 准备工作 实现 初始化io_context并监听信号 启动连接ws的线程并启动io_context 建立tcp链接(以下步骤皆位于ws函数中) ws握手 传输数据 效果 总结 前言 ...
- Python脚本消费多个Kafka topic
在Python中消费多个Kafka topic,可以使用kafka-python库,这是一个流行的Kafka客户端库.以下是一个详细的代码示例,展示如何创建一个Kafka消费者,并同时消费多个Kafk ...
- 读书笔记-C#8.0本质论-05
18.3 基于任务的异步编程模式 18.3.1 使用任务并行库(TPL)实现异步执行高延迟操作 using System; using System.Net.Http; using System.Th ...
- Tomcat并发数优化的方法总结
web应用的并发提升,除了负载均衡.在小企业中也可以通过一些软件的上的设置来进行一些优化.下面是一些在服务器上修改tomcat参数的优化方法,非常简单实用!(这些方法通过网络整理的) 1,让Tomca ...
- Java线程中断的本质和编程原则
在历史上,Java试图提供过抢占式限制中断,但问题多多,例如前文介绍的已被废弃的Thread.stop.Thread.suspend和 Thread.resume等.另一方面,出于Java应用代码的健 ...
- Java Study For Seven Day( 面向对象三)
继承 class Person { String name; int age; } class Student extends Person { void study() { System.out.p ...
- PHP之环境搭建(php7.4 + php8.1)
之前写过几次,使用lnmp,宝塔,源码编译等方式来进行PHP环境的搭建, 随着接触的越来越多, 这里做一个总结, 常用的搭建方式 1.编译安装 之前写个几次,可以参考之前的 这次记录下多个版本PHP的 ...
- redis教程(Mac)
1.首先,检查是否已经安装Homebrew,如果没有安装Homebrew,请先安装 2.使用Homebrew安装命令,在终端输入以下命令 brew install redis 当前默认安装5.0.8版 ...