循环显示图片的两种方法
方法一,用Timer控件
public partial class Form1 : Form
{
static int picIndex = 0;
static IList<string> picList = new List<string>();
static System.Windows.Forms.Timer picTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitPicList();
picTimer.Tick += new EventHandler(picTimer_Tick);
等待0.5秒
picTimer.Interval = 500;
picTimer.Start();
Application.DoEvents(); //慎重使用此语句
}
public void InitPicList()
{
picList.Add(@"D:\szf\tupian\001.jpg");
picList.Add(@"D:\szf\tupian\002.jpg");
picList.Add(@"D:\szf\tupian\003.jpg");
picList.Add(@"D:\szf\tupian\004.jpg");
picList.Add(@"D:\szf\tupian\005.jpg");
picList.Add(@"D:\szf\tupian\006.jpg");
}
private void picTimer_Tick(object sender, EventArgs e)
{
picIndex++;
if (picIndex > (picList.Count - 1))
{
picIndex = 0;
}
string picLocation = picList[picIndex];
pictureBox1.Load(picLocation);
}
}
方法二,用自己编写的延时函数 Sleep(int mm);
private void button1_Click(object sender, EventArgs e)
{
string folderpath =@"D:\szf\tupian\"; //图片文件夹
string p1 = "";
for (int i = 1; i <= 7; i++)
{
p1=folderpath + "00" + i.ToString() + ".jpg";
pictureBox1.ImageLocation = p1.Trim();
Sleep(500) ;
}
p1 = folderpath + "001.jpg";
pictureBox1.ImageLocation = p1.Trim();
MessageBox.Show ("结束了");
}
private void Sleep(int mm)
{
DateTime current = DateTime.Now;
while (current.AddMilliseconds(mm) > DateTime.Now)
{
Application.DoEvents();
}
return;
}