3、具體代碼實現(xiàn):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplicationIntegratedDemo
{
public partial class Form1 : Form
{
private string _directoryInfoPath = string.Empty;//文件夾相對路徑
public string DirectoryInfoPath
{
set { _directoryInfoPath = value; }
get { return _directoryInfoPath; }
}
private int _imageCount = 0;//文件數(shù)
public int ImageCount
{
set { _imageCount = value; }
get { return _imageCount; }
}
private int _imageIndex = 1;//當前圖片的索引值
public int ImageIndex
{
set { _imageIndex = value; }
get {return _imageIndex;}
}
private bool _signExtend = false;//圖片是否放大標示
public bool SignExtend
{
set { _signExtend = value; }
get { return _signExtend; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.DirectoryInfoPath = @"../Images";
DirectoryInfo directoryInfo = new DirectoryInfo(this.DirectoryInfoPath);//創(chuàng)建文件夾對象
if (directoryInfo.Exists)
{
FileInfo[] fileInfos = directoryInfo.GetFiles("*.png");//查詢出后綴名為.png的所有文件
if(fileInfos!=null)
this.ImageCount = fileInfos.Count();
}
this.timer1.Start();//打開計數(shù)器
//this.pictureBox3.Image = this.imageList1.Images[0];//ImageList控件將大圖像的像素改變了
this.pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;//設(shè)置圖片大小
this.pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;//設(shè)置圖片大小
}
//計時器事件
private void timer1_Tick(object sender, EventArgs e)
{
this.pictureBox3.ImageLocation = GetImagePath(this.DirectoryInfoPath,this.ImageIndex);//加載的圖片的路徑
this.pictureBox4.ImageLocation = GetImagePath(this.DirectoryInfoPath, this.ImageCount + 1 - this.ImageIndex);//加載的圖片的路徑
this._imageIndex++;
this.ImageIndex = this._imageIndex;
if (this.ImageIndex == (this.ImageCount + 1))//從新開始顯示圖片
{
this._imageIndex = 1;
this.ImageIndex = 1;
}
}
/// <summary>
/// 圖片的相對路徑
/// </summary>
/// <param name="pathPrefix">路徑前綴(文件夾路徑)</param>
/// <param name="index">圖片索引值</param>
/// <returns></returns>
private string GetImagePath(string pathPrefix,int index)
{
string strIndex = string.Format("/aaa{0:000}.png", index);//將整數(shù)格式化為“/aaa000.png”樣式
string imagePath = pathPrefix + strIndex;
return imagePath;
}
//PictureBox 雙擊事件
private void pictureBox_DoubleClick(object sender, EventArgs e)
{
PictureBox pictureBox = sender as PictureBox;
if (pictureBox != null)
{
if (!this.SignExtend)
{
pictureBox.Width = 220;
pictureBox.Height = 190;
pictureBox.Location = new Point(pictureBox.Location.X + 5, pictureBox.Location.Y + 5);//左上角的位置
this.SignExtend = true;//放大縮小標示
}
else
{
pictureBox.Width = 110;
pictureBox.Height = 90;
pictureBox.Location = new Point(pictureBox.Location.X - 5, pictureBox.Location.Y - 5);//左上角的位置
this.SignExtend = false;//放大縮小標示
}
}
}
}
}