123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsImageList
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //以下两行代码用于获取第一张要加载的图片的路径
- string Path = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
- Path += @"\01.jpg";
- string Path2 = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
- Path2 += @"\02.jpg";
- //创建图像
- Image Mimg = Image.FromFile(Path, true);
- //只有创建图像后,才能通过Add方法来添加图像
- //通过图像类型Image的FromFile方法来加载Path路径,就获得了对应的图像对象
- imageList1.Images.Add(Mimg);
- //通过ImageList的Images属性,调用Add方法添加图片
- Image Mimg2 = Image.FromFile(Path2, true);//创建图像
- imageList1.Images.Add(Mimg2);
- //设置图像大小
- imageList1.ImageSize = new Size(200, 165);
- //初始化PictureBox控件的宽度和高度
- pictureBox1.Width = 200;
- pictureBox1.Height = 165;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //从ImageList中,通过Images属性以索引器的形式取出来
- pictureBox1.Image = imageList1.Images[0];
- }
- private void button2_Click(object sender, EventArgs e)
- {
- pictureBox1.Image = imageList1.Images[1];
- }
- }
- }
|