1、PictureBox 常用属性
Location : 控件左上角相对容器左上角的坐标;
ImageLocagion :加载图片的磁盘或者Web的位置;
2、PictureBox 实例实现的功能:显示4张图像,其中两张图片是静态的,两张是不停的变化,鼠标双击图像时,图像会变大或者缩小。
3、具体代码实现:
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;//文件数
public int ImageCount
![Winform --- PictureBox控件 实例 winform实例](http://img.aihuau.com/images/31101031/31105747t0132c755e2b928293d.jpg)
{
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);//创建文件夹对象
if (directoryInfo.Exists)
{
FileInfo[] fileInfos = directoryInfo.GetFiles("*.png");//查询出后缀名为.png的所有文件
if(fileInfos!=null)
this.ImageCount = fileInfos.Count();
}
this.timer1.Start();//打开计数器
//this.pictureBox3.Image = this.imageList1.Images[0];//ImageList控件将大图像的像素改变了
this.pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;//设置图片大小
this.pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;//设置图片大小
}
//计时器事件
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);//将整数格式化为“/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;//放大缩小标示
}
}
}
}
}