说明:(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();
}
}
}
}