winform 循环播放当前目录下的图片及视频
采用vlc Xabe.FFmpeg
插件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Xabe.FFmpeg; namespace PicShow { public partial class Form1 : Form { int rateSpeed = 3000; private List<string> videoFiles = new List<string>(); private List<string> list = new List<string>(); private string[] imageFiles; // 存储图片文件路径 private int currentIndex = 0; // 当前显示的图片索引 private Thread rotateThread; // 轮播线程 public Form1() { InitializeComponent(); } //窗体调用事件 private void Form1_Load(object sender, EventArgs e) { pictureBox1.Width = this.Width; pictureBox1.Height = this.Height; //this.DesktopBounds = Screen.AllScreens[1].Bounds; //将当前窗体显示在第二个显示器上 this.FormBorderStyle = FormBorderStyle.None; if (Screen.AllScreens.Count() > 0) { this.DesktopBounds = Screen.AllScreens[0].Bounds; } string operatorValue; try { //读取切换间隔时间 StreamReader sr = new StreamReader("config.ini", Encoding.GetEncoding("utf-8")); if (sr.Peek() >= 0) { operatorValue = sr.ReadLine(); rateSpeed = int.Parse(operatorValue); } sr.Close(); } catch (Exception ex) { } StartRotate(); timer1.Start(); } void StartRotate() { // 读取指定文件夹下的所有图片 string folderPath = Directory.GetCurrentDirectory(); ; try { rotateThread = new Thread(async () => { while (true) { imageFiles = Directory.GetFiles(folderPath, "*.*", SearchOption.TopDirectoryOnly); //获取当前目录下的图片和视频文件,保存到list 列表中; GetVideoFiles(folderPath); if (list.Count == 0) { MessageBox.Show("当前目录下没有图片、视频可显示,请添加图片后再运行!", "提示"); Application.Exit(); return; } // 显示下一张图片或视频 //currentIndex = (currentIndex + 1) % list.Count; for (int i = 0; i < list.Count; i++) { int t = list[i].Length; ; string fileFormat = list[i].Substring(list[i].Length - 3, 3); if (fileFormat == "jpg" || fileFormat == "png") { this.Invoke(new Action(() => { pictureBox1.Visible = true; vlcControl1.Visible = false; })); this.Invoke(new Action(() => { pictureBox1.Image = Image.FromFile(list[i]); })); // 等待 ***秒后切换图片 Thread.Sleep(rateSpeed); } if (fileFormat == "mp4" || fileFormat == "avi") { // 取视频总时长 IMediaInfo mediaInfo = await FFmpeg.GetMediaInfo(list[i]); var videoDuration = mediaInfo.VideoStreams.First().Duration; //TimeSpane 格式转换为double double videoLength = videoDuration.TotalSeconds * 1000; //取视频时间长度 this.Invoke(new Action(() => { pictureBox1.Visible = false; vlcControl1.Visible = true; })); vlcControl1.SetMedia(new System.IO.FileInfo(list[i])); vlcControl1.Play(); Thread.Sleep((int)videoLength); } } list.Clear(); } }); rotateThread.IsBackground = true; rotateThread.Start(); } catch (Exception ex) { MessageBox.Show("当前目录没有可显示的图片视频!\r\n" + ex.Message.ToString(), "提示"); Application.Exit(); } } private void pictureBox1_Click(object sender, EventArgs e) { } //双击图片显示窗体边框 private void pictureBox1_DoubleClick(object sender, EventArgs e) { if (this.FormBorderStyle == FormBorderStyle.None) { this.FormBorderStyle = FormBorderStyle.FixedSingle; } else { this.FormBorderStyle = FormBorderStyle.None; this.DesktopBounds = Screen.AllScreens[1].Bounds; } } #region 内存回收 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")] public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize); /// <summary> /// 释放内存 /// </summary> public static void ClearMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { //FrmMian为我窗体的类名 Form1.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } } #endregion private void timer1_Tick(object sender, EventArgs e) { ClearMemory(); } /// <summary> /// C#获取指定目录下多种指定类型文件 /// </summary> /// <param name="filePath">路径</param> private void GetVideoFiles(string filePath) { DirectoryInfo di = new DirectoryInfo(filePath); FileInfo[] afi = di.GetFiles("*.*"); string fileName; for (int i = 0; i < afi.Length; i++) { fileName = afi[i].Name.ToLower(); if (fileName.EndsWith(".jpg") || fileName.EndsWith(".png") || fileName.EndsWith(".mp4") || fileName.EndsWith(".avi")) { list.Add(fileName); } } } private void vlcControl1_VlcLibDirectoryNeeded(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e) { } private void vlcControl1_VlcLibDirectoryNeeded_1(object sender, Vlc.DotNet.Forms.VlcLibDirectoryNeededEventArgs e) { var currentAssembly = Assembly.GetEntryAssembly(); var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName; if (currentDirectory == null) { return; } if (IntPtr.Size == 4) { e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x86")); } else { e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x64")); } } private void vlcControl1_DoubleClick(object sender, EventArgs e) { if (this.FormBorderStyle == FormBorderStyle.None) { this.FormBorderStyle = FormBorderStyle.FixedSingle; } else { this.FormBorderStyle = FormBorderStyle.None; this.DesktopBounds = Screen.AllScreens[1].Bounds; } } } }
活到老,学到老。