Form1.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsImageList
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. private void Form1_Load(object sender, EventArgs e)
  19. {
  20. //以下两行代码用于获取第一张要加载的图片的路径
  21. string Path = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
  22. Path += @"\01.jpg";
  23. string Path2 = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
  24. Path2 += @"\02.jpg";
  25. //创建图像
  26. Image Mimg = Image.FromFile(Path, true);
  27. //只有创建图像后,才能通过Add方法来添加图像
  28. //通过图像类型Image的FromFile方法来加载Path路径,就获得了对应的图像对象
  29. imageList1.Images.Add(Mimg);
  30. //通过ImageList的Images属性,调用Add方法添加图片
  31. Image Mimg2 = Image.FromFile(Path2, true);//创建图像
  32. imageList1.Images.Add(Mimg2);
  33. //设置图像大小
  34. imageList1.ImageSize = new Size(200, 165);
  35. //初始化PictureBox控件的宽度和高度
  36. pictureBox1.Width = 200;
  37. pictureBox1.Height = 165;
  38. }
  39. private void button1_Click(object sender, EventArgs e)
  40. {
  41. //从ImageList中,通过Images属性以索引器的形式取出来
  42. pictureBox1.Image = imageList1.Images[0];
  43. }
  44. private void button2_Click(object sender, EventArgs e)
  45. {
  46. pictureBox1.Image = imageList1.Images[1];
  47. }
  48. }
  49. }