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 = 0; ObservableCollection<BitmapImage> bmList; int index = 0; //记录索引 bool isRendering = false; int speed = 3; 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*1000); //停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 = 2; } } 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+1<bmList.Count?index+1 :0]; img(); index++; } else { index = 0; } isRendering = false; } } private void img() { Random r = new Random(); int i = r.Next(5); Thickness tfrom = new Thickness(); Thickness tto = new Thickness(); if (i == 1) { tfrom = new Thickness(this.Width, 0, 0, 0); this.image1.Margin = tfrom; tto = new Thickness(0, 0, 0, 0); } if (i ==2) { tfrom = new Thickness(0, this.Width, 0, 0); this.image1.Margin = tfrom; tto = new Thickness(0, 0, 0, 0); } if (i == 3) { tfrom = new Thickness(0, 0, this.Width, 0); this.image1.Margin = tfrom; tto = new Thickness(0, 0, 0, 0); } if (i == 4) { tfrom = new Thickness(0, 0, 0, this.Width); this.image1.Margin = tfrom; tto = new Thickness(0, 0, 0, 0); } 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(1); //动画持续时间 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 * 1000; 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 - 1) { nownum = 0; } else { nownum=nownum+1; } 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 = 260)] private string Name = ""; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] private string durLength = ""; [MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)] private string TemStr = ""; int ilong; //定义播放状态枚举变量 public enum State { mPlaying = 1, mPuase = 2, mStop = 3 }; //结构变量 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(127, Convert.ToChar(" ")); Name = Name.PadLeft(260, 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(34) + Name + Convert.ToChar(34) + " alias media"; ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0); ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0); ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0); mc.state = State.mStop; } catch { // MessageBox.Show("出错错误!"); } } } //播放 public void play() { TemStr = ""; TemStr = TemStr.PadLeft(127, Convert.ToChar(" ")); APIClass.mciSendString("play media", TemStr, TemStr.Length, 0); mc.state = State.mPlaying; } //停止 public void StopT() { TemStr = ""; TemStr = TemStr.PadLeft(128, Convert.ToChar(" ")); ilong = APIClass.mciSendString("close media", TemStr, 128, 0); ilong = APIClass.mciSendString("close all", TemStr, 128, 0); mc.state = State.mStop; } public void Puase() { TemStr = ""; TemStr = TemStr.PadLeft(128, Convert.ToChar(" ")); ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0); mc.state = State.mPuase; } private string GetCurrPath(string name) { if (name.Length < 1) return ""; name = name.Trim(); name = name.Substring(0, name.Length - 1); return name; } //总时间 public int Duration { get { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); APIClass.mciSendString("status media length", durLength, durLength.Length, 0); durLength = durLength.Trim(); if (durLength == "") return 0; return (int)(Convert.ToDouble(durLength) / 1000f); } } //当前时间 public int CurrentPosition { get { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); APIClass.mciSendString("status media position", durLength, durLength.Length, 0); 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 ); } }