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可以实现些什么内容. 我不建议初学者做太多动画的工作,但合适的动画可以引导用户视线,提升用户体 ...
随机推荐
- VMware使用Ubuntu20.04时发生屏幕闪烁
问题:VMware使用Ubuntu20.04时发生屏幕闪烁 分析:这是由于虚拟机里面的显示器不支持3D加速问题导致的 解决方法:关闭虚拟机 → 左上角菜单 → 虚拟机 → 设置 → 显示器 → 取消勾 ...
- Nuxt.js 应用中的 vite:configResolved 事件钩子
title: Nuxt.js 应用中的 vite:configResolved 事件钩子 date: 2024/11/17 updated: 2024/11/17 author: cmdragon e ...
- 【一步步开发AI运动小程序】九、姿态辅助调试桌面工具的使用
随着人工智能技术的不断发展,阿里体育等IT大厂,推出的"乐动力"."天天跳绳"AI运动APP,让云上运动会.线上运动会.健身打卡.AI体育指导等概念空前火热.那 ...
- C# 高效餐饮管理系统设计与实现
前言 推荐一个C#开发全面.高效的商用餐饮管理系统.该系统集成了餐饮业日常运营所需的各种功能,包括但不限于订单管理.库存控制.财务结算等,通过信息技术手段,帮助餐饮企业实现管理的自动化和智能化. 系统 ...
- 使用YARP来实现负载均衡
YARP ("Yet Another Reverse Proxy") 是一个库,可帮助创建高性能.生产就绪且高度可自定义的反向代理服务器. YARP 是使用 ASP.NET 和 . ...
- python开发包之远程隧道链接sshtunnel
缘起: 公司很多的数据库的链接都是本地连接或者指定ip地址可以访问, 如果你没有该ip权限, 但是你可以登录该数据库所在的服务器, 这个时候就可以使用ssh链接上这个服务器,以此为跳板进行数据库的链接 ...
- Windows之子系统WSL
[安装] 安装参考:https://learn.microsoft.com/zh-cn/windows/wsl/install-manual#step-4---download-the-linux-k ...
- 一款开源、免费、美观的 Avalonia UI 原生控件库 - Semi Avalonia
前言 最近发现DotNetGuide技术社区交流群有不少小伙伴在学习Avalonia,今天大姚给大家分享一款开源.免费.美观的 Avalonia UI 原生控件库:Semi Avalonia. Ava ...
- 2.3k Star!强得不像开源的问卷调研平台
产品:咱们的新功能上线了,得问问用户的意见,做个调研问卷吧! 运营:对啊,用户意见很重要,我们要认真听取反馈! 领导:问卷别搞得像考试.我们要的是真实的声音,而不是让用户头疼的题目. 程序员:收到! ...
- AtCoder Beginner Contest 378
Contest Link 还得加练. A & B & C & D 不具备任何思维含量. Submission A Submission B Submission C Submi ...