c#编写循环播放文件夹下图片代码

如题所述

参考一下。。。

public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
//存放所有图片路径
List<string> picPathList = new List<string>();
protected override void OnLoad(EventArgs e)
{
//获取指定文件夹的所有文件
string[] paths = Directory.GetFiles(@"D:\photo\Camera");
foreach (var item in paths)
{
//获取文件后缀名
string extension = Path.GetExtension(item).ToLower();
if (extension == ".jpg" || extension == ".png" || extension == ".gif" || extension == ".bmp")
{
picPathList.Add(item);//添加到图片list中
}
}
if (picPathList.Count > 0) //如有图片
timer1.Start();
base.OnLoad(e);
}
private int picIndex = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (picIndex == picPathList.Count) //如果到了最后一个图片,从头开始
picIndex = 0;
this.pictureBox1.Image = Image.FromFile(picPathList[picIndex]);
picIndex++;
}
}

////////////////////////////////////////////////////////////////////////////////////////////
partial class Form4
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(13, 25);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(259, 208);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// timer1
//
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form4
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.pictureBox1);
this.Name = "Form4";
this.Text = "Form4";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Timer timer1;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-04-21
思路:获取一个文件夹下所有的图片文件
用一个计时器控制显示这些图片在一个图片框中
相似回答