wpf图片切换,幻灯效果

xaml:

<Window x:Class="WpfApplication1.PicShow"
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:WpfApplication1"
mc:Ignorable="d"
Title="PicShow" Height="300" Width="300" WindowState="Maximized" ResizeMode="NoResize" WindowStyle="None" >
<Grid KeyDown="Grid_KeyDown"> <Image x:Name="image" HorizontalAlignment="Left" VerticalAlignment="Top" MouseUp="image_MouseUp" Stretch="UniformToFill" KeyDown="image_KeyDown" > </Image>
<Image x:Name="image1" HorizontalAlignment="Left" VerticalAlignment="Top" MouseUp="image_MouseUp" Stretch="UniformToFill" KeyDown="image_KeyDown" />
</Grid>
</Window>

后台代码

using Sound;
using System;
using System.Collections.Generic;
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.Imaging;
using System.Windows.Shapes;
using System.Timers;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows.Media.Animation;
using System.Configuration; namespace WpfApplication1
{
/// <summary>
/// Interaction logic for PicShow.xaml
/// </summary>
public partial class PicShow : Window
{
clsMCI mp = new clsMCI();
Timer timer1 = new Timer();
int nownum = ;
ObservableCollection<BitmapImage> bmList;
int index = ; //记录索引
bool isRendering = false;
int speed = ;
string[] filesounds;
public PicShow()
{
InitializeComponent(); InitList(); CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync();
initsound();
} void bw_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
isRendering = true;
System.Threading.Thread.Sleep(speed*); //停1秒
}
} public void InitList()
{
bmList = new ObservableCollection<BitmapImage>();
string imgpath = AppDomain.CurrentDomain.BaseDirectory + "\\img";
string mp3path = AppDomain.CurrentDomain.BaseDirectory + "\\mp3";
string[] files = Directory.GetFiles(imgpath);
foreach (var str in files)
{
BitmapImage bmImg = new BitmapImage(new Uri(str));
bmList.Add(bmImg);
} filesounds= Directory.GetFiles(mp3path); string spd = ConfigurationSettings.AppSettings["speed"];
if (!int.TryParse(spd, out speed))
{
speed = ;
} } void CompositionTarget_Rendering(object sender, EventArgs e)
{
if (isRendering)
{
if (index < bmList.Count)
{
this.image.Source = bmList[index];
this.image1.Width = this.image.Width;
this.image1.Height = this.image.Height;
this.image1.Source = bmList[index+<bmList.Count?index+ :];
img();
index++;
}
else
{
index = ;
}
isRendering = false;
}
} private void img()
{
Random r = new Random();
int i = r.Next();
Thickness tfrom = new Thickness();
Thickness tto = new Thickness();
if (i == )
{
tfrom = new Thickness(this.Width, , , );
this.image1.Margin = tfrom;
tto = new Thickness(, , , );
}
if (i ==)
{
tfrom = new Thickness(, this.Width, , );
this.image1.Margin = tfrom;
tto = new Thickness(, , , );
}
if (i == )
{
tfrom = new Thickness(, , this.Width, );
this.image1.Margin = tfrom;
tto = new Thickness(, , , );
}
if (i == )
{
tfrom = new Thickness(, , , this.Width);
this.image1.Margin = tfrom;
tto = new Thickness(, , , );
}
ThicknessAnimation ta = new ThicknessAnimation();
ta.From = tfrom;// new Thickness(left,top,right,bottom); //起始值
ta.To = tto;// new Thickness(0, 0, 0, 0); //结束值
ta.Duration = TimeSpan.FromSeconds(); //动画持续时间
this.image1 .BeginAnimation(TextBlock.MarginProperty, ta);//开始动画
} private void initsound()
{
try
{
mp.Puase();
mp.StopT();
timer1.Stop();
timer1 = new Timer();
mp = new clsMCI();
mp.FileName = filesounds[nownum];
mp.play();
int m = mp.Duration;
timer1.Interval = m * ;
timer1.Elapsed += Timer1_Elapsed;
timer1.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.Message);
}
} private void Timer1_Elapsed(object sender, ElapsedEventArgs e)
{
if (nownum == filesounds.Length - )
{
nownum = ;
}
else
{
nownum=nownum+;
}
initsound();
} private void image_MouseUp(object sender, MouseButtonEventArgs e)
{
if (this.WindowState == WindowState.Normal) {
this.WindowState = WindowState.Maximized;
this.WindowStyle = WindowStyle.None;
}
else {
this.WindowState = WindowState.Normal;
this.WindowStyle = WindowStyle.SingleBorderWindow;
} } private void Grid_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
this.Close();
}
} private void image_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
this.Close();
}
}
}
}

mp3播放类

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO; namespace Sound
{
/// <summary>
 /// clsMci 的摘要说明。
 /// </summary>
public class clsMCI
{
public clsMCI()
{
//
// TODO: 在此处添加构造函数逻辑
//
} //定义API函数使用的字符串变量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
private string Name = "";
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
private string durLength = "";
[MarshalAs(UnmanagedType.LPTStr, SizeConst = )]
private string TemStr = "";
int ilong;
//定义播放状态枚举变量
public enum State
{
mPlaying = ,
mPuase = ,
mStop =
};
//结构变量
public struct structMCI
{
public bool bMut;
public int iDur;
public int iPos;
public int iVol;
public int iBal;
public string iName;
public State state;
}; public structMCI mc = new structMCI(); //取得播放文件属性
public string FileName
{
get
{
return mc.iName;
}
set
{
//ASCIIEncoding asc = new ASCIIEncoding();
try
{
TemStr = "";
TemStr = TemStr.PadLeft(, Convert.ToChar(" "));
Name = Name.PadLeft(, Convert.ToChar(" "));
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);
//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
Name = "open " + Convert.ToChar() + Name + Convert.ToChar() + " alias media";
ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, );
ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, );
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, );
mc.state = State.mStop;
}
catch
{
// MessageBox.Show("出错错误!");
}
}
}
//播放
public void play()
{
TemStr = "";
TemStr = TemStr.PadLeft(, Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length, );
mc.state = State.mPlaying;
}
//停止
public void StopT()
{
TemStr = "";
TemStr = TemStr.PadLeft(, Convert.ToChar(" "));
ilong = APIClass.mciSendString("close media", TemStr, , );
ilong = APIClass.mciSendString("close all", TemStr, , );
mc.state = State.mStop;
} public void Puase()
{
TemStr = "";
TemStr = TemStr.PadLeft(, Convert.ToChar(" "));
ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, );
mc.state = State.mPuase;
}
private string GetCurrPath(string name)
{
if (name.Length < ) return "";
name = name.Trim();
name = name.Substring(, name.Length - );
return name;
}
//总时间
public int Duration
{
get
{
durLength = "";
durLength = durLength.PadLeft(, Convert.ToChar(" "));
APIClass.mciSendString("status media length", durLength, durLength.Length, );
durLength = durLength.Trim();
if (durLength == "") return ;
return (int)(Convert.ToDouble(durLength) / 1000f);
}
} //当前时间
public int CurrentPosition
{
get
{
durLength = "";
durLength = durLength.PadLeft(, Convert.ToChar(" "));
APIClass.mciSendString("status media position", durLength, durLength.Length, );
mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
return mc.iPos;
}
}
} public class APIClass
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
); [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}
上一篇:VBS基础篇 - 循环


下一篇:【BZOJ】1833: [ZJOI2010] count 数字计数(数位dp)