使用动画播放文件夹中的图片
生活随笔
收集整理的這篇文章主要介紹了
使用动画播放文件夹中的图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
說明:(1)使用Visual Studio 2010新建一個WPF應用程序項目,
(2)然后將WPF項目名稱保存為:WpfApplication1,
(3)接著將下面的代碼復制到MainWindow.cs文件中
(4)在C:\盤根目錄下新建一個文件夾,名稱為temp
(5)將需要播放的圖片復制到該文件夾下。
(6)編譯運行程序即可。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.IO;
?
namespace WpfApplication1
{
??? /// <summary>
??? /// MainWindow.xaml 的交互邏輯
??? /// </summary>
??? public partial class MainWindow : Window
??? {
??????? Storyboard myStoryboard;
??????? Storyboard ibstoryboard;
??????? string[] files;
?
??????? Image myImage;
??????? public MainWindow()
??????? {
??????????? InitializeComponent();
??????????? GetImageFileInfo();
?
??????????? NameScope.SetNameScope(this, new NameScope());????????????
??????????? StackPanel myPanel = new StackPanel();
??????????? myPanel.Margin = new Thickness(10);
??????????? this.Title = "淡入淡出特效顯示";
??????????? this.WindowState = WindowState.Maximized;
??????????? this.WindowStyle = System.Windows.WindowStyle.None;???????????? this.Topmost = true;
??????????? myImage = new Image();
??????????? myImage.Name = "myImage";
?
??????????? this.RegisterName(myImage.Name, myImage);
??????????? myImage.Width = SystemParameters.PrimaryScreenWidth; ;
??????????? myImage.Height = SystemParameters.PrimaryScreenHeight;
?
??????????? ObjectAnimationUsingKeyFrames myObjectAnimation = new
ObjectAnimationUsingKeyFrames();
??????????? myObjectAnimation.Duration = new Duration(TimeSpan.FromSeconds(25));
??????????? for (int i = 0; i < files.Length; i++)
??????????? {
??????????????? ImageBrush ib = new ImageBrush();
??????????????? ib.ImageSource = new BitmapImage(new Uri(files[i].ToString()));
??????????????? myObjectAnimation.KeyFrames.Add(new
DiscreteObjectKeyFrame(ib.ImageSource,
KeyTime.FromTimeSpan(TimeSpan.FromSeconds(5 * i))));
??????????? }
??????????? myObjectAnimation.AutoReverse = true;
??????????? myObjectAnimation.RepeatBehavior = RepeatBehavior.Forever;
??????????? Storyboard.SetTargetName(myObjectAnimation, myImage.Name);
??????????? Storyboard.SetTargetProperty(myObjectAnimation, new
PropertyPath(Image.SourceProperty));
??????????? ibstoryboard = new Storyboard();
??????????? ibstoryboard.Children.Add(myObjectAnimation);
?
??????????? DoubleAnimation myDoubleAnimation = new DoubleAnimation();
??????????? myDoubleAnimation.From = 0.0;
??????????? myDoubleAnimation.To = 1.0;
??????????? myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
??????????? myDoubleAnimation.AutoReverse = true;
??????????? myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
?
??????????? myStoryboard = new Storyboard();
??????????? myStoryboard.Children.Add(myDoubleAnimation);
??????????? Storyboard.SetTargetName(myDoubleAnimation, myImage.Name);
??????????? Storyboard.SetTargetProperty(myDoubleAnimation, new
PropertyPath(Image.OpacityProperty));
?
??????????? // Use the Loaded event to start the Storyboard.
??????????? myImage.Loaded += new RoutedEventHandler(myImageLoaded);
?
??????????? myPanel.Children.Add(myImage);
??????????? this.Content = myPanel;???????? }
??????? private void myImageLoaded(object sender, RoutedEventArgs e)
??????? {
??????????? myStoryboard.Begin(this);
??????????? ibstoryboard.Begin(this);
??????? }
??????? private void GetImageFileInfo()
??????? {???????????????????????
??????????? string temp = @"c:\temp\";
??????????? files = Directory.GetFiles(temp, "*.jpg");
??????? }
?
??????? private void Window_KeyDown(object sender, KeyEventArgs e)
??????? {
??????????? if (e.Key == Key.Escape)
??????????? {
??????????????? MessageBoxResult yesno;
??????????????? yesno = MessageBox.Show("確定要退出程序嗎?","退出程序提示
",MessageBoxButton.YesNo);
??????????????? if(yesno == MessageBoxResult.Yes)
??????????????????? this.Close();???????????????
?
??????????? }
??????? }
?
??? }
}
?
(2)然后將WPF項目名稱保存為:WpfApplication1,
(3)接著將下面的代碼復制到MainWindow.cs文件中
(4)在C:\盤根目錄下新建一個文件夾,名稱為temp
(5)將需要播放的圖片復制到該文件夾下。
(6)編譯運行程序即可。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.IO;
?
namespace WpfApplication1
{
??? /// <summary>
??? /// MainWindow.xaml 的交互邏輯
??? /// </summary>
??? public partial class MainWindow : Window
??? {
??????? Storyboard myStoryboard;
??????? Storyboard ibstoryboard;
??????? string[] files;
?
??????? Image myImage;
??????? public MainWindow()
??????? {
??????????? InitializeComponent();
??????????? GetImageFileInfo();
?
??????????? NameScope.SetNameScope(this, new NameScope());????????????
??????????? StackPanel myPanel = new StackPanel();
??????????? myPanel.Margin = new Thickness(10);
??????????? this.Title = "淡入淡出特效顯示";
??????????? this.WindowState = WindowState.Maximized;
??????????? this.WindowStyle = System.Windows.WindowStyle.None;???????????? this.Topmost = true;
??????????? myImage = new Image();
??????????? myImage.Name = "myImage";
?
??????????? this.RegisterName(myImage.Name, myImage);
??????????? myImage.Width = SystemParameters.PrimaryScreenWidth; ;
??????????? myImage.Height = SystemParameters.PrimaryScreenHeight;
?
??????????? ObjectAnimationUsingKeyFrames myObjectAnimation = new
ObjectAnimationUsingKeyFrames();
??????????? myObjectAnimation.Duration = new Duration(TimeSpan.FromSeconds(25));
??????????? for (int i = 0; i < files.Length; i++)
??????????? {
??????????????? ImageBrush ib = new ImageBrush();
??????????????? ib.ImageSource = new BitmapImage(new Uri(files[i].ToString()));
??????????????? myObjectAnimation.KeyFrames.Add(new
DiscreteObjectKeyFrame(ib.ImageSource,
KeyTime.FromTimeSpan(TimeSpan.FromSeconds(5 * i))));
??????????? }
??????????? myObjectAnimation.AutoReverse = true;
??????????? myObjectAnimation.RepeatBehavior = RepeatBehavior.Forever;
??????????? Storyboard.SetTargetName(myObjectAnimation, myImage.Name);
??????????? Storyboard.SetTargetProperty(myObjectAnimation, new
PropertyPath(Image.SourceProperty));
??????????? ibstoryboard = new Storyboard();
??????????? ibstoryboard.Children.Add(myObjectAnimation);
?
??????????? DoubleAnimation myDoubleAnimation = new DoubleAnimation();
??????????? myDoubleAnimation.From = 0.0;
??????????? myDoubleAnimation.To = 1.0;
??????????? myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));
??????????? myDoubleAnimation.AutoReverse = true;
??????????? myDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
?
??????????? myStoryboard = new Storyboard();
??????????? myStoryboard.Children.Add(myDoubleAnimation);
??????????? Storyboard.SetTargetName(myDoubleAnimation, myImage.Name);
??????????? Storyboard.SetTargetProperty(myDoubleAnimation, new
PropertyPath(Image.OpacityProperty));
?
??????????? // Use the Loaded event to start the Storyboard.
??????????? myImage.Loaded += new RoutedEventHandler(myImageLoaded);
?
??????????? myPanel.Children.Add(myImage);
??????????? this.Content = myPanel;???????? }
??????? private void myImageLoaded(object sender, RoutedEventArgs e)
??????? {
??????????? myStoryboard.Begin(this);
??????????? ibstoryboard.Begin(this);
??????? }
??????? private void GetImageFileInfo()
??????? {???????????????????????
??????????? string temp = @"c:\temp\";
??????????? files = Directory.GetFiles(temp, "*.jpg");
??????? }
?
??????? private void Window_KeyDown(object sender, KeyEventArgs e)
??????? {
??????????? if (e.Key == Key.Escape)
??????????? {
??????????????? MessageBoxResult yesno;
??????????????? yesno = MessageBox.Show("確定要退出程序嗎?","退出程序提示
",MessageBoxButton.YesNo);
??????????????? if(yesno == MessageBoxResult.Yes)
??????????????????? this.Close();???????????????
?
??????????? }
??????? }
?
??? }
}
?
轉載于:https://www.cnblogs.com/dodui/archive/2012/03/29/2423223.html
總結
以上是生活随笔為你收集整理的使用动画播放文件夹中的图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS(截取字符串,显示当前系统时间yyy
- 下一篇: MVC命名空间中的~UrlHelper中